Imagebutton with sound is not working

后端 未结 1 1953
不知归路
不知归路 2021-01-27 14:10

i made a program in android studio which has 10 imagebutton, each imagebutton when clicked will produce a sound. However, when i run them on emulator only first 7 imagebutton wi

相关标签:
1条回答
  • As MediaPlayer is made for some long running or big media files which includes various formats of Videos, audios so making use of it for some small tones is bad because it itself includes big calculations to perform. There is a architecture diagram to follow to make use of MediaPlayer well instead you should use SoundPool because it is designed for such purposes only.

    Now the main answer to your problem is

    As stated on official documentation

    "Create" Added in API level 1

    MediaPlayer create (Context context, int resid)

    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. Note that since prepare() is called automatically in this method, you cannot change the audio stream type (see setAudioStreamType(int)), audio session ID (see setAudioSessionId(int)) or audio attributes (see setAudioAttributes(AudioAttributes) of the new MediaPlayer.

    In short you must follow the state diagram of MediaPlayer to get safety from such kind of errors or exceptions. You must call release method to release the resources after every call.

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