Unable to play second sound with audio recording while using react-native-sound in ios - Stack Overflow

Everything is working fine in simulator and andriod except ios build i am new to react native sound, i

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745044040a4607986.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信