In the development, we meet the need to play small audio , I thought of it for the first time soundpool But there were two holes , Now let's share the solution
1.soundpool After loading , But there was no sound
@Override public void speak(Object content) { SoundPool mSoundPool = new
SoundPool(5, AudioManager.STREAM_ALARM, 5); int soundID = mSoundPool.load(
mContext,(Integer) content, Integer.MAX_VALUE); mSoundPool.play(soundID, 0.6f,
0.6f, 1, 0, 1); } Last check api Found the problem , SoundPool Provides a callback to monitor the completion of loading resources public void
setOnLoadCompleteListener(OnLoadCompleteListener listener) Just play the sound in this callback :
@Override public void speak(Object content) { SoundPool mSoundPool= new
SoundPool(5, AudioManager.STREAM_ALARM, 5); final int soundID = mSoundPool.load(
mContext,(Integer) content, Integer.MAX_VALUE);
mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override public void onLoadComplete(SoundPool soundPool, int sampleId, int
status) { mSoundPool.play(soundID, 0.6f, 0.6f, 1, 0, 1); } }); }
2. many times stop/pause be unable to stop
The problem is that every time you play it soundPool.play(soundID, 0.6f, 0.6f, 1, 0, 1); The one in the callback later soundID It will change :
` public final int play(int soundID, float leftVolume, float rightVolume,int
priority, int loop, float rate)` Try redefining a variable playId
To store the return value , Make sure it's for the same one every time soundpool The problem is solved by pausing and playing playId= soundPool.play(ringId,
0.1f, 0.1f, 1, -1, 1); Pass this when you need to pause playId

Technology
©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions use Python Make simple games Laplance operator ( Second derivative ) Convert hard disk to GPT Partition format Python Implementation of Hanoi Tower code about String How to create objects vue3 Learning journey 1—— establish vue3 project java String from back to front _Java String String summary use Python Write a story about plants versus zombies 【 Greedy Algorithm 】 Huffman coding problem