Search code examples
iosxcodeswiftavaudioplayer

When I stop AVAudioPlayer I get an error EXC_BAD_ACCESS


I have a UIViewController which is playing music and UITableViewController which lists songs. I made that I leave PlayViewController, AVAudioPlayer will still be playing a song. I want that I return on UITableViewController and I choose a different song the AVAudioPlayer will be stopped. I tried many different methods but I get the same error EXC_BAD_ACCESS. I also made AVAudioPlayer like weak var and I also made PlayViewController is a public class but I got the same error. How can I to fix this problem?

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "playMusic" {
            var playMVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicVC
            var currentIndPass = tableView.indexPathForSelectedRow()!.row
//            println("array item \(arrayItem)")
            playMVC.currentIndex = currentIndPass
            playMVC.arrayOfSongs = arrayItem
            searchController.active = false
//            println("array item \(arrayItem)")
            //test
            var secondPlayView = PlayFromSearchVC()
            if secondPlayView.audioPlayer.playing == true {
                secondPlayView.audioPlayer.stop()
                println("It is works")
            } else {
                println("It is not works")
            }
            //test
        } else if segue.identifier == "summer" {
//            println("array super filter and count \(arrayFilterOfNames) co \(arrayFilterOfNames.count)")
            var playVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayFromSearchVC
            var currentInt = tableView.indexPathForSelectedRow()!.row
            playVC.currentSongString = arrayFilterOfNames[currentInt]
//            playVC.currentIndex = currentInt
//            playVC.arrayOfSongs = arrayFilterOfNames // arrayFilterNames
            searchController.active = false
            //
            var secondPlayView = PlayFromSearchVC()
            if secondPlayView.audioPlayer.playing == true {
                secondPlayView.audioPlayer.stop()
                println("It is works")
            } else {
                println("It is not works")
            }
            //
        }
    }

AVAudioPlayer is

    var audioPlayer = AVAudioPlayer()

enter image description here

Different variant is wrong too enter image description here


Solution

  • This code makes no sense:

    var secondPlayView = PlayFromSearchVC()
    if secondPlayView.audioPlayer.playing == true {
    

    The first line creates a new instance of PlayFromSearchVC. Since it did not exist before, the second line does not make sense. It's like asking a baby that was just born about it's favorite toy.

    You don't show the structure of your PlayViewController class. How is audioPlayer defined?

    Don't use a view controller that can be closed to manage sound play. Create a singleton object that manages sound play. Add a method to the sound manager that stops any sound that is currently playing, and methods to start a new sound playing. Call the sound manager from everywhere that you need to play sounds.