我正在尝试将Web Audio API实施到我的网站中。目前,我有一个功能,要求获得麦克风许可,然后创建一个对象。我想从另一个函数访问该对象。虽然,另一个函数不断声明未定义对象。下面是我正在使用的代码。
根据我的阅读,如果在函数外部引用对象,则不会删除该对象。
我几乎肯定这是一个范围问题,但就我的一生而言,我无法弄清楚。任何帮助表示赞赏。谢谢!
var stopButton = document.getElementById('stop_button');
var startButton = document.getElementById('start_button');
//Gets audio permission from browser & creates media stream if successful
function getPermission(){
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { //Checking for getUserMedia support
console.log('getUserMedia supported.');
navigator.mediaDevices.getUserMedia (
{
audio: true
})
// Success callback
.then(function(stream) { //Successful creation of a media stream
var mediaRecorder = new MediaRecorder(stream);
console.log("Stream & MediaRecorder created successfully");
return mediaRecorder;
})
// Error callback
.catch(function(err) {
console.log('The following getUserMedia error occured: ' + err);
}
);
} else {
console.log('getUserMedia not supported on your browser!');
}
}
//Creates new media recorder instance and passes it the stream directly.
//At this point, the stream is ready to be captured into a Blob (file-like object of immutable, raw data)
window.onload = getPermission();
startButton.onclick = function(){startRecording()};
stopButton.onclick = function(){stopRecording()};
function startRecording(){
mediaRecorder.start();
console.log(mediaRecorder.state);
console.log("Recording started");
}
function stopRecording(){
mediaRecorder.stop();
console.log("Recording stopped");
}
我尝试过的事情:
我已经阅读了至少10个其他SO问题,但标题相似,无济于事。它们是相似的,但不是重复的。