0

I'm using Back4app as Parse server I'm trying to stream audio from Prase everything works fine , but if I click on any index (except index 0) on tableview I got error "fatal error: unexpectedly found nil while unwrapping an Optional value"

I don't know why when I click on Index 0 it work fine but any other index I got error !

func grabSong() {        
    let SongQuery = PFQuery(className: "Songs")

    SongQuery.getObjectInBackground(withId:iDArray[SelectedSongNumber!] ,block: { (object : PFObject?, error : Error?) ->  Void in           
        if let AudioFileURLTemp : PFFile = object?.value(forKey: "SongFile") as? PFFile {            
            print(AudioFileURLTemp)

            audioP = AVPlayer(url: NSURL(string: AudioFileURLTemp.url!) as! URL)            
            audioP.play()        
        }       
    })
}

I got the error on this line:

audioP = AVPlayer(url: NSURL(string: AudioFileURLTemp.url!) as! URL)
shim
  • 9,289
  • 12
  • 69
  • 108
Basel
  • 550
  • 8
  • 21
  • Check piece by piece: `AudioFileURLTemp.url`, then `NSURL(string: AudioFileURLTemp.url!) as! URL`. If you use Swift 3, do you have to switch between `NSURL` & `URL`? – Larme Jan 04 '17 at 16:56
  • I think so , if I delete (as! URL) I got this error "Cannot convert value of type 'NSURL?' to expected argument type 'URL" and suggest me to add as! URL .... I also check AudioFileURLTemp.url it give me different song link depend on which index I clicked – Basel Jan 04 '17 at 17:05
  • Can't you use `URL(string:AudioFileURLTemp.url!)`? – Larme Jan 04 '17 at 17:06
  • I can use " audioP = AVPlayer(url: URL(string:AudioFileURLTemp.url!)!) " but I got same problem ,first index working fine the rest I got same error – Basel Jan 04 '17 at 17:12

1 Answers1

0

I found out why I got the error. It's because some of the song names have spaces in them, but the first song doesn't have any spaces in its names. So I tried to copy the link in the browser and see how the browser deals with spaces. I found out it replaces spaces with %20.

So I replaced every space with "%20" and now it works.

Correct code

func grabSong() {        
    let SongQuery = PFQuery(className: "Songs")
    SongQuery.getObjectInBackground(withId:iDArray[SelectedSongNumber] ,block: { (object : PFObject?, error : Error?) ->  Void in                    
        if let AudioFileURLTemp : PFFile = object?.value(forKey: "SongFile") as? PFFile {                       
            var songID = AudioFileURLTemp.url!.replacingOccurrences(of: " ", with: "%20")

            audioP = AVPlayer(url: URL(string:songID)!)
            audioP.play()
        }
    })
}
shim
  • 9,289
  • 12
  • 69
  • 108
Basel
  • 550
  • 8
  • 21
  • 2
    Swift has a built-in function for this: `.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)` The advantage of this is that it will handle other invalid characters that might end up in a song name, such as accented characters like "é" There also are a few different options for the character set that you can use. – shim Jan 04 '17 at 17:51