Play sound using soundpool example

后端 未结 5 1475
旧时难觅i
旧时难觅i 2020-11-27 14:51

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.

5条回答
  •  青春惊慌失措
    2020-11-27 15:15

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

提交回复
热议问题