gpt4 book ai didi

ios - SKAudioNode 不从 url 加载声音

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:35 27 4
gpt4 key购买 nike

我想从 url 下载声音并播放一次:

let sound = SKAudioNode(url:URL(string:"http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02-16kHz.wav")!)

sound.run(SKAction.play())

我也尝试了 .mp3 音乐。它不会下载或流式传输并返回:nil

但是这段代码会导致错误:

Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: file != nil' *** First throw call stack: ....

libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

我想你试试 AVAudioPlayer

var resourcePath = url  //your url
var objectData = Data(contentsOf: NSURL(string: resourcePath)!)
var error: Error!
do {
audioPlayer = try AVAudioPlayer(objectData)
}
catch let error {
}
audioPlayer.numberOfLoops = 0
audioPlayer.volume = 1.0
audioPlayer.prepareToPlay()
if audioPlayer == nil {
print("\(error.description)")
}
else {
audioPlayer.play()
}

关于ios - SKAudioNode 不从 url 加载声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39973323/

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