Android stops when you press a button several times

2

I am developing an app to listen to streaming radios but if I press the button 3 times, it stops. Another problem is that I would like you to be a second in buferring before I start playing to prevent it from starting, stop and continue. Log Cat:

10-02 21:22:15.160 20358-20358/com.david.animefm2 V/MediaPlayer: message received msg=100, ext1=-38, ext2=0
10-02 21:22:15.160 20358-20358/com.david.animefm2 V/MediaPlayer: notify(100, -38, 0) callback on disconnected mediaplayer
10-02 21:22:15.265 20358-20358/com.david.animefm2 D/[email protected][MainActivity]: ViewPostImeInputStage processPointer 0
10-02 21:22:15.323 20358-20358/com.david.animefm2 D/[email protected][MainActivity]: ViewPostImeInputStage processPointer 1
10-02 21:22:15.324 20358-20358/com.david.animefm2 V/MediaPlayer: setVideoSurfaceTexture
10-02 21:22:15.324 20358-20358/com.david.animefm2 V/MediaPlayer: prepare
10-02 21:22:15.324 20358-20358/com.david.animefm2 E/MediaPlayer: prepareAsync called in state 0, mPlayer(0x0)
10-02 21:22:15.325 20358-20358/com.david.animefm2 D/AndroidRuntime: Shutting down VM
10-02 21:22:15.325 20358-20358/com.david.animefm2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.david.animefm2, PID: 20358
                                                                    java.lang.IllegalStateException
                                                                        at android.media.MediaPlayer._prepare(Native Method)
                                                                        at android.media.MediaPlayer.prepare(MediaPlayer.java:1408)
                                                                        at com.david.animefm2.MainActivity$1.onClick(MainActivity.java:72)
                                                                        at android.view.View.performClick(View.java:6261)
                                                                        at android.widget.TextView.performClick(TextView.java:11180)
                                                                        at android.view.View$PerformClick.run(View.java:23748)
                                                                        at android.os.Handler.handleCallback(Handler.java:751)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

This is my code:

//Boton Reproducir y pausa
    Button start = (Button) findViewById(R.id.START);
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isPlay ==0) {
                try {
                    mediaPlayer.setDataSource(url);
                    mediaPlayer.prepare();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                mediaPlayer.start();
                //start.setText("PAUSE");
                isPlay = 1;
            } else if (isPlay == 1) {
                mediaPlayer.stop();
                //start.setText("START");
                isPlay = 2;
            }else if (isPlay == 2){
                try {
                    mediaPlayer.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();
                //start.setText("PAUSE");
                isPlay = 1;
            }else {
                Toast.makeText(getApplicationContext(),"First select a radio station", Toast.LENGTH_SHORT).show();
            }
        }
    });
    
asked by Djdadi43 02.10.2017 в 21:00
source

1 answer

2

In this case the error is a IllegalStateException when trying to use the .prepare() method:

  

FATAL EXCEPTION: main Process: com.david.animefm2, PID: 20358   java.lang.IllegalStateException at   android.media.MediaPlayer._prepare (Native Method) at   android.media.MediaPlayer.prepare (MediaPlayer.java:1408) at   com.david.animefm2.MainActivity $ 1.onClick (MainActivity.java:72)

In this case, you should not use prepare() and then start() or vice versa:

 mediaPlayer.prepare();
 mediaPlayer.start();

You must use the listener OnPreparedListener () to determine when the MediaPlayer is ready to play and in this way call the start() method to start playback:

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer arg0) {
        //Inicia reproducción.
        mediaPlayer.start();
    }
});

Regarding what you do:

  

If I press the button 3 times, it stops.

You must validate if the MediaPlayer is playing if it is so you must stop the playback, load the new url and perform the previous procedure, prepare and play:

 if(mediaPlayer!=null && mediaPlayer.isPlaying()){
        mediaPlayer.stop();
        mediaPlayer.reset();     
 }

If you need to implement buffering, review the options suggested by @WebServeis:

MediaPlayer - Do I need to create a buffer? How do I do it?

    
answered by 02.10.2017 / 22:19
source