views:

135

answers:

2

I'm using Page Control example from Apple. I want to be able to scroll horizontally to certain page and get the sound automatically play when viewing that page? And then sound stop when viewing the other page?

So, for example. On page 1, i have some texts. When i go to the next page which is on page 2, i want sound to be automatically play and stop when i go to page 3.

I'm planning to use If statements (since it's not that many pages) to determine which page will get the sound.

My question is simple. How do you get the sound play automatically when viewing certain page? By the way, I'm planning to use System Sound.

I know how to play the sound using button. But how do you get it automatically - start when certain page appear and stop when moving to the next page.

Thank you in advance!

Edited:

I put in this code in ViewDidLoad but the sound played on sooner before it reach the page intended:

// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
    pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
    self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
    if (pageNumber == 3) {
     NSString *path = [[NSBundle mainBundle] pathForResource:@"crunch" ofType:@"wav"];
     SystemSoundID soundID;
     AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
     AudioServicesPlaySystemSound(soundID);
}
}
A: 

Why cant you make the sound go off when your desired viewControllers view goes on screen? You know when it goes on screen from the scrollViewDidScroll method and loadPage method (in the Page Control example). By looking at the methods in there you should be able to identify when you need to play the sound (because the specific view has come on the screen) and play it. Another approach would be to subclass UIViewController with something like UISoundViewController and have it play the sound on viewDidLoad.

Daniel
I edited the question and put in my actual code. I tried to do it under viewDidLoad but the sound play earlier before it get to the intended page for the sound. I wonder why.
viper15
are you using a specific view controller for that view? if you are using the same view controller for all of them then theres the issue
Daniel
I see. No, I'm using the same view controller. Any suggestion how to fix this?
viper15
easiest way would be add a shouldPlaySound boolean property on the controller and you set that when you load the viewControllers...so here again, you gonna have to k now which viewController is the one with sound when you loading them into the scroll view and you can set that property accordingly
Daniel
A: 

-viewDidLoad is called once and only once* when the view controller's view property is loaded. You only add stuff that is called once and only once in this method.

Playing sound clearly isn't a once-and-only-once event. Put those into -viewDidAppear:.

*: Unless the view controller is deallocated.

KennyTM