Full record of SoundPool problems in Android Development

Posted by abasel on Tue, 31 Mar 2020 23:41:50 +0200

This tool class actually solves the problem that the Android higher version cannot play the resource file normally! And call extremely simple, a code can, do not understand there is Demo to download!

All code

import android.media.*;
import android.content.*;
import android.os.*;
import android.app.*;
import android.widget.*;

public class SoundPoolHelper
{
    private SoundPool mainSoundPool;
    private AudioManager mainAudioManager;
    private float volume;
    // Maximumn sound stream.
    private static final int MAX_STREAMS = 5;
    // Stream type.
    private static final int streamType = AudioManager.STREAM_MUSIC;
    private int soundId;
    private int resId;
    private Context mainContext;
    public SoundPoolHelper(Context context){
        this.mainContext=context;
    }

    //To be careful
    public void playSoundWithRedId(int resId){
        this.resId=resId;
        init();
    }

    //init settings
    private void init(){
        // AudioManager audio settings for adjusting the volume
        mainAudioManager = (AudioManager)this.mainContext. getSystemService(Context.AUDIO_SERVICE);

        // Current volumn Index of particular stream type.
        float currentVolumeIndex = (float) mainAudioManager.getStreamVolume(streamType);

        // Get the maximum volume index for a particular stream type.
        float maxVolumeIndex  = (float) mainAudioManager.getStreamMaxVolume(streamType);

        // Volumn (0 --> 1)
        this.volume = currentVolumeIndex / maxVolumeIndex;

        // Suggests an audio stream whose volume should be changed by
        // the hardware volume controls.
        ((Activity)this.mainContext).setVolumeControlStream(streamType);

        // For Android SDK >= 21
        if (Build.VERSION.SDK_INT >= 21 ) {

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

            SoundPool.Builder builder= new SoundPool.Builder();
            builder.setAudioAttributes(audioAttrib).setMaxStreams(MAX_STREAMS);

            this.mainSoundPool = builder.build();
        }
        // for Android SDK < 21
        else {
            // SoundPool(int maxStreams, int streamType, int srcQuality)
            this.mainSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
        }

        // When Sound Pool load complete.
        this.mainSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                        playSound();
                }
            });

        //load res
        this.soundId=this.mainSoundPool.load(this.mainContext,this.resId,1);
    }

    //play the sound res
    private void playSound(){
            float leftVolumn = volume;
            float rightVolumn = volume;
            // Play sound of gunfire. Returns the ID of the new stream.
            int streamId = this.mainSoundPool.play(this.soundId,leftVolumn, rightVolumn, 1, 0, 1f);
    }

}

Calling code

new SoundPoolHelper(this).playSoundWithRedId(R.raw.gun);

Example DEMO: https://download.csdn.net/download/qq_26914291/10280874
One of the biggest problems of SoundPool is that there is an error, and it will not throw out an error, which makes me not sure where the problem is. There are few articles on this aspect on the Internet. After a lot of hard work, I finally found the key to the problem: the problem of Android system version, the SoundPool was changed after 21Api, resulting in that the system above Android 5.0 can not play audio at all!

Topics: Android SDK