当您点击两个按钮时,您会听到两个声音。如何停止第一个声音并播放第二个声音或当我播放第一个声音时,第二个声音停止?我怎么能创造这个?这是我的媒体播放器
final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
JSONArray songs = (JSONArray) response.getJSONArray("songs");
for ( int i = 0; i < songs.length(); i++)
{
// Create LinearLayout
audio = songs.getJSONObject(i).getString(TAG_AUDIO) ;
String chanson = songs.getJSONObject(i).getString("title") ;
String duration = songs.getJSONObject(i).getString(TAG_DURATION) ;
LinearLayout ll = new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.HORIZONTAL);
final TextView txtchanson = new TextView(getApplicationContext());
txtchanson.setText(chanson);
txtchanson.setPadding(10, 0, 10, 0);
TextView txtduration = new TextView(getApplicationContext());
txtduration.setText(duration);
final Button btn = new Button(getApplicationContext());
final MediaPlayer mp = new MediaPlayer();
mp.setAudioSessionId(i+1);
mp.setDataSource(audio);
mp.prepare();
btn.setId(i+1);
btn.setTypeface(ionicons);
btn.setText(getString(R.string.play_str));
btn.setLayoutParams(params);
final int index = i;
mp.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
btn.setText(getString(R.string.play_str));
}
});
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Log.i("TAG", "index :" + index);
Toast.makeText(getApplicationContext(),
"Clicked Button Index :" + btn.getId(),
Toast.LENGTH_LONG).show();
boolean b = false ;
if ( b == false )
{
//txtchanson.setTextColor(R.color.green);
mp.start();
btn.setText(getString(R.string.pause_str));
b = true;
}
else //if (btn.getText().equals( getString(R.string.pause_str)))
{
mp.pause();
btn.setText(getString(R.string.play_str));
//txtchanson.setTextColor(R.color.gray);
b = false;
}
/* else if (b == true )
{
btn.setText(getString(R.string.play_str));
mp.pause();
}*/
}
});
ll.addView(btn);
ll.addView(txtchanson);
ll.addView(txtduration);
lm.addView(ll);
}
答案 0 :(得分:1)
您应该将boolean b = false ;
行放在onClick方法之外,它应该是一个全局变量,因为在每次点击时,当你转到onClick方法时,你的b
变量会得到一个假值它永远不会进入你的if-else语句的else子句。其他一切都还可以。