Skip to content Skip to sidebar Skip to footer

Imagebutton With Sound Is Not Working

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 will

Solution 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.

Post a Comment for "Imagebutton With Sound Is Not Working"