如何在php中解码base64 audiodata字符串后获得清晰的语音

时间:2017-08-25 07:28:20

标签: php ios swift audio

我开发了一个演示应用程序,用于将音频文件从ios应用程序上传到服务器。

Bellow是我将audiofile数据转换为base64字符串的快速代码

let url = URL(string: "https://localhost/development/uploadAudio.php")

    let path:NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
    let documentDirectory: AnyObject = path.object(at: 0) as AnyObject
    var fileName = "audio.mp3"

    filePath = documentDirectory.appendingPathComponent(fileName as String)
    print(URL(fileURLWithPath: "\(filePath)"))
    var fileData = try! Data(contentsOf: URL(fileURLWithPath: filePath))
    var data:String = (fileData.base64EncodedString(options: .lineLength64Characters))



    let request = NSMutableURLRequest(url:url!);
    request.httpMethod = "POST"
    let post:NSString = "subjective=\(data)"  as NSString

    let postData:Data = post.data(using: String.Encoding.utf8.rawValue)!
    let postLength:NSString = String(postData.count) as NSString
    request.httpBody = postData
    request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
    request.setValue("application/json", forHTTPHeaderField: "Accept")
    _ = URLSessionConfiguration.default

    let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in

        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)

        if error != nil{

        print("error is : \(error!)")

        }

        print("Response is : \(responseString!)")




    })
    task.resume()

在php中,我在php中获得了这些数据的价值 在下面的代码中

public function saveRecordedVoice()
{

  $audio = $_POST['data'];
  $decoded = base64_decode($audio);
  $file_location = "assets/records/recorded_audio.mp3";

  $success = file_put_contents($file_location, $decoded);

  if($success){
     echo "File Saved";
  }else{
    echo "Uh-uh!";
  }

}

这里我将各个文件夹文件中的数据保存为“recorded_audio.mp3” 但是当我在音频播放器中播放此文件时,我没有得到清晰的语音音频文件。 我怎么能这样做。

0 个答案:

没有答案