Reputation: 2695
I'm using react-native-sound to playback looped audio files (exactly mp3 format). The problem is that there is a gap between the loops. Here is my code example:
loadSound = (s) => {
let sound = new Sound(s, Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log('failed to load the sound', error);
return;
} else {
sound.setNumberOfLoops(-1);
}
});
return sound;
}
let sound = loadSound('campfire.mp3')
sound.play();
Is there any workaround how to make the loops to sound smooth?
Actually, this issue is open on github, but there is no solution yet...
Upvotes: 3
Views: 3086
Reputation: 382
Using the below version of the react-native-sounds fixed the gap issue.
https://github.com/Menardi/react-native-sound-gapless.git
From the README of the git repo:
Simply setting a track to loop on Android does not make it gapless, instead leaving a short silence before restarting the track. However, Android does actually support gapless playback between different tracks. So, this fork essentially loads the same track twice, and gets Android to handle the gapless playback for us. The downside is that you will use more memory than before, because the track is loaded twice. For most cases, this shouldn't matter, but you can profile your app in Android Studio if you are concerned.
Upvotes: 0
Reputation: 1
I have same issue with looping the sound. What I did to make it work is to call the same play sound function once sound play completed. I hope this will helps you.
function playBackground(){
bgSound = new Sound(bgMusic, (error, sound) => {
if (error) {
alert('error' + error.message);
return;
}
bgSound.play((success) => {
if(success)
{
// console.log("Play completed");
playBackground();
}
bgSound.release();
});
});
}
Upvotes: -1
Reputation: 2695
There are gaps between loops because of the bug in Android MediaPlayer (which is used under the hood). To fix the problem I've created the react-native-audio-exoplayer module, which has pretty same functionality as the react-native-sound, but it is based on the Android ExoPlayer. The API is a bit different but more robust and modern (have a look at the docs). Later if I have time I'll do a pull request to introduce ExoPlayer in the react-native-sound. But for now feel free to use react-native-audio-exoplayer as a good workaround (for now it's implemented only for Android).
Upvotes: 4