Android开发之SoundPool简单使用工具类

这个工具类实测解决了Android高版本不能正常播放资源文件的问题!并且调用极其简单,一句代码即可,再搞不懂还有Demo可供下载!

全部代码

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

    //鎾斁
    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);
    }

}

调用代码

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

实例DEMO:https://download.csdn.net/download/qq_26914291/10280874
soundPool 一个最大问题就是有错误,也不会抛出错误,害我根本定不到问题在哪,网上这方面的文章也很少,经过千辛万苦终于发现问题的关键:Android系统版本的问题,21Api后对SoundPool进行了更改,导致Android 5.0以上系统根本不能播放音频!

你可能感兴趣的:(android开发)