Everything is working fine in simulator and andriod except ios build i am new to react native sound, i have used the Sound.setCategory and Sound.setMode also but no effect, countsound is being played but the sound2 is not. Code:
const startCountdownWithAudio = () => {
if (!countSound || !countSound.isLoaded()) {
console.error("Sound not loaded");
return;
}
Sound.setCategory('PlayAndRecord', true);
Sound.setActive(true);
Sound.setMode('VideoRecording', true);
countSound.setVolume(1.0);
countSound.setCurrentTime(0);
let audioStarted = false;
let countdownTimer;
setCount("Initializing..."); // Show "Initializing..." before audio starts
countSound.play((success) => {
console.log("IS countdown success", success);
if (success) {
setCount(-1);
startRecording(); // Ensure recording starts right after countdown finishes
}
});
const checkStart = setInterval(() => {
if (!audioStarted && countSound.isPlaying()) {
audioStarted = true;
console.log("Audio started at:", new Date().toISOString());
let currentCount = 3;
setCount(currentCount);
countdownTimer = setInterval(() => {
if (currentCount > 0) {
setCount(--currentCount);
} else {
clearInterval(countdownTimer);
}
}, highlightInterval.current);
clearInterval(checkStart);
}
}, 100);
};
const startRecording = async () => {
running.current = true;
try {
AudioRecord.start();
setIsRecording(true);
sound1.setVolume(0);
sound2.setVolume(1);
setShouldPlayMetronome(true);
// Ensure sound2 is loaded before trying to play
if (!sound2 || !sound2.isLoaded()) {
console.error("sound2 is not loaded yet");
return;
}
console.log("PLAYING METRONOME");
sound2.setCurrentTime(0);
const playSound2 = new Promise((resolve, reject) => {
sound2.play((success) => {
if (success) {
console.log("Sound2 started successfully");
resolve();
} else {
console.error("Metronome playback failed");
reject(new Error("Metronome playback failed"));
}
});
});
const highlightPromise = new Promise((resolve) => {
console.log("Highlighting started in metronome only");
highlight();
resolve();
});
await Promise.all([playSound2, highlightPromise]);
console.log("Metronome and highlight started in parallel");
} catch (error) {
console.error('Recording error:', error);
Alert.alert('Error', 'Failed to start recording. Please try again.');
setIsRecording(false);
running.current = false;
}
};
I was trying to start countdown in sync with the setcount, then as soon as the countdown completes the next sound2 should start to play with recording the user input. The highlight() fun should start in parallel with the sound2, the highlight() fun is being called properly but the audio of sound2 is not playing.