我正在使用 HTML5 构建一个简单的录制应用程序,我正在尝试将文件拆分为更小的位并将每个位保存到 Web 服务器以模拟实时流媒体?我已经获得了记录和拆分的代码,但由于某种原因,只有第一个切片会记录。其余的有数据但不会播放(至少不在 vlc 中)
这是代码:
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
var audio = document.querySelector('audio');
var context = new webkitAudioContext();
var recorder;
var lastByte = 0;
var intv;
var fileCount = 1;
var onFail = function(e) {
document.getElementById("results").innerHTML = "An error Occured<br />" + e;
};
var onSuccess = function(s) {
var mediaStreamSource = context.createMediaStreamSource(s);
recorder = new Recorder(mediaStreamSource);
recorder.record();
intv = setInterval(function(){getSnippet();} , 3000);
}
function startRecording() {
if (navigator.getUserMedia) {
document.getElementById("results").innerHTML = "Starting....";
navigator.getUserMedia({audio: true}, onSuccess, onFail);
}
else {
document.getElementById("results").innerHTML = "Get User Media not Present/Active";
}
}
function stopRecording() {
recorder.stop();
clearInterval(intv);
recorder.exportWAV(function(s) { console.log(s);
saveSnippet(s, "Final");
audio.src = window.URL.createObjectURL(s);
});
}
function getSnippet(){
recorder.exportWAV(function(s) {
//Slice the unsaved part of the file
slc = s.slice(lastByte, s.size, s.type );
console.log("Start: " + lastByte + "; End: " + s.size + "; FileName: " + fileCount);
saveSnippet(slc, fileCount);
lastByte = s.size;
fileCount++;
});
//recorder.exportWAV(function(s) { saveFile(s); });
}
function saveSnippet(s, fname){
var fd = new FormData();
fd.append('fname', fname + '.wav');
fd.append('fileToUpload', s);
$.ajax({
url: 'savefile.php',
type: 'POST',
data: fd,
processData: false,
contentType: false,
success: function(data){ document.getElementById("results").innerHTML = "File Saved: <br />" + data;},
error: function(jqXHR, textStatus, errorMessage) { document.getElementById("results").innerHTML = errorMessage; }
});
}
我做错了什么,为什么第一个文件记录但没有后续文件?我是一个完整的新手,所以请温柔。如果有更好的方法可以做到这一点,我会全力以赴!
根据要求,这里是 savefile.php 的代码
if(count($_FILES) > 0 ){
echo "Data Found<br />";
echo $_FILES['fileToUpload']['tmp_name'];
$fl = file_get_contents($_FILES['fileToUpload']['tmp_name']);
echo "<br />File Uploaded: " . file_put_contents("uploads/" . $_POST['fname'], $fl, FILE_APPEND);
}