tags:

views:

634

answers:

4

I have an instance of AVAudioPlayer running inside one of my tabs. It is activated through an IBAction.

I would like the music to stop when a user clicks on another tab.

How would I go about doing this?

I've tried theAudio.stop; in viewDidLoad, but that didn't work.

A: 

In your UITabBarControllerDelegate implement the following method;

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

This is called whenever the user selects a new view in the controller.

I think you could also override the following method in the UIViewController that's playing the audio

-(void) viewDidDisappear:(BOOL)animated
Andrew Grant
viewDidDisappear: isn't called by UITabBarController unfortunately.
millenomi
ahh - well in that case you could easily have your UITabBarControllerDelegate call these methods and encapsulate the logic of what to do on a tab change in each controller.
Andrew Grant
Thanks for the help, but I can't get it to work. I can't get the @interface in UITabBarControllerDelegate.h to accept <AVAudioPlayerDelegate>.
A: 

Wire up a UITabBarControllerDelegate to your main view, then listen for (void)tabBarController:(UITabBarController *)tabBarController didSelectItem:(UITabBarItem *)item

When you get that event, find the player in your object model and stop it.

slf
A: 

Were you able to solve this? Have the same problem here.

john
A: 

I happened upon this post while trying to answer the same exact question. Just in case anyone else is still looking, I finally figured out how to do it with an NSNotificationCenter. Basically, an NSNotificationCenter sends "broadcasts" a message to the entire application. If an "observer" happens to be listening, as you can see below, a given method is called. The code looks like this:

In your App Delegate:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];

    // Make sure you add this so that your tab bar calls its delegate methods
    tabBarController.delegate = self;
}

    // Optional UITabBarControllerDelegate method (this will be commented out by default - uncomment it)

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
                // Create the NSNotificationCenter
                [[NSNotificationCenter defaultCenter] postNotificationName:@"tabChanged" object:nil];
    }

In your view controller:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register an observer to stop audio recording/playing on tab change
    [[NSNotificationCenter defaultCenter] addObserver:self
              selector:@selector(tabChanged)
              name:@"tabChanged"
              object:nil];
}

- (void)tabChanged {
     @"Received Notification!";
     if([player isPlaying]) {
      [player stop];
     }
}