base64字符串到iOS中的音频mp3?

时间:2019-05-03 07:30:02

标签: ios swift iphone base64 decode

我有一个base64字符串,想将其转换为mp3音频文件。

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

它总是返回nil。

1 个答案:

答案 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 {
    }
}