SKAudioNode不加载来自网址的声音 [英] SKAudioNode not load sound from url
本文介绍了SKAudioNode不加载来自网址的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从网址下载声音并播放一次:
I want download sound from url and play it once:
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
I tried .mp3
music too. It doesn't download it or stream and returns: nil
但此代码导致错误:
由于未捕获的异常终止应用
'com.apple.coreaudio.avfaudio',原因:'必需条件为false:
file!= 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:以$ $类型的未捕获异常终止b $ b NSException
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()
}
这篇关于SKAudioNode不加载来自网址的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文