1

While playing second song我實現了播放/停止按鈕。當我在播放時按下相同的按鈕,它應該停止或暫停& &背景也會改變播放 - >停止。當我在播放時按下另一個按鈕時,應該停止並播放新的歌曲。不改變後臺播放--->停止。幫我out.how獲得價值上一首歌曲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; 
} 

}

+0

你能編輯這個問題嗎?背景是什麼意思?你是在播放歌曲的背景嗎?精心製作 –

+0

你到底想要什麼??你想播放歌曲時按下播放,每當改變歌曲時改變背景? – PankajSharma

+0

添加圖像來告訴你到底想要什麼...... –

回答