I am very new to Swift programming and has started learning a few weeks now. I am testing a AVAudioPlayer from YouTube tutorials and it works perfectly. Here is the code.
let filelocation = NSString( string: NSBundle.mainBundle().pathForResource(self.navigationItem.title, ofType: "mp3")!)
player = AVAudioPlayer(contentsOfURL: NSURL( fileURLWithPath: filelocation as String)!, error: &error)
Those mp3 files are placed in the project so it is playing locally. I changed the code to the following to get mp3 file from URL and played it here. But it didn't work.
let url : NSString = "https://ia601409.us.archive.org/20/items/UsaNationalAnthemFromSilo/silosinging-us-anthem_64kb.mp3"
let urlStr : NSString = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
player = AVAudioPlayer(contentsOfURL: NSURL( fileURLWithPath: urlStr as String)!, error: &error)
I got an error at player.prepareToPlay() with "fatal error: unexpectedly found nil while unwrapping an Optional value" and value is "error NSError? domain: "NSOSStatusErrorDomain" - code: 2003334207 0x00007fc3e3e4b820"
I am thinking NSURL(fileURLWithPath: urlStr as String)! is not correct, so it is giving just nil. Any thoughts on how to correct it! Thanks.