📜  Android 中的 SoundPool 示例

📅  最后修改于: 2021-10-28 02:42:01             🧑  作者: Mango

在本文中,我们将学习如何在 android 中添加SoundPool类。音频样本的集合可以从资源加载到内存中并可以使用。 SoundPool类用于播放简短的重复声音,而 MediaPlayer 类用于播放较长的剪辑,如音乐。它使用MediaPlayer服务来解码音频。外汇:- 游戏由多个级别的游戏组成。对于每个级别,都有一组仅由该级别使用的独特声音。为此,我们使用SoundPool类来完成我们的工作。所以学习 SoundPool 类可以更好地理解处理短音频剪辑。

方法:

  1. 为此创建一个新的Android 资源目录
    右键单击 res 文件夹 -> Android 资源目录,
    确保选择资源类型为原始。在这个原始文件夹中粘贴您的音频剪辑。
  2. activity_main.xml文件中添加以下代码。这里添加了三个按钮。如果单击每个按钮,将调用playSound方法。
    activity_main.xml
    
    
      
        


    MainActivity.java
    package org.geeksforgeeks.gfgsoundpool;
      
    import androidx.appcompat.app.AppCompatActivity;
    import android.media.AudioAttributes;
    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.os.Build;
    import android.os.Bundle;
    import android.view.View;
      
    public class MainActivity
        extends AppCompatActivity {
        SoundPool soundPool;
        int game_over,
            level_complete,
            player_died;
      
        @Override
        protected void onCreate(
            Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            if (Build.VERSION.SDK_INT
                >= Build.VERSION_CODES.LOLLIPOP) {
                AudioAttributes
                    audioAttributes
                    = new AudioAttributes
                          .Builder()
                          .setUsage(
                              AudioAttributes
                                  .USAGE_ASSISTANCE_SONIFICATION)
                          .setContentType(
                              AudioAttributes
                                  .CONTENT_TYPE_SONIFICATION)
                          .build();
                soundPool
                    = new SoundPool
                          .Builder()
                          .setMaxStreams(3)
                          .setAudioAttributes(
                              audioAttributes)
                          .build();
            }
            else {
                soundPool
                    = new SoundPool(
                        3,
                        AudioManager.STREAM_MUSIC,
                        0);
            }
      
            // This load function takes
            // three parameter context,
            // file_name and priority.
            game_over
                = soundPool
                      .load(
                          this,
                          R.raw.game_over,
                          1);
            level_complete
                = soundPool.load(
                    this,
                    R.raw.level_complete,
                    1);
            player_died
                = soundPool.load(
                    this,
                    R.raw.player_died,
                    1);
        }
      
        public void playSound(View v)
        {
            switch (v.getId()) {
      
            case R.id.button_sound1:
      
                // This play function
                // takes five parameter
                // leftVolume, rightVolume,
                // priority, loop and rate.
                soundPool.play(
                    game_over, 1, 1, 0, 0, 1);
                soundPool.autoPause();
                break;
      
            case R.id.button_sound2:
                soundPool.play(
                    player_died, 1, 1, 0, 0, 1);
                break;
      
            case R.id.button_sound3:
                soundPool.play(
                    level_complete, 1, 1, 0, 0, 1);
                break;
            }
        }
    }


  3. MainActivity 中添加以下代码。 Java文件。在这里,创建了 SoundPool 类的对象,并使用load方法添加了三个音频剪辑game_overlevel_completeplayer_died 。还添加了 playSound 方法,该方法播放与相应按钮关联的音频剪辑。

    主要活动。Java

    package org.geeksforgeeks.gfgsoundpool;
      
    import androidx.appcompat.app.AppCompatActivity;
    import android.media.AudioAttributes;
    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.os.Build;
    import android.os.Bundle;
    import android.view.View;
      
    public class MainActivity
        extends AppCompatActivity {
        SoundPool soundPool;
        int game_over,
            level_complete,
            player_died;
      
        @Override
        protected void onCreate(
            Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            if (Build.VERSION.SDK_INT
                >= Build.VERSION_CODES.LOLLIPOP) {
                AudioAttributes
                    audioAttributes
                    = new AudioAttributes
                          .Builder()
                          .setUsage(
                              AudioAttributes
                                  .USAGE_ASSISTANCE_SONIFICATION)
                          .setContentType(
                              AudioAttributes
                                  .CONTENT_TYPE_SONIFICATION)
                          .build();
                soundPool
                    = new SoundPool
                          .Builder()
                          .setMaxStreams(3)
                          .setAudioAttributes(
                              audioAttributes)
                          .build();
            }
            else {
                soundPool
                    = new SoundPool(
                        3,
                        AudioManager.STREAM_MUSIC,
                        0);
            }
      
            // This load function takes
            // three parameter context,
            // file_name and priority.
            game_over
                = soundPool
                      .load(
                          this,
                          R.raw.game_over,
                          1);
            level_complete
                = soundPool.load(
                    this,
                    R.raw.level_complete,
                    1);
            player_died
                = soundPool.load(
                    this,
                    R.raw.player_died,
                    1);
        }
      
        public void playSound(View v)
        {
            switch (v.getId()) {
      
            case R.id.button_sound1:
      
                // This play function
                // takes five parameter
                // leftVolume, rightVolume,
                // priority, loop and rate.
                soundPool.play(
                    game_over, 1, 1, 0, 0, 1);
                soundPool.autoPause();
                break;
      
            case R.id.button_sound2:
                soundPool.play(
                    player_died, 1, 1, 0, 0, 1);
                break;
      
            case R.id.button_sound3:
                soundPool.play(
                    level_complete, 1, 1, 0, 0, 1);
                break;
            }
        }
    }
    

    输出: