Android-工作遭遇-音量音频播放控制篇(1)

这一篇是主要是讲述音量控制以及短音频加载控制.通过MediaPlayer或者SoundPool进行短音频的播放.

我这边采用了动态初始化的方式进行加载,也就是说,你放进去的音频会一直加载进去.并不会加载一次之后,便被销毁

首先讲下SoundPool的封装使用方法.音量控制直接在AudioUtil中

注:此类是不能监听播放完成回调.

   private AudioUtil.SoundPoolManager.PlayManager mPlayManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 先初始化
        mPlayManager = AudioUtil.getInstance(MainActivity.this).getSoundPoolManager().loadResArray(
                R.raw.prompt,
                R.raw.prompt2
        );

        mPlayManager = AudioUtil.getInstance(MainActivity.this).getSoundPoolManager().loadResArray(
                R.raw.prompt3
        );
    }


    public void btn1(View view) {
        mPlayManager.play(R.raw.prompt);

    }

    public void btn2(View view) {
        mPlayManager.play(R.raw.prompt2);
    }

    public void btn3(View view) {
        mPlayManager.play(R.raw.prompt3);
    }

接下来是MediaPlayer的封装播放短视频处理,AudioUtil.getInstance(MainActivity.this).getMediaMusicManager()首先获取media管理,在进行播放,MediaPlayer.create(sContext, re)当中是自动会调用prepare方法,所以不用调用了


    private AudioUtil.MediaMusicManager.PlayManager mPlayManager1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 先初始化
        mPlayManager1 = AudioUtil.getInstance(MainActivity.this).getMediaMusicManager().loadResArray(R.raw.prompt, R.raw.prompt2);
        mPlayManager1 = AudioUtil.getInstance(MainActivity.this).getMediaMusicManager().loadResArray(R.raw.prompt3);
        mPlayManager1.setListener(new AudioUtil.onCompleteListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                System.out.println("播放完成");
            }
        });
    }


    public void btn1(View view) {
        mPlayManager1.play(R.raw.prompt);
    }

    public void btn2(View view) {

        mPlayManager1.play(R.raw.prompt2);
    }

    public void btn3(View view) {

        mPlayManager1.play(R.raw.prompt3);
    }

 

 

附上使用源码


public class AudioUtil {
    private static Context      sContext;
    private        AudioManager mAudioManager;
    private static AudioUtil    mInstance;

    private SoundPoolManager mSoundPoolManager;
    /**
     * 默认加载大小
     */
    private int mMaxStreams = 1;
    private MediaMusicManager mMediaMusicManager;

    private AudioUtil(Context context) {
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }

    public static AudioUtil getInstance(Context context) {
        sContext = context.getApplicationContext();
        if (mInstance == null) {
            synchronized (AudioUtil.class) {
                if (mInstance == null) {
                    mInstance = new AudioUtil(context);
                }
            }
        }
        return mInstance;
    }


    public SoundPoolManager getSoundPoolManager() {
        if (mSoundPoolManager == null) {
            mSoundPoolManager = new SoundPoolManager();
        }
        return mSoundPoolManager;
    }

    public MediaMusicManager getMediaMusicManager() {
        if (mMediaMusicManager == null) {
            mMediaMusicManager = new MediaMusicManager();
        }
        return mMediaMusicManager;
    }


    public int getMediaMaxVolume() {
        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    }

    public int getCurrentMediaVolume() {
        return mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    public int getCallMaxVolume() {
        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
    }

    public int getSystemMaxVolume() {

        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
    }

    public int getSystemVolume() {

        return mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
    }

    public int getAlermMaxVolume() {
        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
    }


    public void setMediaVolume(int volume) {
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, //音量类型
                volume,
                AudioManager.FLAG_PLAY_SOUND);
    }

