如何在Chrome扩展中添加桌面捕获中的音频

时间:2017-06-04 11:55:29

标签: javascript google-chrome audio google-chrome-extension

我已经在桌面屏幕上添加了音频到Chrome扩展程序。但音频质量不高。因为,我记录了标签的屏幕。我得到了高质量的视频和音频。如何在桌面录制中获得高质量的音频。这是我正在使用的

 chrome.desktopCapture.chooseDesktopMedia(['screen', 'window', 'audio'], function (streamId) {
        if (streamId) {
              var obj = {
               audio: {
                mandatory: {
                        chromeMediaSource: 'desktop',
                        chromeMediaSourceId: streamId
                    }
               },
               video: {
                optional: [],
                   mandatory: {
                      chromeMediaSource: 'desktop',
                      chromeMediaSourceId: streamId,
                      maxWidth: 2560,
                      maxHeight: 1440
                   }
               }
            };

            countdownRun(function () {
                window.navigator.mediaDevices.getUserMedia(obj).then(function(stream) {
    $streamVideo = stream;
    $timeStart = Date.now();
    var audio = stream.getAudioTracks()[0];
    var video = stream.getVideoTracks()[0];
    alert(JSON.stringify(audio))
    alert(JSON.stringify(video))
    if (micSound) {
        window.navigator.webkitGetUserMedia({audio: true}, function (s) {
            $streamAudio = s;
            audio = s.getAudioTracks()[0];
            captureUseNacl(audio, video);
        }, function (e) {
            chrome.tabs.create({url: 'mic.html'});
        })
    } else {
        captureUseNacl(audio, video);

        (function () {
            var v = document.createElement('video');
            document.body.appendChild(v);
            v.setAttribute('autoplay', '');
            v.addEventListener('canplay', function () {
                console.log('play video');
            }, false);
            v.src = window.URL.createObjectURL(stream);
            $streamElement = v;
        })()
    }
}).catch(function(err) {
alert(err)
    alert(JSON.stringify(err));
});;
            })
        }
    });

1 个答案:

答案 0 :(得分:3)

您可以通过删除document.createElement和createObject部分来完成此操作。它只是为录音添加鼻子。如果您检查音频的就绪状态,它就会结束。它应该是活的。再次,将maxFrameRate添加到视频部分以同步音频和视频。这是你的代码

chrome.desktopCapture.chooseDesktopMedia(['screen', 'window', 'audio'], function (streamId) {
        if (streamId) {

              var obj = {
               audio: {
                mandatory: {
                        chromeMediaSource: 'desktop',
                        chromeMediaSourceId: streamId
                    }
               },
               video: {
                optional: [],
                   mandatory: {
                      chromeMediaSource: 'desktop',
                      chromeMediaSourceId: streamId,
                      maxWidth: 2560,
                      maxHeight: 1440,
                      maxFrameRate:30
                   }
               }
            };

            countdownRun(function () {
                navigator.mediaDevices.getUserMedia(obj).then(function(stream) {
    $streamVideo = stream;
    $timeStart = Date.now();
    var audio = stream.getAudioTracks()[0];
    var video = stream.getVideoTracks()[0];
    console.log(stream);
    if (micSound) {
        window.navigator.webkitGetUserMedia({audio: true}, function (s) {
            $streamAudio = s;
            audio = s.getAudioTracks()[0];
            captureUseNacl(audio, video);
        }, function (e) {
            chrome.tabs.create({url: 'mic.html'});
        })
    } else {
        captureUseNacl(audio, video);
    }
}).catch(function(err) {
 console.log(err)
});
            })
        }
    });