我正在尝试使用RecordRTC.js录制麦克风的音频并将其上传到nancyfx服务器。
出于测试目的,我只是尝试上传音频流并将其保存到wav文件中。但是,我的要求是将流保存为16位,频率为22050Hz。
问题是,当我使用标准配置(没有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;
}]);
关于可能出现什么问题的任何想法?
答案 0 :(得分:2)
了解您需要查看的问题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
文件的samplerate值(可以检查函数mergeLeftRightBuffers
进行确认),但数据仍然会记录在原始采样率上。
如果您真的想修改sampleRate,可以查看Record audio on web, preset: 16000Hz 16bit