为什么上传的音频在上传明显成功时损坏?
我已经使用JavaScript,PHP和Cordova进行了上传。一切都很好。但是当我尝试在浏览器或Windows Media Player等桌面播放器中打开上传的mp3时,它说文件已损坏。任何想法为什么会发生这种情况?
我还必须说,当我在浏览器中检查损坏的文件时,它具有视频标签而不是音频标签。
我的代码:
//method to upload the audio
function uploadAudio(recordedSrc) {
var win = function(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
var fail = function(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = "recordupload.mp3";
options.mimeType = "audio/mpeg";
console.log(options);
var ft = new FileTransfer();
console.log(ft);
console.log(recordedSrc);
ft.upload(recordedSrc, encodeURI(app_url + "json/upload.php"), win, fail, options);
}
$('.upload').on('click', function(e) {
e.preventDefault();
//Method to upload Audio file to server
uploadAudio(mediaRecSrc);
});
PHP 中的服务器端处理脚本:
<?php
// Where the file is going to be placed
$target_path = dirname(__FILE__) . "/uploaded_records/";
if (!file_exists($target_path)) {
mkdir ($target_path, 0777);
}
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['file']['name']);
$path = $_FILES['file']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
var_dump("ext is: " . $ext);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename( $_FILES['file']['name']);
echo "target_path: " .$target_path;
}
?>
更新:
似乎问题出在文件(在Android上可以播放)。我通过USB设备复制了文件并尝试播放它,但存在相同的问题,该文件无法播放。我不得不说,该文件是使用cordova的媒体插件录制的。也许这就是问题所在,对吧?
第二次更新:
我录制并上传了一个文件作为格式,并将其转换为在线这里,声音工作。关于如何解决这个问题的任何想法?.amr
.mp3