我有一个base64字符串,想将其转换为mp3音频文件。
var audioData = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)
print(audioData)
它总是返回nil。
答案 0 :(得分:0)
let base64String:String =“一些样本base64” 让audioData = Data(base64Encoded:base64String,选项:.ignoreUnknownCharacters) 如果audioData!= nil { 如果让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 {
}
}