Skip to content Skip to sidebar Skip to footer

How To Play Sound When Button Is Clicked In Android?

I'm trying to play a sound file when a button is clicked but keeps getting an error. The error is: 'The method create(Context, int) in the type MediaPlayer is not applicable for t

Solution 1:

There are a few things going on here (disclaimer, this is just how I'm used to using it, there may be a better way):

  • You seem to be doing a lot more work per click than you need to. You're creating and adding a new onClickListener for every click in the Activity's View, not the Button. You only need to set the listener once, and for the Button rather than the overarching View; I tend to do that in the constructor of the Activity.

  • Regarding your error, MediaPlayer works fine for me when the Context I pass it is the overriding Activity. When you pass this, it's passing the onClickListener you are creating, throwing off the MediaPlayer.

  • Finally, to actually play the sound, you have to call start().

So for the constructor in the Activity, you can create the MediaPlayer once, find the Button, and attach an onClickListener that will play the sound from the MediaPlayer you've just created. It would look something like:

publicclassMyActivityextendsActivity {

    publicMyActivity(Bundle onSavedStateInstance) {
        // eliding some bookkeeppingMediaPlayermp= MediaPlayer.create(this, R.raw.mamacita_zero);

        Buttonzero= (Button)this.findViewById(R.id.btnZero);
        zero.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                mp.start();
            }
        });
    }
}

Hope that helps!

Solution 2:

I have played around with media-player, and it is easy to get in trouble. I followed the advice of Volodymyr, and SoundPool is much easier to manage.

MediaPlayer does not like to play more than one sound at the time, like for instance when you have lots of quick tabs on your buttons. I managed with the following method:

private void playSound(Uri uri) {
    try {
        mMediaPlayer.reset();
        mMediaPlayer.setDataSource(this, uri);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    } catch (Exception e) {
        // don't care
    }

}

In the constructor I did:

mMediaPlayer = new MediaPlayer();mSoundLess = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.less);mSoundMore = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.more);

On click I would then call playSound(mSoundLess):

Instead I have created a SoundPool helper:

package com.mycompany.myapp.util;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

publicclassSoundPoolHelperextendsSoundPool {
    private Set<Integer> mLoaded;
    private Context mContext;

    publicSoundPoolHelper(int maxStreams, Context context) {
        this(maxStreams, AudioManager.STREAM_MUSIC, 0, context);
    }

    publicSoundPoolHelper(int maxStreams, int streamType, int srcQuality, Context context) {
        super(maxStreams, streamType, srcQuality);
        mContext = context;
        mLoaded = newHashSet<Integer>();
        setOnLoadCompleteListener(newOnLoadCompleteListener() {
            @OverridepublicvoidonLoadComplete(SoundPool soundPool, int sampleId, int status) {
                mLoaded.add(sampleId);
            }
        });
    }

    publicvoidplay(int soundID) {
        AudioManageraudioManager= (AudioManager) mContext.getSystemService( Context.AUDIO_SERVICE);
        floatactualVolume= (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        floatmaxVolume= (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        floatvolume= actualVolume / maxVolume;
        // Is the sound loaded already?if (mLoaded.contains(soundID)) {
            play(soundID, volume, volume, 1, 0, 1f);
        }
    }
}

Now I init like this:

mSoundPoolHelper = new SoundPoolHelper(1, this);mSoundLessId = mSoundPoolHelper.load(this, R.raw.less, 1);mSoundMoreId = mSoundPoolHelper.load(this, R.raw.more, 1);

and play a sound like this:

privatevoidplaySound(int soundId){
    mSoundPoolHelper.play(soundId);
}

Don't forget to call mSoundPoolHelper.release();, for instance in your onDestroy(). Something similar is needed if you use MediaPlayer.

Solution 3:

http://developer.android.com/reference/android/media/SoundPool.html This is better for small sounds http://www.vogella.com/articles/AndroidMedia/article.html#tutorial_soundpool - tutorial

Solution 4:

MediaPlayermp= MediaPlayer.create(getBaseContext(),
R.raw.yoursoundfile);
mp.start();

the file yoursoundfile must to be in the res/raw folder

Solution 5:

If you really have to invoke the click programmatically because the view has no own sound, i would solve it like that, its the simplest solution and a oneliner

view.playSoundEffect(SoundEffectConstants.CLICK);

very simple and works, if you want to make a layout play a sound you need to put

android:soundEffectsEnabled="true"

in your xml.

Post a Comment for "How To Play Sound When Button Is Clicked In Android?"