MediaPlayer stops playing the sounds - Android

前端 未结 2 605
北恋
北恋 2021-01-06 04:37

Here is a simple piano app and it works but there is a problem. After about 20 clicks (sometimes it is exactly 28 clicks) even I click the buttons it doesn\'t play any sound

相关标签:
2条回答
  • 2021-01-06 05:02

    Looks like you're creating a new MediaPlayer instance to play each sound. You should either reuse them or clean them up.

    From the documentation of the MediaPlayer.create() method:

    Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again.

    When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception.

    0 讨论(0)
  • 2021-01-06 05:15

    simple way is to add onComplationListener

    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer p1) {
            p1.release();
        }
    });
    
    0 讨论(0)
提交回复
热议问题