MediaPlayer stops playing the sounds - Android

前端 未结 2 606
北恋
北恋 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.

提交回复
热议问题