Can't get AVFoundation Pause () working in CollectionView

169 Views Asked by At

In my app I have a collectionViewCell that plays music, In the collectionViewCell class I made the player variable

 var player: AVAudioPlayer?

I also tried:

var player = AVAudioplayer()

I made a method pauseMusic ()

func pauseMusic (){

    if player != nil {
    if player!.playing == true{

    player!.pause()
       }
    }
}

When I call this pauseMusic() method from my FirstViewController in the UIBarButtonItem action, my app crashes

 @IBAction func pauseButton(sender: UIBarButtonItem) {

    var colVW = CollectionViewCell()
    colVW.pauseMusic()
}   

I also uploaded this screenshot that shows the error when it crashes

http://nl.tinypic.com/r/t66zir/8

Is anybody familiar with this crash? I would really appreciate if you could give me some advice!

Additional code:

This func checks which song has to be played, this func lives in the CollectionView. Because of the link between this func and the CollectionViewCell class, I can't move the AVPlayer somewhere else..

func prepareAudios() {

   var songIndexString = (imageOutlet.titleLabel?.text)!
    //println(songIndexString)

    var songIndex = (songIndexString.toInt())!

    println("songIndex: \(songIndex)")

    if musicStopped == true {

        rateButton()
        println("empty")

    } else if var filePath = NSBundle.mainBundle().pathForResource(audioArray[songIndex], ofType: "mp3"){

        var filePathUrl = NSURL.fileURLWithPath(filePath)
        player = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil)
        player!.play()

    } else {
        println("Path for audio file not found")
    }
}
0

There are 0 best solutions below