This article is mainly about volume control and short audio loading control. Play short audio through MediaPlayer or SoundPool
I use the dynamic initialization method to load. That is to say, the audio you put in will be loaded all the time. It will not be loaded once and then destroyed
First, let's talk about the encapsulation and usage of SoundPool. The volume control is directly in AudioUtil
Note: this class is unable to monitor playback completion callback
private AudioUtil.SoundPoolManager.PlayManager mPlayManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialization first 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); }
Next is the encapsulation and short video processing of MediaPlayer. AudioUtil.getInstance(MainActivity.this).getMediaMusicManager() first obtains the media management. In the process of playing, MediaPlayer.create(sContext, re) automatically calls the prepare method, so it is unnecessary to call it
private AudioUtil.MediaMusicManager.PlayManager mPlayManager1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialization first 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("Play completed"); } }); } 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); }
Use source code attached
public class AudioUtil { private static Context sContext; private AudioManager mAudioManager; private static AudioUtil mInstance; private SoundPoolManager mSoundPoolManager; /** * Default load size */ 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 type 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) { //speaker mAudioManager.setSpeakerphoneOn(true); mAudioManager.setMode(AudioManager.MODE_NORMAL); } else { // Set maximum volume int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL); mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, max, AudioManager.STREAM_VOICE_CALL); // Set to handset mode mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); mAudioManager.setSpeakerphoneOn(false);// Turn off speakers mAudioManager.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); } } class SoundPoolManager { private SoundPool mSoundPool; @SuppressLint("UseSparseArrays") private Map<Integer, Integer> resTask = new HashMap(); private PlayManager mPlayManager; SoundPoolManager() { initSoundPool(); } // Synchronization code guarantee initialization 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) { // Size dynamically 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<Integer, Integer> entry : resTask.entrySet()) { Integer key = entry.getKey(); resTask.put(key, mSoundPool.load(sContext, key, 1)); } } class PlayManager { private Map<Integer, Integer> mResTask; PlayManager(Map<Integer, Integer> resTask) { this.mResTask = resTask; } private void setResTask(Map<Integer, Integer> 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("Need first loadResArray"); } mSoundPool.play(integer, 1, 1, 0, 0, 1); } } } class MediaMusicManager { private PlayManager mPlayManager; private Map<Integer, MediaPlayer> 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<Integer, MediaPlayer> mResTask; private onCompleteListener listener; PlayManager(Map<Integer, MediaPlayer> resTask) { this.mResTask = resTask; } private void setResTask(Map<Integer, MediaPlayer> 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("Need first loadResArray Or it has been recycled"); } mediaPlayer.setOnCompletionListener(this); mediaPlayer.start(); } public void release(@RawRes int res) { //release MediaPlayer mediaPlayer = mResTask.get(res); if (mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; mResTask.remove(res); } } public void release(MediaPlayer mp) { for (Map.Entry<Integer, MediaPlayer> entry : mResTask.entrySet()) { if (entry.getValue() == mp) { Integer key = entry.getKey(); release(key); return; } } } @Override public void onCompletion(MediaPlayer mp) { if (listener != null) { // It's better to recycle when it's finished listener.onCompletion(mp); } } public void setListener(onCompleteListener listener) { this.listener = listener; } } } public interface onCompleteListener { void onCompletion(MediaPlayer mp); } }