Pausing Audio when App Goes to the Background

how to stop apps from stopping music android
how to keep music playing when screen turns off android
why does my music stop playing on my android
why does my music keep stopping on my android
audio stops when iphone locks
how to stop apps from pausing music iphone
android music player stops randomly
audible stops playing when phone sleeps

I have a notification setup to alert my VC when the app is going to resign the active state:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(pauseGame)
                                             name:UIApplicationWillResignActiveNotification
                                           object:[UIApplication sharedApplication]];

As you can see it calls a method called pauseGame. That method, amongst other things, contains:

[audio pauseAudio];

I use this same method within the app to pause and everything works as it should.

This method is properly being called when the app is being sent to the background, however, when the app is resumed the audio continues to play. The other pause items are working on restore so the method is completing.

How can I get the audio to pause properly when entering the background?

Update: People have been mentioning the ApplicationDidEnterBackGroundNotification. I tried that but it works the same. It should be noted that Apple in their own comments in the AppDelegate recommend the method I originally used. See below for their pre-placed comment within the delegate.

    - (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

Update 2: Using WillResignActive works correctly for pausing when a call comes in for example. With that event the audio stops properly. So it seems to be just about the app going to the background when the home button is pressed. The background notification isn't called for incoming calls so obviously a call and home press result in different states.

    - (void)applicationDidEnterBackground:(UIApplication *)application
 {
   /*
   Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
 If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
   */
    NSLog(@"Application moving to background");
  // Here Call your notification for pause  audio
}

https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Audio stops playing on Samsung mobile devices or accessories, Remove audio apps from sleeping apps list to keep playback from stopping. to run in the background, your audio may stop if the phone or app goes to sleep. How do I pause my sound playing in the background in my app? So I press a button and my app goes to the credits where Concerning Hobbits starts playing. I want to have the music stop (or in this case, it would be mPlayer.pause() ) when the user presses the back button on their phone.

Just try this way...

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

    bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

            [[UIApplication sharedApplication] endBackgroundTask:bgTask];
        }];

    }

Also declare bgTask on Appdelegate.h

UIBackgroundTaskIdentifier bgTask;

Here you just change the NSNotifcation Centre as

[[NSNotificationCenter defaultCenter] addObserver: self
                                                 selector: @selector(pauseGame)
                                                     name: UIApplicationDidEnterBackgroundNotification
                                                   object: nil];

. Hope this will help you....

Paused Avplayer resumes when app comes to foreg |Apple , In my app I am calling avplayer.pause() to explicity pause the content and (I have following set in plist to enable background audio mode). If music apps on your phone or tablet are not allowed to run in the background, your audio may stop if the phone or app goes to sleep. Whether you're streaming music from third-party apps like Spotify, using Samsung earbuds, or listening directly to audio files on your phone, you'll need to update your sleeping apps settings to prevent this from happening.

Write this in your viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayer) name:UIApplicationDidEnterBackgroundNotification object:nil];

and add a new method:

-(void)pausePlayer{
    [audio pauseAudio];
}

There are many different notifications, just check the UIApplication.h

Technical Q&A QA1668: Playing media while in the background , With this capability enabled, your app's audio can continue when users is automatically paused when the app is sent to the background. Try to Clear the Data of that particular Music App. To do so, follow these steps: * Open Setting App * Click on App option * Find your Music App and click * Click on Storage option * Then Clear Data.

I have found stopping the audio versus pausing it takes care of the issue. It is interesting to note that stop does not start the audio over when resumed but rather will start where it left off. In this way stop works the same as pause and satisfies my needs.

Music stops when the screen is locked, Documents app allows you to listen to audio and video files when the app is in If the music stops while in the background, please open Documents settings Please note: video files (e.g. .mp4 video) will be paused once you lock the screen​. Alternatively, on laptops and tablets, it's also possible to prevent apps from running in the background enabling the Battery Saver mode. While the feature will enable automatically as the battery life drops below 20 percent, you can enable the mode manually at any time using these steps: Open Settings.

I know this is an ancient question, but I just found this solution to the same problem (which is still around in 2018 with iOS 11 and I assume iOS 12):

Register for AVAudioSessionInterruptionNotification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(avAudioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil];

When your application gets resumed you'll get this notification. When you resume the app, you'll be told that the interruption began then immediately be told that it ended. If your audio is playing, you will pause it in the "began" handler. Then "ended" message will not have the "resume" value, so you should stop your audio. Here's my code:

- (void) avAudioSessionInterruptionNotification:(NSNotification *)notification
    {
    AVAudioSessionInterruptionType type = [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
    if ( type == AVAudioSessionInterruptionTypeBegan )
        {
        // If your audio is active, pause it here.
        }
    else if ( type == AVAudioSessionInterruptionTypeEnded )
        {
        AVAudioSessionInterruptionOptions optionsValue = [notification.userInfo[AVAudioSessionInterruptionOptionKey] intValue];
        if ( optionsValue == AVAudioSessionInterruptionOptionShouldResume )
            {
            // Resume your audio here
            }
        else
            {
            // Stop your audio here
            }
        }
    }

Audio Playback Continues After App goes Background · Issue #266 , Audio Playback Continues After App goes Background #266 Also, in the MainFragment's onPause(), I tried stopping playback on the current  Looking at the diagnostic log, everytime the music player pauses by itself is when one of the background processes for 2 apps also seems to be crashing. Looks more than a coincidence. The two apps are: 1. GV Phone by Mo+ 2. GV Phone Plus by Mo+ The two processes in question are: 1. com.moplus.monkey. 2. com.moplus.monkey.gv.

Programming Windows Store Apps with HTML, CSS, and JavaScript, The start page isn't really essential for background audio (because the app will will always be paused and muted when the app goes into the background. If Restrict Background Data is set to ON, the music app may not be permitted to connect to the internet by this feature resulting to the issue you’re having. Set Keep wifi on during sleep to ON

Video is paused when app enters background. · Issue #88 , I've setup my app to play background audio, this includes setting the to pause to avoid the small video pause when app goes in background. Well yea, the title says it all. When I play a song (headphones always plugged) and my phone goes turns the screen of, spotily stops streaming. (well not instantly, but after a while) it's not an internet problem, promise! PS:/ Will android get the "Extreme quality" feature?

Continuing to play music and go to other apps without pausing the , Spotify/SiriusXM adverts are all audio not video based so there is no requirement to keep the screen on so they can be used in the background as  If you want an app to continue performing these functions, you should allow it to continue running in the background. If you don’t care, feel free to prevent the app from running in the background. You can still use the app normally, but you may have to wait for it to fetch new data after you launch it. Prevent All Apps From Running in the Background With Battery Saver Mode. You could also use Battery Saver mode to accomplish this. When Battery Saver mode is activated, no apps will run in

Comments
  • Thank you for the formatting edits.
  • try to use "UIApplicationDidEnterBackgroundNotification" - like this: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseGame) name:UIApplicationDidEnterBackgroundNotification object:nil];
  • Same issue as using the resign statement I posted above.
  • Apple's own comments in the AppDelegate file recommend using the method I did in my original post. However, I tried the background method and it doesn't work any better.
  • Unfortunately this does not correct the issue. Same problem as my original post.
  • I already in pretty much doing exactly this in what I posted to start. changing the notification to background produces the same issue as I describe above.