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

后端 未结 3 1425
误落风尘
误落风尘 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: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.

提交回复
热议问题