Android - 播放并停止不同的媒体播放器,点击不同的按钮

时间:2022-03-31 18:59:36

I have multi buttons and multi song when I clicked in another button the first button dont stop Assume I have two buttons: Button1 and Button2. If I click on Button1 then audio file A.mp3 plays. After this click on Button2 then audio file A.mp3 stops and B.mp3 plays. Then again click on Button1 to stop B.mp3 and play A.mp3 instantly and so on.

当我点击另一个按钮时,我有多个按钮和多个歌曲,第一个按钮不停止假设我有两个按钮:Button1和Button2。如果我单击Button1,则播放音频文件A.mp3。点击Button2后,音频文件A.mp3停止,B.mp3播放。然后再次单击Button1以停止B.mp3并立即播放A.mp3,依此类推。

This is what i have tried so far

这是我到目前为止所尝试的

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);
}

1 个解决方案

#1


Once a user taps on file1 then the file1 start play. And when user taps on another file, the file1 pause the playing and start playing for the tapped file. This should happen for all file.

一旦用户点击file1,文件1就开始播放。当用户点击另一个文件时,file1暂停播放并开始播放所点击的文件。这应该发生在所有文件中。

#1


Once a user taps on file1 then the file1 start play. And when user taps on another file, the file1 pause the playing and start playing for the tapped file. This should happen for all file.

一旦用户点击file1,文件1就开始播放。当用户点击另一个文件时,file1暂停播放并开始播放所点击的文件。这应该发生在所有文件中。