gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:27:39 26 4
gpt4 key购买 nike

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

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

它总是返回零。

最佳答案

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

关于ios - base64 字符串到 iOS 中的音频 mp3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55965147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com