Paused sound starts to automatically start on applicationWillEnterForeground:

When applicationDidEnterBackground: fires, I pause the sound that is played using AVAudioPlayer :

 [self.avPlayer pause]; 

Now that applicationWillEnterForeground: starts, the sound starts to play automatically! Since I did not start the sound, the user interface is not updated and it shows that the sound is still paused.

What's happening? This happens on iOS 6.x, on iPad 2. This issue does not play on an earlier iPad running iOS 5.x.

This is how I configure AVAudioSession :

 // Setup the audio session BOOL succeeded = NO; NSError *sessionError = nil; AVAudioSession *session = [AVAudioSession sharedInstance]; [session setDelegate:self]; if ([session respondsToSelector:@selector(setPreferredSampleRate:error:)]) { succeeded = [session setPreferredSampleRate:128000.0f error:&sessionError]; } else { succeeded = [session setPreferredHardwareSampleRate:128000.0f error:&sessionError]; } succeeded = [session setCategory:AVAudioSessionCategorySoloAmbient error:&sessionError]; succeeded = [session setActive:YES error:&sessionError]; 
+6
source share
4 answers

In one of my recent applications, I found a similar problem where audio playback automatically pauses and resumes after you interrupt the call and the GUI does not update in my application. I fixed the problem using the following method:

Register this in the class in which you process the player code

 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

And use the AVAudioSession audioPlayerEndInterruption delegate method to gain control after the application resumes. In this function, you can respectively resume playback and update the interface.

 - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags 

Hope this helps.

+1
source

I found some weird UX, and when I went back and forth from the background and based on a bunch of messages, I planned to catch the handle a bit. I did something quite simple - check if other music is playing when the application comes to the fore:

  func applicationWillEnterForeground(application: UIApplication) { if AVAudioSession.sharedInstance().otherAudioPlaying{ NSNotificationCenter.defaultCenter().postNotificationName(Notifications.ForegroundEnteredWithOtherAudioPlaying, object: nil) } } 

If so, you can pause the player when you receive this notification. You mentioned that you didn’t work, but without seeing the rest of your code, it’s hard to understand why.

Another thing I had to do was remove the observer with the status flag from the player element, because I am listening to it for the ReadyToPlay value, where I tell AvPlayer to play. This works when the application comes to the forefront and there is an active audio session. Therefore, by removing this observer when paused and setting it every time I play (which is not crazy, because I claim that this restriction is OK, to say that I only watch after I got into the game), I can control a lot of weirdness.

I also wrote a bunch of test cases that I will cover here to make sure your experience is in very good shape. It is not comprehensive, but it is also quite thorough.

  // 1. Play, leave app, don't open anything else, come back // 2. Play, leave app, open an app that plays music, come back // 3. Play, leave app, open an app that plays music, play music, come back // 4. Play, let audio go for 5 seconds, pause, open an app that plays music, come back // 5. Play, let audio go for 5 seconds, pause, open an app that plays music, play music, come back // 6. Press play, leave app before playback starts, open an app that plays music, come back // 7. Crazy s$*t like play, leave app, open an app that plays music, play music, come back, tap a different song 
+1
source

HRM solution does not work for me. The only way I got this to avoid restarting the game automatically is to use stop: [self.avPlayer stop]

0
source

I also had a problem. So I registered my class for UIApplicationWillResignActive , and I paused the sound when I received a notification.

0
source

Source: https://habr.com/ru/post/948941/


All Articles