Stop sound when user push home button - iphone

I play a sound in viewDidLoad now when user push home button sound paused.when user run app again sound play from place that paused before.how can i stop it(no pause) when user push home button?

Have a look into the application delegate methods:
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillResignActive:(UIApplication *)application
You may place code there that gets run, when your app is closed. Try keeping a pointer to you AVAudioplayer around and "stop" its currently playing content if necessary.
Be sure to restart it when the user comes back!
The documentation on the "UIApplicationDelegate Protocol Reference" helps :)

NSString *path = [[NSBundle mainBundle] pathForResource:#"*" ofType:#"mp3"];
If you use * in the place of the mp3 name the music will be stopped.

Related

iPhone - Need to replay background sound when it comes to foreground from background

My application plays a background music when it launches. It stops when I quit the application. But not playing the music again when I open my application. The application has 2 views and I want to play background sound on only main view. So when the app launches it should play the sound until user quit the app or when the user goes to the second view in the app. The sound should be played again only when the user returns to the main view. Currently, I am calling the sub function which plays the sound in 'viewDidLoad' function. please help.
Thanks
Try using this method in your AppDelegate:
- (void)applicationDidBecomeActive:(UIApplication *)application
DidBecomeActive Apple Documentation

Is there a way in iOS to observe the home button being pushed?

I am basically trying to make the device play a sound whenever the app exits (even if the phone is being turned off, the sound would play while the button is being held down). Is this possible?
You could try the - (void)applicationWillResignActive:(UIApplication *)application UIApplicationDelegate method.
Alternately, you could listen for the UIApplicationWillResignActiveNotification notification.

MPMoviePlayerController problem when play a movie

i put a movie when my application will lunch . just like gameloft games .
so when application has lunched , movie plays fine but before the move plays .. my FirstViewController xib file show first then moview start to play ! why ?
here is my code :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"movie" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
IntroMovie.movieControlMode = MPMovieControlModeHidden;
[IntroMovie play];
}
Your question is a little unclear, but what I think you are asking is why your movie is not playing when the application launches before the user sees the controls and items in your XIB file. If this is correct, then this is the normal behavior as typically the application will launch and only then call the function you have (applicationDidFinishLaunching). There is no guarantee that you will get that function called before the controls have loaded and are viewable on the screen all the time.
It is bad form to start you application with a movie, but if you want to then you have to deal with the fact that there will potentially be a default image or even your controls that you put in your XIB file.
One way to start with a movie, is that you must hide or delete everything in the XIB file so the screen will appear blank when the application loads. Then, when you application calls the applicationDidFinishLaunching function the user will not have seen your application and the first thing they will see is the movie. When the movie is finished playing, you will need to have a notification listener instruct your application to show the controls for your application or switch to another XIB file that has the app controls within it.
I hope this helps.

How to play sound automatically when scrolling to the next page?

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);
}
}
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.
-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.

Make AVFoundation framework not fading when screen fades

I am implementing some audiobooks for iPhone. I used AVFoundation. Something like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"intro" ofType:#"mp3"];
player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
I have a problem. When the screen goes dark (single audio files can be very long) the audio stops playing.
I solved this problem with this string of code
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// something else here...
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
This does not allow the iPhone to "sleep". However you can guess how this is foolish: your battery level goes down in minutes and this is not possible by audiobooks lasting more than 20 hours, for example...
So, do you know a way to prevent that when the screen sleeps the AVAudioPlayer does not stop playing?
Thanks...
Fabio
Set your audio session to kAudioSessionCategory_MediaPlayback to playback while the screen is locked.
Could I suggest that once you start playing the audio file that you tell the user to press the power button (not home button) which will lock the phone. It will not close you app but it will power off the screen with you application running in the background. Currently several apps do this.