Totoajax
Totoajax

Reputation: 1

Pausing AvAudioPlayer from another view controller returns EXC_BAD_ACCESS Swift

I've a page-based application where the root view controller has a button which is suposed to pause an AVAudioPlayer in the data view Controller. I take for granted that you know how the root and data view controller work. Mine looks like this.

When I'm pressing the button I get an EXC_BAD_ACCESS. But when I'm pausing from a button I have on data view controller it's working fine without the crash. I've tried:

• To add the pause code in a function and call the function

var dataView = self.storyboard?.instantiateViewControllerWithIdentifier("DataViewController") as! DataViewController
dataView.pause()

dataViewController

func pause() {
    player.pause()
}

• Calling a function in dataView that calls a function that pauses the player

• Tried to call with

dataViewController().player.pause()

• Tried to call player.prepareForPlay() before player.pause

And all of them gives me the same error. Can it be because rootViewController doesn't have permission to edit player in dataViewController?

Upvotes: 0

Views: 626

Answers (1)

Duncan C
Duncan C

Reputation: 131398

This code:

dataViewController().player.pause()

Creates a new instance of DataViewController. That new instance doesn't have a sound player. Don't do that. You need a way to get a pointer to your existing instance of dataViewController.

By the way, you should follow the Swift naming convention of naming classes and types starting with an upper-case letter. Thus the class name should be DataViewController.

Upvotes: 1

Related Questions