Nafiz
Nafiz

Reputation: 206

Media player not playing any sound

I am developing a simple calculator for a device, there I am using mediaplayer to play some sound when a button is pressed using

media=MediaPlayer.create(this, R.raw.wipe);
            media.start();

and in a reset button i am releasing it with

media.release();

but after some button clicks it stops playing any sound. How can I fix this problem?

Upvotes: 0

Views: 382

Answers (4)

Jinsi K
Jinsi K

Reputation: 267

On click of reset button , also use media.reset(); after media.release();

Upvotes: 0

Akhil
Akhil

Reputation: 14038

MediaPlayer is not a good option when you are playing small sound effects as the user can click on multiple buttons very soon and you will have to create a MP object for all of them which doesnt happen synchronously. That is why you are not hearing sounds for every click. Go for the SoundPool Class which allows you to keep smaller sounds loaded in memory and you can play them any time you want without any lag which you would feel in a mediaplayer. http://developer.android.com/reference/android/media/SoundPool.html Here is a nice tutorial : http://www.anddev.org/using_soundpool_instead_of_mediaplayer-t3115.html

Upvotes: 1

dinesh707
dinesh707

Reputation: 12582

I suggest you to play your media always in another thread.

Upvotes: 0

Hasmukh
Hasmukh

Reputation: 4670

try like this,

MediaPlayer media=MediaPlayer.create(this, R.raw.wipe);
            media.start();

On reset button write like:

media.release();
media.reset();

Upvotes: 0

Related Questions