SKAudioNode does not load sound from URL

I want to 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())

I tried the music .mp3. It does not download or stream:nil

But this code causes an error:

The application terminated due to the uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'prerequisite is false: file! = Nil' *** First stack of throw calls: ....

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

+4
source share
2 answers

I think you will try 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()
}
+1
source

, URL- URL- , . mp3 , -

let urlpath = Bundle.main.path(forResource: "[name of file]", ofType: "mp3")
let audioURL = NSURL.fileURL(withPath: urlpath!)

let sound = SKAudioNode(url: audioURL)

sound.run(SKAction.play())
0

Source: https://habr.com/ru/post/1657363/


All Articles