tags:

views:

143

answers:

1

I have an AVAudioPlayer,let's call it player1, that starts in a particular UIViewController, let's call it myController1. I set numberOfLoops to -1 so the audio loops indefinitely. I set up a protocol so that myController1 acts as delegate for the AVAudioPlayer, so that I can trigger audioPlayerDidFinishPlaying. I also wrote a routine to stop the player from within myController1. All of this works.

Suppose I have additional UIViewControllers, say, myController2 and myController3. I have combed the forums and Apple documentation, but I cannot find a way to stop the player from within either of these additional controllers. Is it possible? (I'm reluctant to believe it is not.) If so, can I get some hints?

I'm seventy years old, so when it comes to learning new things I'm sort of like an old dog. I programmed 8 and 16 bit chips for industrial process control in C for thirty years. Object oriented programming is a whole new ball of wax, but I'm having a ball learning.

Many thanks for any help.

Dan

A: 

You could use a Singleton to provide access to the instance of AVAudioPlayer. This would involve creating a class with a static member (the AVAudioPlayer) and a class method to access the static member. myController2 and myController3 could call the class method like so:

[[MyPlayer player] stop];

dbarker
Thanks. I had a workaround of passing pointers to the player from view to view, but this seems error-prone.I set up a Singleton using the example from the Cocoa Fundamentals Guide. My static class compiles OK, but when I try to access methods I get an exception.How do I set up methods in the Singleton, such as a routine to set up the player. I can't show the routine due to the 600 ch limit.I can't figure out how to call such a routine, but I thought the whole idea of a singleton was to make possible calling certain routines from anywhere. Can you point me to an example?Thanks,DanDan
Dan