当我点击另一个按钮时,我有多个按钮和多首歌,第一个按钮不会停止 假设我有两个按钮:Button1和Button2。如果我单击Button1,则播放音频文件A.mp3。点击Button2后,音频文件A.mp3停止,B.mp3播放。然后再次单击Button1以停止B.mp3并立即播放A.mp3,依此类推。
这是我到目前为止所尝试的
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;
btn.setOnClickListener(new OnClickListener() {
@SuppressLint("ResourceAsColor")
public void onClick(View v) {
Log.i("TAG", "index :" + index);
Toast.makeText(getApplicationContext(),
"Clicked Button Index :" + btn.getId(),
Toast.LENGTH_LONG).show();
if (btn.getText() == getString(R.string.play_str)) {
btn.setText(getString(R.string.pause_str));
txtchanson.setTextColor(R.color.green);
mp.start();
} else if (btn.getText() == getString(R.string.pause_str)) {
mp.pause();
btn.setText(getString(R.string.play_str));
txtchanson.setTextColor(R.color.gray);
}
}
});
ll.addView(btn);
ll.addView(txtchanson);
ll.addView(txtduration);
lm.addView(ll);
}
答案 0 :(得分:0)
一旦用户点击file1,文件1就开始播放。当用户点击另一个文件时,file1暂停播放并开始播放所点击的文件。这应该发生在所有文件中。