0

this is what i have in my android currently its playing but unable to stop the mp3 it crashes at the mediaPlayer.stop();

  private static MediaPlayer mediaPlayer;
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view= inflater.inflate(R.layout.fragment_home, container, false);
        Button play = (Button) view.findViewById(R.id.play);
        Button stop = (Button) view.findViewById(R.id.stop);
        play.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                try {
                    if (mediaPlayer == null){
                        final  MediaPlayer mediaPlayer = new MediaPlayer();

                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mediaPlayer.setDataSource("http://www.xxxxx/001.mp3");
                        mediaPlayer.prepare();
                        if (!mediaPlayer.isPlaying()) {
                            System.out.println("Should Play");
                            mediaPlayer.start();
                        }}
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            });


 public synchronized void stopMedia()
    {
        if( mediaPlayer.isPlaying())
        {
            mediaPlayer.stop();


            //status = MediaStatus.STOP;

        }
    }

it plays but i am unable to stop it , it crashes the app at
mediaplayer.stop();
not sure why wasted long hours searching for a clue.

=============== here is the new code according to the comment

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            final View view= inflater.inflate(R.layout.fragment_home, container, false);
            Button play = (Button) view.findViewById(R.id.play);


            Button stop = (Button) view.findViewById(R.id.stop);
            play.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    String url = "http://www.brothershouse.narod.ru/music/pepe_link_-_guitar_vibe_113_club_mix.mp3"; // your URL here
                    MediaPlayer myMediaPlayer = new MediaPlayer();
                    myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    try {
                        myMediaPlayer.setDataSource(url);
                        myMediaPlayer.prepareAsync(); // might take long! (for buffering, etc)

                    } catch (IOException e) {
                     //   Toast.makeText(this, "mp3 not found", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }

                    //mp3 will be started after completion of preparing...
                    myMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                        @Override
                        public void onPrepared(MediaPlayer player) {
                            player.start();
                        }

                    });

                }});



now how do i stop this from a different button
stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //stop

                stopPlaying();

                try {


                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        });



private void stopPlaying() {

        myMediaPlayer.pause();

}

now how do i stop the playback once again

  • use `mediaPlayer.reset()` instead, also if you're using using media from a url (not local storage) its better to call `prepareAsync()` and implement the `onPreparedListener()` method `onPrepared(MediaPlayer mp)` – Mark Feb 22 '16 at 16:38
  • is it possible to show me an example how onprepared code looks like as i am new and not sure if i have done this but i am not afriad to research on it , il kindly appreciate it if you can provide me with the code so i can se the example cheers. the mediaPlayer.reset() still crashes the app not sure why – Jusna Leema Feb 22 '16 at 16:58
  • i found this searching http://stackoverflow.com/questions/10307131/android-mediaplayer-prepareasync-method how ever i cannot beleive only for 1 stop button so much hey lol – Jusna Leema Feb 22 '16 at 17:00
  • Good example here : https://android.googlesource.com/platform/development/+/master/samples/RandomMusicPlayer/src/com/example/android/musicplayer/MusicService.java most of the code you want to look at is in MusicService.java - I would also recommend that if you intend for music to continue playing once the app finishes that you use a service. – Mark Feb 22 '16 at 17:22
  • hi sir , erm when the play button is clicked the music plays leave it to that , i only require now a stop code i tried few they dont work in above code , check the codes above – Jusna Leema Feb 22 '16 at 18:03
  • So does it continue to play OK when you rotate the device, I.e. a configuration change? The sample code in the link is just placed in a service class .. All the fundemental information is there, also unlike copy and pasting code from SO and it not working, this example is fully commented so you can understand exactly what's happening. – Mark Feb 22 '16 at 18:17
  • hi buddy this code of mine above from were it says =============== here is the new code according to the comment am using this code only trying to figure out how to use the stop so the song stops. – Jusna Leema Feb 22 '16 at 20:46
  • this helped looked into the code took out what i need this is what i took out this code il just paste the top line since i cannot paste all here – Jusna Leema Feb 22 '16 at 21:11
  • void createMediaPlayerIfNeeded() { this whole code makes single player i can stop it now thanks. – Jusna Leema Feb 22 '16 at 21:12

0 Answers0