0

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?

jucajl
  • 1,195
  • 3
  • 12
  • 29
  • Read here: http://stackoverflow.com/questions/2590947/about-finish-in-android and change the position of finish() and stop() instructions. – rosco May 22 '12 at 14:51
  • when I call the method finish (); the button to return to the previous activity works correctly, as you can see in my post on "voltar.setOnclickListener" the problem is when the User does not come back for my button, but the return of the native android . – jucajl May 22 '12 at 16:51

1 Answers1

0

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

Awais
  • 222
  • 3
  • 14
  • did not quite understand your answer, you told me to put mediaPlayer.stop () in onStop (), but what is this onStop ()? a method? the button I have mediaPlayer.stop stop (); as you can see in my post, and it works perfectly, except when the User presses the button to return all phones with android is because the button closes the activity of the activity and when I return is as if the MediaPlayer is stopped, because the activity started from scratch. – jucajl May 22 '12 at 16:46
  • was only understand the life cycle of activity – jucajl May 22 '12 at 17:14