Read the original article:SoundPool does not play the audio.
Problem Description
SoundPool does not play the audio.
Background Knowledge
SoundPool provides functionalities such as loading, playing, setting volume, looping, stopping playback, and unloading resources for short audio files. The SoundPool.unload method is used to unload audio that has already been loaded.
Problem Identification
Search for the keyword "SoundPool::Play" in the hilog logs to identify the audio ID played at the time the issue occurred. Record the audio ID and the playback time.
04-29 18:02:32.140 SoundPool::Play soundID::74,priority:0
Then search for the keyword "SoundPool::Unload soundID: audio ID" in the log to check the time when the audio was unloaded.
04-29 18:02:30.235 SoundPool::Unload soundID::74
Analysis Conclusion
If the audio unload time is earlier than the audio playback time, it indicates that the application unloaded the audio before the playback was completed, resulting in the audio not being played.
Suggestion for Modification
Listen to the 'playFinished' event of SoundPool, and unload the audio in the callback function.
import { BusinessError } from '@kit.BasicServicesKit';
// Create a soundPool instance.
let soundPool: media.SoundPool;
let audioRendererInfo: audio.AudioRendererInfo = {
usage: audio.StreamUsage.STREAM_USAGE_MUSIC,
rendererFlags: 1
}
media.createSoundPool(5, audioRendererInfo, (error: BusinessError, soundPool_: media.SoundPool) => {
if (error) {
console.error(`Failed to createSoundPool`);
return;
} else {
soundPool = soundPool_;
console.info(`Succeeded in createSoundPool`);
soundPool.on('playFinished', () => {
console.info('Succeeded in playFinished');
// Execute the audio unload method here.
});
}
});
Top comments (0)