javascript - RecordRTC 具有自定义采样率记录静音

标签 javascript angularjs webrtc getusermedia

我正在尝试使用 RecordRTC.js 从麦克风录制音频并将其上传到 nancyfx 服务器。

出于测试目的,我只是尝试上传音频流并将其保存到 wav 文件。然而,我的要求是流以 22050Hz 的 16 位保存。

问题是,当我使用标准配置(没有 recordRtcOptions)录制文件时,我可以上传文件并保存它。当我在 recordrtc 中指定采样率时,输出文件只是静音。

代码的相关部分位于 angularJS 服务内部,如下所示:

app.service('AudioService', ['$window', '$http', function($window, $http) {

    var recordRtcOptions = {
        'sample-rate' : 22050
    };

    navigator.getUserMedia = (
        $window.navigator.getUserMedia ||
        $window.navigator.webkitGetUserMedia ||
        $window.navigator.mozGetUserMedia ||
        $window.navigator.msGetUserMedia)

    var _recordRTC = {};
    navigator.getUserMedia({ audio: true, video: false }, function (stream) {
        console.log('starting to initialize getUserMedia');
        console.log(recordRtcOptions);

        _recordRTC = RecordRTC(stream, recordRtcOptions);    

        console.log('Finished initializing getUserMedia');
    }, function (error) {
        console.log('Error initializing media stream: ' + error);  
    });

    var instance = {};

    instance.startRecording = function() {
        console.log('starting to record...');
        console.log('sample rate: ' + _recordRTC.sampleRate);
        _recordRTC.startRecording();
    };

    instance.stopRecording = function(uploadPath) {

        console.log('sample rate: ' + _recordRTC.sampleRate);


        _recordRTC.stopRecording(function(audioVideoMURL) {
            console.log('stopped recording...');
            console.log('recordrtc stop sample rate: ' + _recordRTC.sampleRate);

            $http({
                method : 'POST',
                url : uploadPath,
                data : _recordRTC.getBlob()
            }).success(function(data) {
                console.log('POST /audio Success');

            }).error(function() {
                console.log('POST /audio error'); 
            });
        });

    };

    return instance;

}]);

知道可能出现什么问题吗?

最佳答案

要了解问题,您需要查看 RecordRTC.js:

  • 首先,函数 mergeProps 复制您提供的配置。
  • 函数reformatProps将“sample-rate”转换为属性“sampleRate”。
  • StereoRecorder 用于录音,其内部使用 StereoAudioRecorder ,本质上与 mattdiamond/Recorderjs 类似。 .

当您研究它时,它不会将 sampleRate 作为输入,而是根据这些行确定它

var Recorder = function(source, cfg){
    ...
    this.context = source.context;
    ...
        sampleRate: this.context.sampleRate, // --> this is the right way to provide sampleRate

在 RecordRTC 中,

var sampleRate = typeof config.sampleRate !== 'undefined' ? config.sampleRate : context.sampleRate || 44100;

长话短说,如果您注意到,这里当未提供采样率时,它需要 context.sampleRate 这是麦克风提供的采样率,所以只需更改配置中的采样率即可还不够,因为所做的只是更改写入 .wav 文件的采样率值(可以检查函数 mergeLeftRightBuffers 进行确认),但数据将具有仍然以原始采样率记录。

如果你确实想修改sampleRate,可以查看Record audio on web, preset: 16000Hz 16bit

关于javascript - RecordRTC 具有自定义采样率记录静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900450/

相关文章:

JavaScript 在 Div 标签上动态追加另一个 Div 数据

javascript - View 中 View 中的主干 View - 为什么 this.$el.html ('insert some content here' ) 什么都不做?

html - 如果某些行值在 Angularjs 中不可用,我如何求和列值的总和?

linux - 有没有适用于 Linux 的 WebRTC 库?

webrtc - ARM 处理器上是否有任何浏览器支持 WebRTC?

javascript - clientWidth 检测关闭 15px,可能是由于滚动条

javascript - 申请后的 Facebook 链接

javascript - 将 Firebase 注入(inject) AngularFire 时找不到 $firebaseObject

angularjs - 在 AngularJS 中设置 window.location 或 window.open 在 IE 11 中给出 "access is denied"

google-chrome-extension - 如何使用 webrtc insde google chrome 扩展?