Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓-播放和停止不同的mediaplayer,点击不同的按钮_Java_Android_Button_Android Mediaplayer - Fatal编程技术网

Java 安卓-播放和停止不同的mediaplayer,点击不同的按钮

Java 安卓-播放和停止不同的mediaplayer,点击不同的按钮,java,android,button,android-mediaplayer,Java,Android,Button,Android Mediaplayer,我有多个按钮和多首歌曲,当我点击另一个按钮时,第一个按钮不会停止 假设我有两个按钮:按钮1和按钮2。如果我点击按钮1,音频文件A.mp3就会播放。点击按钮2后,音频文件A.mp3停止播放,B.mp3播放。然后再次单击按钮1,停止B.mp3并立即播放A.mp3,依此类推 这就是我迄今为止所尝试的 final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain); LinearLayout.LayoutParams params

我有多个按钮和多首歌曲,当我点击另一个按钮时,第一个按钮不会停止 假设我有两个按钮:按钮1和按钮2。如果我点击按钮1,音频文件A.mp3就会播放。点击按钮2后,音频文件A.mp3停止播放,B.mp3播放。然后再次单击按钮1,停止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);
}

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

您是否正在为每首歌曲创建布局?当前发生了什么?为什么不起作用?btn.getText==getStringR.string.play\u str应该是btn.getText.EqualGetStringr.string.play\u strit不工作,当我点击两个按钮play!!!