Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中音频mp3的base64字符串?_Ios_Swift_Iphone_Base64_Decode - Fatal编程技术网

iOS中音频mp3的base64字符串?

iOS中音频mp3的base64字符串?,ios,swift,iphone,base64,decode,Ios,Swift,Iphone,Base64,Decode,我有一个base64字符串,想把它转换成mp3音频文件 var audioData = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters) print(audioData) 它总是返回nil。您的字符串是有效的base64字符串吗?因为数据(base64Encoded:)是一个可失败的初始值设定项,这就是为什么如果您的输入不合适,它只会重新运行nil。您的字符串是有效的base64字符串吗?因为数据(base64E

我有一个base64字符串,想把它转换成mp3音频文件

var audioData = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)
print(audioData)

它总是返回nil。

您的字符串是有效的base64字符串吗?因为
数据(base64Encoded:)
是一个可失败的初始值设定项,这就是为什么如果您的输入不合适,它只会重新运行nil。您的字符串是有效的base64字符串吗?因为
数据(base64Encoded:)
是一个失败的初始值设定项,这就是为什么如果您的输入不合适,它只会重新运行nil
let base64String : String = "some sample base64"
let audioData = Data(base64Encoded: base64String, options: .ignoreUnknownCharacters)

if audioData != nil {
  if let audData = audioData {
    self.playAudio(audioData: audData)
  }
}

func playAudio(audioData : Data) {
  let filename = documentsDirectory.appendingPathComponent("output.mp3")
  do {
    try audioData.write(to: filename, options: .atomicWrite)
    do {
      audioPlayer = try AVAudioPlayer(contentsOf: filename)
      guard let player = audioPlayer else { return }
      player.prepareToPlay()
      player.play()
    } catch let error {
      print(error.localizedDescription)
    }
  } catch {
  }
}