AVPlayer will not stop playing, returning to the previous view controller (SWIFT)

I have two TableViewController view controllers where I have a list of musical instruments and a UIViewController where music data is displayed and music is playing. Music automatically plays when the image loads and pauses when you press the pause button. However, whenever I return to the previous TableViewController to select other music, the music continues to play. And if I choose other music, they both play together

override func viewDidLoad() {
    super.viewDidLoad()

    timeLabel.text = "00:00"

    if let object = currentObject {

        audioTitle.text = object["audioTitle"] as? String
        let days = object["daysActive"] as! Int
        daysActive.text = "Powertalks: Day \(days)"

        var initialThumbnail = UIImage(named: "trc_app_icon.png")

        audioImage.image = initialThumbnail
        if let thumbnail = object["image"] as? PFFile {
            audioImage.file = thumbnail
            audioImage.loadInBackground()
        }

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

            if let audioPath: String = audioFile.url {

            audioPlayer = AVPlayer(URL: NSURL(string: audioPath))

            audioSlider.minimumValue = 0
            audioSlider.maximumValue = Float(CMTimeGetSeconds(audioPlayer.currentItem.asset.duration))
            audioSlider.value = Float(CMTimeGetSeconds(audioPlayer.currentTime()))

            audioPlayer.volume = volumeSlider.value

            playAudio()

            }

        }

    }

    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateSlider"), userInfo: nil, repeats: true )

}
+4
source share
1 answer

, . AVPlayer , rate 0.0 ( pause()) currentItem . ( )

 override func viewWillDisappear(animated: Bool) {
   audioPlayer.pause()
   audioPlayer.currentItem = nil
 }
0

All Articles