简体   繁体   English

JavaScript 中的录音问题 一些录音丢失

[英]Audio Record Problem in JavaScript some of the recording is missing

Hello Everyone I am trying to Record Audio and then after Recording I play it and when I hit the Cut function it just get the second I am on it in the player and cut to the end and start recording again and then overwrite the chunks array till the end with the new recording to sum up all of that all I need is when I record 5 seconds and then I want to overwrite the last 3 seconds so I remove the last 3 elements in the array and push the new chunks to the original array when I do that and send the new chunk to the audio player it plays the first time as intended and if I pressed play again it just plays the newly recorded part only without the first 2 seconds that I preserved from the old recording大家好,我正在尝试录制音频,然后在录制后播放它,当我点击 Cut function 时,它只是得到第二个我在播放器中的位置,然后切到最后并再次开始录制,然后覆盖块数组直到新记录的结尾总结了我需要的所有内容是当我记录 5 秒然后我想覆盖最后 3 秒所以我删除数组中的最后 3 个元素并将新块推送到原始数组当我这样做并将新块发送到音频播放器时,它会按预期第一次播放,如果我再次按下播放,它只会播放新录制的部分,而不会播放我从旧录音中保留的前 2 秒

let audioChunks = [];
let Recorder = null;
const recordAudio = () => {
    return new Promise(resolve => {
        navigator.mediaDevices.getUserMedia({ audio: true })
            .then(stream => {
                const mediaRecorder = new MediaRecorder(stream);
                mediaRecorder.addEventListener("dataavailable", event => {
                    audioChunks.push(event.data);
                });

                const start = () => {
                    mediaRecorder.start(1000);
                };

                const stop = () => {
                    return new Promise(resolve => {
                        mediaRecorder.addEventListener("stop", () => {
                            const audioBlob = new Blob(audioChunks,{type: 'audio/mpeg-3'});
                            const audioUrl = URL.createObjectURL(audioBlob);
                            const audio = new Audio(audioUrl);
                            document.getElementById("AudioRecodingPlayer").src = audio.src;
                            document.getElementById("AudioRecodingPlayer").currentTime = 0;
                            const play = () => {
                                audio.play();
                            };

                            resolve({ audioBlob, audioUrl, play });
                        });

                        mediaRecorder.stop();
                    });
                };

                resolve({ start, stop });
            });
    });
};

async function StartRecording(){
    Recorder = await recordAudio();
    Recorder.start();
}

async function StopRecording(){
    const audio = Recorder.stop();
}
function Cut(){
    var Audio = document.getElementById("AudioRecodingPlayer");
    var CurrenTime = Math.round(Audio.currentTime);
    audioChunks.length = CurrenTime;
    StartRecording();
}

On the Cut() function, you are not actually removing the elements of the array, nor changing the index or window.Cut() function 上,您实际上并没有删除数组的元素,也没有更改索引或 window。 You are only changing the value of length.您只是更改长度的值。

What you would have to do is overwrite the audio chunks, or remove the chunks from that point forward.您必须做的是覆盖音频块,或从该点向前删除块。

let audioChunks = [];
let Recorder = null;
const recordAudio = () => {
    return new Promise(resolve => {
        navigator.mediaDevices.getUserMedia({ audio: true })
            .then(stream => {
                const mediaRecorder = new MediaRecorder(stream);
                mediaRecorder.addEventListener("dataavailable", event => {
                    audioChunks.push(event.data);
                });

                const start = () => {
                    mediaRecorder.start(1000);
                };

                const stop = () => {
                    return new Promise(resolve => {
                        mediaRecorder.addEventListener("stop", () => {
                            const audioBlob = new Blob(audioChunks,{type: 'audio/mpeg-3'});
                            const audioUrl = URL.createObjectURL(audioBlob);
                            const audio = new Audio(audioUrl);
                            document.getElementById("AudioRecodingPlayer").src = audio.src;
                            document.getElementById("AudioRecodingPlayer").currentTime = 0;
                            const play = () => {
                                audio.play();
                            };

                            resolve({ audioBlob, audioUrl, play });
                        });

                        mediaRecorder.stop();
                    });
                };

                resolve({ start, stop });
            });
    });
};

async function StartRecording(){
    Recorder = await recordAudio();
    Recorder.start();
}

async function StopRecording(){
    const audio = Recorder.stop();
}

function Cut(){
    var Audio = document.getElementById("AudioRecodingPlayer");
    var CurrenTime = Math.round(Audio.currentTime);
    audioChunks.splice(CurrentTime); // this will actually remove the elements after the "CurrentTime" index
    StartRecording();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM