在互联网上的大量资源之后,我正在尝试建立一个简单的网页,我可以在那里录制一些内容(我的声音),然后从录制中制作一个mp3文件,最后将该文件上传到服务器。
此时我可以录制并播放,但我还没有上传,似乎我甚至无法在本地制作MP3文件。有人能告诉我我做错了什么,或者错误的顺序?
下面是我此时的所有代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
<h2>Audio record and playback</h2>
<p>
<button id=startRecord><h3>Start</h3></button>
<button id=stopRecord disabled><h3>Stop</h3></button>
<audio id="player" controls></audio>
<a id=audioDownload></a>
</p>
</div>
<script>
var player = document.getElementById('player');
var handleSuccess = function(stream) {
rec = new MediaRecorder(stream);
rec.ondataavailable = e => {
audioChunks.push(e.data);
if (rec.state == "inactive") {
let blob = new Blob(audioChunks,{type:'audio/x-mpeg-3'});
player.src = URL.createObjectURL(blob);
player.controls=true;
player.autoplay=true;
// audioDownload.href = player.src;
// audioDownload.download = 'sound.data';
// audioDownload.innerHTML = 'Download';
mp3Build();
}
}
player.src = stream;
};
navigator.mediaDevices.getUserMedia({audio:true/*, video: false */})
.then(handleSuccess);
startRecord.onclick = e => {
startRecord.disabled = true;
stopRecord.disabled=false;
audioChunks = [];
rec.start();
}
stopRecord.onclick = e => {
startRecord.disabled = false;
stopRecord.disabled=true;
rec.stop();
}
var ffmpeg = require('ffmpeg');
function mp3Build() {
try {
var process = new ffmpeg('sound.data');
process.then(function (audio) {
// Callback mode.
audio.fnExtractSoundToMP3('sound.mp3', function (error, file) {
if (!error) {
console.log('Audio file: ' + file);
audioDownload.href = player.src;
audioDownload.download = 'sound.mp3';
audioDownload.innerHTML = 'Download';
} else {
console.log('Error-fnExtractSoundToMP3: ' + error);
}
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
}
</script>
</body>
</html>
当我尝试使用Web控制台内的调试器进行调查并查看发生的情况时;在线上:
var process = new ffmpeg('sound.data');
我收到这条消息:
Paused on exception
TypeError ffmpeg is not a contructor.
在线:
var ffmpeg = require('ffmpeg');
我收到这条消息:
Paused on exception
ReferenceError require is not defined.
当我看到表达ffmpeg时,我可以看到:
ffmpeg: undefined
经过一些进一步的调查,并使用browserify我使用以下代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
<h2>Audio record and playback</h2>
<p>
<button id=startRecord><h3>Start</h3></button>
<button id=stopRecord disabled><h3>Stop</h3></button>
<audio id="player" controls></audio>
<a id=audioDownload></a>
</p>
</div>
<script src="bundle.js"></script>
<script>
var player = document.getElementById('player');
var handleSuccess = function(stream) {
rec = new MediaRecorder(stream);
rec.ondataavailable = e => {
if (rec.state == "inactive") {
let blob = new Blob(audioChunks,{type:'audio/x-mpeg-3'});
//player.src = URL.createObjectURL(blob);
//player.srcObject = URL.createObjectURL(blob);
//player.srcObject = blob;
player.srcObject = stream;
player.controls=true;
player.autoplay=true;
// audioDownload.href = player.src;
// audioDownload.download = 'sound.data';
// audioDownload.innerHTML = 'Download';
mp3Build();
}
}
//player.src = stream;
player.srcObject = stream;
};
navigator.mediaDevices.getUserMedia({audio:true/*, video: false */})
.then(handleSuccess);
startRecord.onclick = e => {
startRecord.disabled = true;
stopRecord.disabled=false;
audioChunks = [];
rec.start();
}
stopRecord.onclick = e => {
startRecord.disabled = false;
stopRecord.disabled=true;
rec.stop();
}
var ffmpeg = require('ffmpeg');
function mp3Build() {
try {
var process = new ffmpeg('sound.data');
process.then(function (audio) {
// Callback mode.
audio.fnExtractSoundToMP3('sound.mp3', function (error, file) {
if (!error) {
console.log('Audio file: ' + file);
//audioDownload.href = player.src;
audioDownload.href = player.srcObject;
audioDownload.download = 'sound.mp3';
audioDownload.innerHTML = 'Download';
} else {
console.log('Error-fnExtractSoundToMP3: ' + error);
}
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
}
</script>
</body>
</html>
这解决了以下问题:
the expression ffmpeg being: undefined
但是回放不再起作用了。我可能没有使用player.srcObject做正确的事情,也许还有其他一些事情。
当我使用这一行时:
player.srcObject = URL.createObjectURL(blob);
我收到这条消息:
Paused on exception
TypeError: Value being assigned to HTMLMediaElement.srcObject is not an object.
当我使用这一行时:
player.srcObject = blob;
我收到这条消息:
Paused on exception
TypeError: Value being assigned to HTMLMediaElement.srcObject does not implement interface MediaStream.
最后,如果我使用这个:
player.srcObject = stream;
我没有收到任何错误消息,但录音仍然无法正常工作。
qazxsw poi在浏览器中不起作用。
你应该使用“browserify”这是它的github页面require
还有更好的(如果没有必要)在新的Blob中使用浏览器MediaRecorder支持的音频类型检查此链接https://github.com/browserify/browserify
最后,你的代码中不需要https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/isTypeSupported%22(我说的是第二个)加上它会给你一个错误检查这个player.src = stream;
这是一个小编辑
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject#Basic_example