简体   繁体   中英

android media player mp3 streaming audio on the other wheel

have the following code to run an mp3 podcast

public void playPodcast( final Uri data){
            new Thread (new Runnable(){
                public void run() {
                  if(!mediaPlayer.isPlaying()){
                        try {
                            mediaPlayer.setDataSource(getApplicationContext(), data);
                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (SecurityException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            mediaPlayer.prepare();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } 
                        mediaFileLengthInMilliseconds = mediaPlayer.getDuration();
                        duracaoPodcast = mediaPlayer.getDuration();

                        mediaPlayer.start();


                        if(!(controlTimer >= 1)){
                            timer();
                            controlTimer++;
                        }

                        primarySeekBarProgressUpdater();


                }
              } 
            }).start();
        }

and to close the User Intent have:

  voltar.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    finish();
                    mediaPlayer.stop();
                }
            });

the problem occurs when the User closes the intent that all the return button phones with android is because it does not:

finish();
mediaPlayer.stop();

makes only:

finish();

and so the audio is still playing, even if I go back to that intent the pause:

stop.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.stop(); 
                    frame.setVisibility(View.INVISIBLE);

                }

            }
        });

button does not work, and if I press the play he starts a new audio on top of that emptied playing,

any idea how to solve?

put mediaPlayer.stop(); in activity onStop() Method as far as i understood your question.

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