AVAudioPlayer Interruption issue - ios

I have a problem with AVAudioPlayer Interruption.
And I'm not talking about the interruption during the incoming call. I can handle this situation very well. Call -> Interruption start -> End of call -> Interruption finish.
The problem is with the camera on the device. The situation is:
My player app is playing on background. I want to make a photo. I open the camera and make a photo. No interruption at all! But if I want to capture some video the interruption starts and the player becomes silent. At the same moment interruption finishes! (I have checked it on two devices with the help of NSLog - and I find it very weird) But the player doesn't continue playing the song because video camera is still ON. I close camera and NOW I expect the interruption to finish, but it was finished earlier and the player remain silent.
The question is: How to handle this situation?

Related

How to capture the events of system alarm being triggered/dismissed on iOS 10

This is my scenario: I have a video player playing some video/audio and suddenly the alarm is called since I set it earlier.
Before iOS 10, the alarm's UI and audio would block the main UI thread so the alarm event can be captured by observe the UIWindowDidResignKeyNotification.
But on iOS 10, it seems that alarm UI behaves like Notification messages and will not block the main UI.
But its alarm sound would take over audio thread, so the result is that the video picture is playing while the video sound is not, instead the alarm sound is playing.
With iOS 10 what I have done is I observe AVAudioSessionInterruptionNotification, which would be posted when alarm is running, and once I get it I pause my video playing.
But this results another issue, which is that my users have to click the play button manually again after they dismiss the alarm, this is not so good.
Plus AVAudioSessionInterruptionNotification would be posted when any other type of system audio is triggered, such as iMessage, Push Notification which are messages with very short audio, and that causing pauses would really gets users into trouble.
So I would like to know if there is a way to listen the events that users dismiss the alarm UI up/down.
Or if anyone could come up with some other solution to solve this kind of problem.
UPDATE:
This alarm sound takes over the audio thread issue would only happen when video is decoded by hardware.
If it's default system decoding for video then it'll be fine.
And if incoming call is triggered and call sound also would take over the audio thread which is fine, since incoming call would block the main UI so I can also manage it by observing UIWindowDidResignKeyNotification.
Ok, my fault that I didn't read through apple doc, everything is here.
You can use the userInfo in AVAudioSessionInterruptionNotification.
It gives the enums that when it has began, when it ended and even when it's the best time to resume.

Resume playing music after alarm

My application have permanent music inside that should play when it opened. Problem appears when alarm rang. It automatically stops app music, but it's not resumed after. Anyone know how can I handle this?

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.

AVAudio Recorder is not recording after comes from background

I have used AVAudioRecorder. The recorder is not recording when i come from background.
My Scenario is
Start recording press home button (Recording stopped)
Open my app the recorder is still recording (Checked by recorder.isRecording is YES)
Stop the recording and playing the recorded audio plays only what recorded before my app goes background.
I am recoding 5sec -> app goes background -> comes fore foreground -> record another 5 secs -> play the audio the audio plays only the first 5 secs.
While recording I am checking the size of the file path given to the audio recorder(By NSTimer) the file size is not increased after come from background but still the recorder status is recording
Can any one point out what is the mistake. Thanks
Note: in ios6 there is no problem it plays entire audio that is entire 10 sec it comes only in ios7
So if you dont want to record in the background and you only want to append your recording before and after going to background, pause your recorder in applicationWillEnterBackground (or applicationWillResignAcive) and when you come back form background, continue your recording and you'd be fine. (You can have notifications letting your class which is recording know about those life cycle delegate methods.)
But if you want to record in the background you need to turn on Background Mode: Audio and Airplay in your app settings -> Capabilities. But thats another story.
You should check AVAudioSessionDelegate delegate method. Audio recording will stop during phone call
- (void)beginInterruption{
}
-(void)endInterruption{
}
You can continue background recording. Add the background modes as seen in image

Use javascript to detect if a youtube video is having trouble loading and playing

Is it possible, using javascript, to detect if an embedded youtube video pauses playback in order to let the video buffer? I know that there are events that fire when the user presses pause, but I'm looking for an event that fires when the video pauses due to a slow connection. I'm creating a web application where it's important to have the video play through smoothly. If the video pauses due to a slow connection, I want to detect that.
Use this code player.getPlayerState():Number it seems like you are allowed to ask the player what status it is in so this may help you
https://developers.google.com/youtube/js_api_reference
There is also a state 'buffering' being fired when the player needs to buffer more data..in that case the video stops. I guess also 'error' state might be of help.
The solution I worked out is just to use the javascript API's onStateChange callback (https://developers.google.com/youtube/js_api_reference) to detect when the player is started for the first time and when it finishes playing at the end. When the player is started, I grab the current time. When it finishes, it sees how much time has elapsed. In my application, the user cannot pause the video, so comparing the elapsed time to the video length indicates if it paused for loading.

Resources