1
votes

I am always getting the fatal error: unexpectedly found nil while unwrapping an Optional value. but if i look the the fileURL variable it has some values. please let me know what i missed here:

Error:

Optional(http:/files.parsetfss.com/461a4eda-d153-4d46-bd85-28ddd355a94c/tfss-03d4eb57-51cb-424d-8c90-2d8a89429203-00255--How_To_Build_A_Loving_Family.mp3 -- file:///)   
fatal error: unexpectedly found nil while unwrapping an Optional value

Code:

        if let audioFile = object["audioFile"] as? PFFile {

            var audioPath: String = audioFile.url!

            var fileURL = NSURL(fileURLWithPath: audioPath as String)
            println(fileURL)
            audioPlayer = AVAudioPlayer(contentsOfURL: fileURL, error: nil)

            audioPlayer.volume = volumeSlider.value

            audioPlayer.play()

        }
1
debug your code and try to find out which line it give you fatal errorRizwan Shaikh
http:/files is not a valid url, maybe that is the problem, otherwise show where it crashesVolker
it is giving an error here: audioPlayer = AVAudioPlayer(contentsOfURL: fileURL, error: nil)Jingo Bracamonte

1 Answers

0
votes

This code is working fine with your URL:

let url = "http://files.parsetfss.com/461a4eda-d153-4d46-bd85-28ddd355a94c/tfss-03d4eb57-51cb-424d-8c90-2d8a89429203-00255--How_To_Build_A_Loving_Family.mp3"

    let soundData = NSData(contentsOfURL: NSURL(string: url)!)

    var error: NSError?
    self.audioPlayer = AVAudioPlayer(data: soundData, error: &error)
    if audioPlayer == nil
    {
        if let e = error
        {
            println(e.localizedDescription)
        }
    }

    audioPlayer!.volume   = 1.0
    audioPlayer!.prepareToPlay()

    audioPlayer!.play()

This way you can convert your audio to data which will take some time to play your audio.

Here is another way to play song live:

let url = audioFile.url!
let playerItem = AVPlayerItem( URL:NSURL( string:url ) )
player = AVPlayer(playerItem:playerItem)
player.rate = 1.0;
player.play()