Media Player start stop start

后端 未结 9 2093
死守一世寂寞
死守一世寂寞 2020-12-10 13:09

I am making a new android sound application. I made a clickable button to play sound when I click on it. But I also want it to stop playing sound when I click for the second

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 13:34

    You're calling mpButtonClick1.reset() after mpButtonClick1.stop() - don't do that:

    if (mpButtonClick1.isPlaying()) {
        mpButtonClick1.stop();
        mpButtonClick1.reset(); //<--------- calling reset(), remove this line
    }
    

    The docs for reset() say:

    Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare().

    Remove mpButtonClick1.reset() and it should work.

    Keep in mind that MediaPlayer works as a state machine, which means that if you call methods in the wrong order, you'll get problems. Please read about MediaPlayer here and here.

提交回复
热议问题