使用soundpool示例播放声音

Sal*_*mez 48 media android soundpool

我想学习如何使用soundpool方法.我希望你能给我看一个运行2个声音的非常简单的例子.

The*_*ash 66

创建一个命名为文件夹的原始your_app/res/.然后将您的铃声粘贴到此文件夹中your_app/res/ringtone.mp3.现在使用以下代码:

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);
Run Code Online (Sandbox Code Playgroud)

务必在使用后释放SoundPool资源:

soundPool.release();
soundPool = null;
Run Code Online (Sandbox Code Playgroud)

  • 现在不推荐使用SoundPool构造函数,但由于SoundPool.Builder需要API级别21,我想我还在使用它. (11认同)

use*_*732 51

是.我也经历过这个.但为了安全起见,我保存了一条在网上找到的代码.虽然没有使用它,但我知道它会很快派上用场......

1)您需要创建AudioAttributes对象:

AudioAttributes attributes = new AudioAttributes.Builder()
    .setUsage(AudioAttributes.USAGE_GAME)
    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
    .build();
Run Code Online (Sandbox Code Playgroud)

2)创建SoundPool对象:

SoundPool sounds = new SoundPool.Builder()
    .setAudioAttributes(attributes)
    .build();
Run Code Online (Sandbox Code Playgroud)

3)如何在所有API级别上使用SoundPool示例:

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 注意:如果您想要具有相同的行为,同时播放多个声音,就像旧的那样,您也需要在 createNewSoundPool 的 Builder 上使用 .setMaxStreams(5) 。另外,5 也可以是最终的整数或其他东西。 (2认同)

Ida*_*dan 6

这是的一个小型工作示例soundPool,它取自此处,并进行了略微修改以匹配21后的API。

需要注意的一件事是maxStreams,它指示允许并行运行多少个流,如果为一个(默认),则可以将其从构建器中删除。

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

public class SoundManager extends Activity
{
  static SoundPool soundPool;
  static int[] sm;

  public static void InitSound() {

    int maxStreams = 1;
    Context mContext = getApplicationContext();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        soundPool = new SoundPool.Builder()
                .setMaxStreams(maxStreams)
                .build();
    } else {
        soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
    }

    sm = new int[3];
    // fill your sounds
    sm[0] = soundPool.load(mContext, R.raw.sound_1, 1);
    sm[1] = soundPool.load(mContext, R.raw.sound_2, 1);
    sm[2] = soundPool.load(mContext, R.raw.sound_3, 1);

  }

  static void playSound(int sound) {

      soundPool.play(sm[sound], 1, 1, 1, 0, 1f);
  }

   public final void cleanUpIfEnd() {
    sm = null;
    soundPool.release();
    soundPool = null;
  } 
}
Run Code Online (Sandbox Code Playgroud)


Ash*_*i K 5

我编写了一个 SoundPoolManager,可用于加载声音文件并在需要时播放。你可以在这里看到它。

谢谢。

  • 我发现 SoundPoolManager 代码可读,但我不清楚使用 int 而不是 SoundPool 的好处。我猜是为了方便。如果您能更全面地解释这一点,将会有所帮助。SoundPoolManager 是否解决了 SoundPool 的缺点,使我的代码更安全/更便携/更易于管理/更高效?如果是这样,为什么?提前致谢。 (2认同)

Aja*_*ati 5

private final int NUMBER_OF_SIMULTANEOUS_SOUNDS = 5;
        private final float LEFT_VOLUME_VALUE = 1.0f;
        private final float RIGHT_VOLUME_VALUE = 1.0f;
        private final int MUSIC_LOOP = 0;
        private final int SOUND_PLAY_PRIORITY = 0;
        private final float PLAY_RATE= 1.0f;


        SoundPool soundPool;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            soundPool= new SoundPool.Builder()
                    .setMaxStreams(NUMBER_OF_SIMULTANEOUS_SOUNDS)
                    .build();
        } else {
            // Deprecated way of creating a SoundPool before Android API 21.
            soundPool= new SoundPool(NUMBER_OF_SIMULTANEOUS_SOUNDS, AudioManager.STREAM_MUSIC, 0);
        }
        int soundId = soundPool.load(getApplicationContext(), R.raw.sound_1, 1);

        soundPool.play(soundId , LEFT_VOLUME_VALUE , RIGHT_VOLUME_VALUE, SOUND_PLAY_PRIORITY , MUSIC_LOOP ,PLAY_RATE);
Run Code Online (Sandbox Code Playgroud)