简体   繁体   中英

Android MediaPlayer not stopping by clicking another button

When you click on two buttons you hear two sounds. How can i Stop the first sound and play the second sound or when i play the first sound, that the second sound stops? How can i create this? Here my Media PLayer

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

                        }

You should place your boolean b = false ; line out of the onClick method it should be as a global variable, because on every click, when you go to the onClick method, your b variable gets a false value and it never steps into the else clause of your if-else statement. Everything else is okay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM