1

在播放第二首歌曲时 我实现了播放/停止按钮。当我在播放时按下同一个按钮时,它应该是停止或暂停&&背景也改变播放 - >停止。当我在播放时按下另一个按钮时,它应该停止并播放新歌曲。它有效。但不能t改变背景播放--->停止。帮帮我。如何获得以前的歌曲ID值...

         public class MySimpleArrayAdapter extends ArrayAdapter<String>{
protected static final String TAG = "MySImpleArrayAdapter";
private final Context context;
protected ListView mListView;
int itemno;
 Boolean playing = false;
private MediaPlayer mp;
private LayoutInflater inflater;
 String[] values;  
 int songs []=                   {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5,R.raw.sound6,R.raw.sound7};
  static class ViewHolder {
        public TextView text;
        public Button button;

      }

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.activity_main, values);
    inflater = LayoutInflater.from(context);
    this.context = context;
    this.values = values;

  }


 @Override
  public View getView( int position, View rowView, ViewGroup parent) {

     if (rowView == null) {

          rowView = inflater.inflate(R.layout.activity_main, null);
          ViewHolder viewHolder = new ViewHolder();
          viewHolder.text = (TextView) rowView.findViewById(R.id.label);
                  viewHolder.button = (Button) rowView
              .findViewById(R.id.logo);

          rowView.setTag(viewHolder);
        }

        ViewHolder holder = (ViewHolder) rowView.getTag();
        String s = values[position];
      final int   pos=position;
        holder.text.setText(s);
        holder.button.setOnClickListener(
                new OnClickListener() {
                    public void onClick(View v) {
                         if(mp == null){

                              //  mListView.invalidateViews();
                                mp = MediaPlayer.create(context,songs[pos]);

                                mp.start();
                                playing = true;
                                itemno = pos;
                                //Toast.makeText(context, "ist playing", Toast.LENGTH_LONG).show();
                                v.setBackgroundResource(R.drawable.ok);

                            }
                            else {
                                if(mp.isPlaying() && itemno == pos){

                                    mp.pause();
                                    playing = false;

                                    v.setBackgroundResource(R.drawable.no);
                                }
                                else{
                                    if(playing == false  && itemno == pos){

                                        mp.start();
                                        playing = true;
                                        v.setBackgroundResource(R.drawable.ok);
                                    }
                                    else {

                                        mp.stop();

                                        mp.release();
                                        mp = MediaPlayer.create(context,songs[pos]);
                                        mp.start();
                                        playing = true;
                                        itemno = pos;
                                        //Toast.makeText(context, "playing", Toast.LENGTH_LONG).show();
                                        v.setBackgroundResource(R.drawable.ok);

                                    }if (!mp.isPlaying())
                                        v.setBackgroundResource(R.drawable.no);

                                }
                            }

                    }
                });






        return rowView;


      }

 @Override
 public boolean  areAllItemsEnabled() {
     return false;          
 }

 @Override
 public boolean isEnabled(int position) {
         return false;
 }

}

4

1 回答 1

1

http://www.brightec.co.uk/blog/custom-android-media-controller

http://www.verious.com/tutorial/android-building-audio-player-tutorial/

您好,这可能会帮助您...浏览这些链接并找到解决方案。

于 2013-07-17T06:18:48.990 回答