Reputation: 6841
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()
Different variant is wrong too
Upvotes: 0
Views: 973
Reputation: 2005
When you try to set currentTime to AVAudioPlayer item but before you set playing content -> you'll definitely get such error
Upvotes: 0
Reputation: 131398
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.
Upvotes: 1
Reputation: 424
Since the AvAudioPlayer (secondPlayView) is a weak property.It would have gone while PlayViewController is removed from memory.Make sure your AVAudioPlayer instance is initialized properly or referring the same instance when you come back to PlayViewController.
Upvotes: 1