【问题标题】:Play sound using soundpool example使用 soundpool 示例播放声音
【发布时间】:2013-06-08 19:52:58
【问题描述】:

我想学习如何使用soundpool 方法。我希望您向我展示一个运行 2 种声音的非常简单的示例。

【问题讨论】:

    标签: android media soundpool


    【解决方案1】:

    your_app/res/ 下创建一个名为raw 的文件夹。然后将您的铃声粘贴到此文件夹中,例如your_app/res/raw/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);
    

    使用后务必释放 SoundPool 资源:

    soundPool.release();
    soundPool = null;
    

    【讨论】:

    • SoundPool 构造函数现在已弃用,但由于 SoundPool.Builder 需要 API 级别 21,我想我仍在使用它。
    • 最后两行(MediaPlayer 的东西)不是必需的。对于其他人来说,这很有帮助。谢谢:)
    • @voghDev ,为什么没有必要?
    • @TheFlash ,在我们将内容设置为null 之后,垃圾收集器不会自动执行它吗?调用 release 有什么意义?
    【解决方案2】:

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

    1) 你需要创建 AudioAttributes 对象:

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

    2) 创建 SoundPool 对象:

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

    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);
    }
    

    【讨论】:

    • 注意:如果你想拥有相同的行为,你也需要一个 .setMaxStreams(5) 在 Builder 的 createNewSoundPool 中,同时播放多个声音,就像旧的一样。 5 也可以是最终的 int 或其他东西。
    【解决方案3】:

    这是soundPool 的一个小工作示例,它取自here,并稍作修改以匹配post 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;
      } 
    }
    

    【讨论】:

    • amg 未使用
    • 已初始化但从未使用过。
    • 加一个有用的答案,尽管 amg 行不需要完成(在没有它的情况下测试)
    • getApplicationContext();导致错误。 "不能从静态上下文中引用非静态方法"
    【解决方案4】:

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

    谢谢。

    【讨论】:

    • 我发现 SoundPoolManager 代码可读,但我不清楚使用 int 代替 SoundPool 的好处。我猜是为了方便。如果您能更全面地解释这一点,将会有所帮助。 SoundPoolManager 是否解决了 SoundPool 的缺点,使我的代码更安全/更便携/更易于管理/更高效?如果是这样,如何以及为什么?提前致谢。
    • 有趣的样本。 onLoadComplete + 回调
    【解决方案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);
    

    【讨论】:

    • 干得好。它奏效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多