Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play sound using soundpool example

I would like to learn how to use soundpool method. I would like you to show me a very simple example that run 2 sounds.

like image 908
Sally Gomez Avatar asked Jun 12 '13 15:06

Sally Gomez


People also ask

What is SoundPool How to create a SoundPool?

The SoundPool class manages and plays audio resources for applications. A SoundPool is a collection of sound samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaCodec service to decode the audio into raw 16-bit PCM.

How to use SoundPool in Android Studio?

So learning SoundPool class gives a better understanding to deal with the short audio clips. right-click on res folder -> Android Resource Directory, make sure to select resource type as raw. In this raw folder paste your audio clips.

How to play sound in Android Kotlin?

This example demonstrates how to play sound using SoundPool in Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.


2 Answers

Create a folder named as raw under your_app/res/. Then paste your ringtone in this folder, for example your_app/res/raw/ringtone.mp3. Now use the following code:

SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
int soundId = soundPool.load(context, R.raw.ringtone, 1);
// soundId for reuse later on

soundPool.play(soundId, 1, 1, 0, 0, 1);

Be sure to release the SoundPool resources after use:

soundPool.release();
soundPool = null;
like image 143
TheFlash Avatar answered Oct 19 '22 11:10

TheFlash


Yes. i went through this too. but for safety i have saved a piece of code i found online. Though havent used it, i know it will come in handy soon...

1) You need to create AudioAttributes object:

AudioAttributes attributes = new AudioAttributes.Builder()
    .setUsage(AudioAttributes.USAGE_GAME)
    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
    .build();

2) Create SoundPool object:

SoundPool sounds = new SoundPool.Builder()
    .setAudioAttributes(attributes)
    .build();

3) How to use SoundPool on all API levels example :

SoundPool sound;

protected void createSoundPool() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        createNewSoundPool();
    } else {
        createOldSoundPool();
    }
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void createNewSoundPool(){
    AudioAttributes attributes = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_GAME)
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .build();
    sounds = new SoundPool.Builder()
        .setAudioAttributes(attributes)
        .build();
}

@SuppressWarnings("deprecation")
protected void createOldSoundPool(){
    sounds = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
}
like image 64
user3833732 Avatar answered Oct 19 '22 12:10

user3833732