Android MediaPlayer Error (-38, 0) “stop called in state 0”

后端 未结 3 1423
误落风尘
误落风尘 2021-02-13 22:02

I have looked at various different websites looking for a solution for this problem in my code. It is a basic audio player with 3 buttons: play, pause and stop. Play and pause w

相关标签:
3条回答
  • 2021-02-13 22:39

    I fixed this problem by `mediaPlayer.prepare(); My problem is about live radio streaming and I achieve this problem like below code:

    if (!radioIsOpen) {
                    try {
                        mediaPlayer = new MediaPlayer();
                        mediaPlayer
                                .setDataSource(URL);
                        mediaPlayer.prepare();
                    } catch (Exception e) {
                    }
    
                    mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
    
                        public void onPrepared(MediaPlayer mp) {
                            mediaPlayer.start();
                        }
                    });
                    Toast.makeText(getBaseContext(), "Radio is opening...", 2).show();
                } else {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                        mediaPlayer.release();
                        Toast.makeText(getBaseContext(), "Radio is closing...", 2).show();
                    }
                }
    
    0 讨论(0)
  • 2021-02-13 22:48

    I fixed this problem by removing the player.stop() which was being called when the player finished the song because of player.setOnCompletionListener(). This has now resolved the problem and the player is working perfectly.

    0 讨论(0)
  • 2021-02-13 22:49

    The problem is once you've stopped it, you setDataSource again. You have to then call prepare()

    player.setDataSource(getAssets().openFd("raw/airbourne_runnin_wild.mp3").getFileDescriptor());
    player.prepare();
    

    Anytime you call setDataSource() on it you will need to prepare() it again. Think of it as loading the file, getting ready for playback.

    By the way I notice you mix up calling MediaPlayer.create() and also call setDataSource() multiple times for the same file. There's no need to do this. Once the MediaPlayer is created and prepared (which calling .create() does for you), you can just call .stop(), .pause(), .play() on it, no need to reset it all the time.

    0 讨论(0)
提交回复
热议问题