How can I avoid the AVPlayerLayer `setPlayer` audio blip? - ios

I have an application that plays video using AVPlayer and AVPlayerLayer. In my app, I'm able to play audio when the app is locked by setting the player property of my AVPlayerLayer to nil when the application enters the background:
-(void)appEnteredBackgroundNotification:(NSNotification *)notification {
[[self playerLayer] setPlayer:nil];
}
However, when I do this, the audio will lag/blip for around 0.5 seconds. This sounds really really bad for the end user. Same goes for when the app enters foreground and I re-set the player property.
How can I avoid this audio blip? As a test I've tried removing the player in a background thread to no avail.
Update: I spoke with an Apple engineer at WWDC and they said that this issue is a bug on their end (so far not fixed in iOS 9) and this approach is the correct approach. Great...

I think may not you call pause before setting to nil and vice versa. And, try calling prepare before play.

Related

iOS: AVPlayer play won't play sometimes

So I have an app built with a player that plays a video, I have a [player pause] and [player play] in the didBecomeActive and willResignActive methods. Most of the time works fine, but when I open the app, and press the home button and repeat again that process, around the 8th time the video will not play even though I see the play method getting called.
Does anyone have any idea what might be going on?
The app can be in several states that are not foreground. Before playing, check to see if you still have a player, that it still has a player.currentItem, and if it's status is AVPlayerStatusReadyToPlay.
If any of those conditions are not met, then the player and the item must be reinitialized using the code that you used to create it in the first place.
This is a good candidate for a lazy initializer for your player property.

Is there a way to prevent AVPlayerViewController from updating the lock screen via MPNowPlayingInfoCenter?

here is my problem:
I've got an app playing audio files, updating the lockscreen info via MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo, and this part is working fine.
But in an other view, i'm playing a video with AVPlayerViewController and AVPlayer, and when the video starts playing, it's updating the lock screen automatically, with nothing except the video duration.
I didn't find anything about this behaviour in Apple's documentation, I can't find a way to disable it.
So far, I've tried calling UIApplication.sharedApplication().endReceivingRemoteControlEvents() before the video starts playing, and beginReceivingRemoteControlEvents() after. It doesn't work.
Does anyone know a way to prevent this?
Starting with iOS 10 there is a BOOL property in AVPlayerViewController called updatesNowPlayingInfoCenter, that has the default value: YES. Just change it to NO:
//playerController is an instance of AVPlayerViewController
if ([self.playerController respondsToSelector:#selector(setUpdatesNowPlayingInfoCenter:)])
{
self.playerController.updatesNowPlayingInfoCenter = NO;
}

Why does SKVideoNode automatically starts playing video on iOS9?

When initialising a SKVideoNode with a video URL in iOS9 the behaviour changed to automatically starting the video as soon as the node is added, i.s.o. after the play method is called in previous versions of iOS.
doing the following inside the SKScene init
SKVideoNode* videoNode = [SKVideoNode videoNodeWithVideoFileNamed:#"sample.m4v"];
[self addChild:videoNode];
is enough to see the video playing, while previous iOS versions (more logically) required to also call
[videoNode play];
before the video starts playing.
Is this an intended change? A possible workaround I see is always immediately calling pause after initialising a video but it's a bit weird this behaviour changed.
(it feels like a bug to be honest)
(I also found the iOS9 simulator to have issues with SKVideoNodes playing at all, you have to test this on a device)

Play Audio when suspending to background

I have an app that under certain circumstances needs to play audio the moment it is pushed into background.
I can't use any background key, as I don't fulfill any requirements for these and Apple rejected my app, when I tried to use the background audio key.
Currently I'm start playing the audio file and then request extra processing time within applicationDidEnterBackground: The audio never gets played even though I have the processing time and the app is still running (I've used an NSTimer to check every 2 seconds).
As audio mode I'm using AVAudioSessionCategoryPlayback with AVAudioSessionCategoryOptionMixWithOthers.
With active background audio mode this works fine.
I also know, that I could use local notification and play the audio file there (as it is under 30 seconds), but that would be my last resort.
Does Apple prevent audio from being played in applicationDidEnterBackground: ?
I get a callback for audioPlayerBeginInterruption: immediately.
I've seen:
Why does the following code to play audio in the background not work?
But this doesn't seem to work anymore on iOS8.
Thanks :)
Your app should not be playing a sound (and certainly not a sound of any length) just because the user backgrounds it, and now Apple has succeeded in stopping you from doing this. You should accept this, abandon your attempts to work around this perfectly reasonable restriction, and move on.

AVPlayer or AVQueuePlayer during iPhone Call

I have an app that uses AVPlayer (or AVQueuePLayer) to play local files that were recorded by the App. All works great. But I also want this to work on iPhone when a call is in progress (the videos are event recordings). What I found is that during a phone call, the video feed to avplayerLayer goes blank, AVPlayer rate change to 0 (STOP), and all attempts to change rate to non-zero (PLAY) are ignored (rate stays at 0). There does not appear to be any documentation on this, and the only way to detect this condition in the player, is that player is STOPPED and will not start PLAYBACK. Of course, I also check for audio interruptions, and call center calls in progress.
Obviously, in this case the interruption is caused by a call, so there is always a inactive/resume or a intactive/background/foreground/resume transition. As well as audio route notification, audio interruption. So indirectly I know the condition is probably occurring.
So questions are:
(1) Is there any direct method (specific to AVPlayer,AVPlayerLayer) to be notified that AVPlayer is in this non-playing mode. I now use "avplayer.rate failed to change rate from 0 to non-zero", but this seems hacky (and too much "crossing the streams"!) I want to Notify user that video temporarily can not be played or previewed, so they do not think the App is broken. And also inform them or automatically continue Playback when iPhone call ends. (Without a looping process that keeps trying to start playback every 500ms!)
(2) Can AVPlayer play anything while a iPhone call (Green Bar) is in progress? or is this just the way apple designed the AVPlayer SDK? (If so there is no documentation on this) Obviously, other apps can play video during an iPhone call, but I suspect they are using a lower level SDK and not AVPlayer.

Resources