I would like to play sound after touching the button. MediaPlayer works fine, but I read somewhere that this library is for long .wav (like music).
Is there any better way to play short .wav(2-3 sec.)?
In addition to playing WAV files on Android through converting WAV to Android supported formats, you can use a WAV player for Android or an Android music player which enables to open and play wav on Android as well, such as “Remote Wave Free”. If you have VLC Media Player, you can use VLC to convert WAV files too.
VLC for Android is the Android version of VLC, which lets you open WAV file with this free media player on your Android device. VLC for Android plays most local video and audio files, as well as homemade DVD ISOs, like the desktop version of VLC. Pros: Play WAV files directly on your Android phone.
Top 1: Tipard Blu-ray Player Thanks to the wide support of media formats, you can play almost all audio tracks smoothly, including WAV, OGG, MP4, AAC, FLAC, APE, WMA, and more. Besides, you can adjust the audio track, customize the audio volume and make other changes during the Blu-ray or DVD disc movie playback.
On Windows, the Windows Media Player is capable of playing WAV files. On MacOS, iTunes or QuickTime can play WAV files. On Linux, your basic ALSA system can play these files.
The SoundPool is the correct class for this. The below code is an example of how to use it. It is also the code I use in several apps of mine to manage the sounds. You can have as may sounds as you like (or as memory permits).
public class SoundPoolPlayer {
private SoundPool mShortPlayer= null;
private HashMap mSounds = new HashMap();
public SoundPoolPlayer(Context pContext)
{
// setup Soundpool
this.mShortPlayer = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSounds.put(R.raw.<sound_1_name>, this.mShortPlayer.load(pContext, R.raw.<sound_1_name>, 1));
mSounds.put(R.raw.<sound_2_name>, this.mShortPlayer.load(pContext, R.raw.<sound_2_name>, 1));
}
public void playShortResource(int piResource) {
int iSoundId = (Integer) mSounds.get(piResource);
this.mShortPlayer.play(iSoundId, 0.99f, 0.99f, 0, 0, 1);
}
// Cleanup
public void release() {
// Cleanup
this.mShortPlayer.release();
this.mShortPlayer = null;
}
}
You would use this by calling:
SoundPoolPlayer sound = new SoundPoolPlayer(this);
in your Activity's onCreate() (or anytime after it). After that, to play a sound simple call:
sound.playShortResource(R.raw.<sound_name>);
Finally, once you're done with the sounds, call:
sound.release();
to free up resources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With