I 将 php 中的 Base64 字符串转换为 mp3 声音文件,但生成的文件 wav 已损坏
转换音调之前声音沙沙作响
如转换的语音文件:
http://vocaroo.com/i/s1Dlol5Wu3Zo
PHP代码 :
<?php
if(isset($_POST['audio'])){
$data = str_replace('data:audio/wav;base64,', '', $_POST['audio']);
$data = base64_decode($data);
$track_name = get_random_string(mt_rand(6,10)) . ".mp3";
$upl_dir = "../up/" . $track_name;
if( !file_put_contents($upl_dir, $data) ){
$response['status'] = 1;
$response['message'] = "File could not be uploaded. Try again >later.";
echo $data;
echo "E1";
}
echo $data;
}
我在转换之前测试声音是对的。
Base64 代码 :
https://ufile.io/174101
JS代码库64创建者:
function base641()
{
Fr.voice.export(function(url){
// console.log("Here is the base64 URL : " + url);
base = url;
ejem2 = 1;
// alert("Check the web console for the URL");
// $("<a href='"+ url +"' target='_blank'></a>")[0].click();
}, "base64");
};
试试这个:
file_put_contents('audio.mp3', base64_decode($data));