    public void setCallVolume(int volume) {
        mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                volume,
                AudioManager.STREAM_VOICE_CALL);
    }

    public void setSpeakerStatus(boolean on) {
        if (on) { //扬声器
            mAudioManager.setSpeakerphoneOn(true);
            mAudioManager.setMode(AudioManager.MODE_NORMAL);
        } else {
            // 设置最大音量
            int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
            mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, max, AudioManager.STREAM_VOICE_CALL);
            // 设置成听筒模式
            mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            mAudioManager.setSpeakerphoneOn(false);// 关闭扬声器
            mAudioManager.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
        }
    }


    class SoundPoolManager {
        private SoundPool mSoundPool;
        @SuppressLint("UseSparseArrays")
        private Map resTask = new HashMap();
        private PlayManager mPlayManager;


        SoundPoolManager() {
            initSoundPool();
        }

        // 同步代码保证初始化
        private void initSoundPool() {
            if (mSoundPool == null) {
                synchronized (SoundPoolManager.class) {
                    if (mSoundPool == null) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            mSoundPool = new SoundPool.Builder()
                                    .setMaxStreams(mMaxStreams)
                                    .build();
                        } else {
                            mSoundPool = new SoundPool(mMaxStreams, AudioManager.STREAM_MUSIC, 0);
                        }

                    }
                }
            }
        }

        public PlayManager loadResArray(@RawRes int... res) {
            // 动态设置大小
            if (resTask.size() + res.length + 1 > mMaxStreams) {
                mSoundPool.release();
                mSoundPool = null;
                mMaxStreams = resTask.size() + res.length + 1;
                initSoundPool();
                SoundPoolLoad();
            }
            for (int re : res) {
                resTask.put(re, mSoundPool.load(sContext, re, 1));
            }
            if (mPlayManager != null) {
                mPlayManager.setResTask(resTask);
                return mPlayManager;
            }
            mPlayManager = new PlayManager(resTask);
            return mPlayManager;
        }

        private synchronized void SoundPoolLoad() {
            for (Map.Entry entry : resTask.entrySet()) {
                Integer key = entry.getKey();
                resTask.put(key, mSoundPool.load(sContext, key, 1));
            }
        }

        class PlayManager {
            private Map mResTask;

            PlayManager(Map resTask) {
                this.mResTask = resTask;
            }

            private void setResTask(Map resTask) {
                mResTask = resTask;
            }

            public void play(@RawRes int res) {
                System.out.println(mMaxStreams + "");
                if (getCurrentMediaVolume() < (getMediaMaxVolume() / 2)) {
                    setMediaVolume(getMediaMaxVolume());
                }
                Integer integer = mResTask.get(res);
                if (integer == null) {
                    throw new RuntimeException("需要先loadResArray");
                }
                mSoundPool.play(integer, 1, 1, 0, 0, 1);

            }
        }
    }


    class MediaMusicManager {

        private PlayManager mPlayManager;
        private Map resTask = new HashMap();

        MediaMusicManager() {
        }


        public PlayManager loadResArray(@RawRes int... res) {
            for (int re : res) {
                resTask.put(re, MediaPlayer.create(sContext, re));
            }
            if (mPlayManager != null) {
                mPlayManager.setResTask(resTask);
                return mPlayManager;
            }
            mPlayManager = new PlayManager(resTask);
            return mPlayManager;
        }

        class PlayManager implements MediaPlayer.OnCompletionListener {

            private Map mResTask;
            private onCompleteListener        listener;

            PlayManager(Map resTask) {
                this.mResTask = resTask;
            }

            private void setResTask(Map resTask) {
                mResTask = resTask;
            }

            public void play(@RawRes int res) {
                if (getCurrentMediaVolume() < (getMediaMaxVolume() / 2)) {
                    setMediaVolume(getMediaMaxVolume());
                }
                MediaPlayer mediaPlayer = mResTask.get(res);
                if (mediaPlayer == null) {
                    throw new RuntimeException("需要先loadResArray或者已经被回收");
                }
                mediaPlayer.setOnCompletionListener(this);
                mediaPlayer.start();
            }

            public void release(@RawRes int res) {
                //释放
                MediaPlayer mediaPlayer = mResTask.get(res);
                if (mediaPlayer != null) {
                    mediaPlayer.release();
                    mediaPlayer = null;
                    mResTask.remove(res);
                }
            }

            public void release(MediaPlayer mp) {
                for (Map.Entry entry : mResTask.entrySet()) {
                    if (entry.getValue() == mp) {
                        Integer key = entry.getKey();
                        release(key);
                        return;
                    }
                }
            }

            @Override
            public void onCompletion(MediaPlayer mp) {
                if (listener != null) {
                    // 最好在完成的时候进行回收
                    listener.onCompletion(mp);
                }

            }

            public void setListener(onCompleteListener listener) {
                this.listener = listener;
            }
        }
    }

    public interface onCompleteListener {
        void onCompletion(MediaPlayer mp);
    }
}

 

你可能感兴趣的:(Android-工作遭遇-音量音频播放控制篇(1))