Detect end of playback on lock screen - AVPlayer - ios

I've the array of audio file and would like to play them consecutively. Is there any way to detect when AVPlayer ending playing on lock screen so that I could call a completion handler and play next sound? I want to call nextPlayAudio() from my PlayerViewController class. I am fire notification after finishing current playback audio but nextPlayAudio() not get called after finishing.

Use the AVPlayerItemDidPlayToEndTime notification to manage this.
Example as a service here: https://github.com/noreasonprojects/ModernAVPlayer/blob/develop/Sources/Core/Services/PlaybackObservingService.swift
| Check the PlayingState file, where the service is used.

Related

Detecting When An Apple Music Song Has Finished Playing

I'm writing an app that needs to enact an action when a song has finished playing. I'm using MPMusicPlayerController.applicationQueuePlayer() as my music player to play the user's apple music. I was wondering if there was a way I could detect when a users song has finished playing or when the queue has finished (either would be helpful)
MPMusicPlayerController has an instance method beginGeneratingPlaybackNotifications(). There are three Notifications that will be delivered to subscribers:
MPMusicPlayerControllerNowPlayingItemDidChange
MPMusicPlayerControllerPlaybackStateDidChange
MPMusicPlayerControllerVolumeDidChange
To detect when playing a song or a queue has finished, you can use MPMusicPlayerControllerNowPlayingItemDidChange.
When you receive that notification, check the MPMusicPlayerControllers nowPlayingItem (see Documentation). If the song finished and another one is played nowPlayingItem will have changed. If the whole queue finished and there is nothing playing, nowPlayingItem will have a value nil.

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.

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

iOS: AVQueuePlayer does not indicate that it has stopped playback

I have an AVQueuePlayer which loads URLs to play audio files and it works well for the most part. However, I have run into a problem where after the player finishes playing a file (with another file in the queue), it will simply stop playing. Normally, the app would be able to use the player's rate, status, and items. In this case, I have gone through with the debugger and everything looks normal.
Everything appears to be playing, except for the player itself. After forcing the player to play, the player will skip to the next track, indicating that the AVPlayerItem it had was not loaded (I can confirm the audio urls are valid).
Does anyone have any ideas how I catch this programatically?
You need to have the delegate method:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
Once this method is finished, start the new audio.

is that UIWebView player a MPMoviePlayerController instance, and how to get a reference to that instance?

I need to play a youtube video in UIWebview and handle the event when user click the DOne button and when the video finish playing. I think of the solution that will handle the event that sent from UIWebview's media player.
My question is:
1. is that the UIWebview's media player a MPMoviewPlayerController?
2. How do i refer to that instance and handle the event it sent out (such as: pause, stop, done, finish playing...)
Thanks.
1. is that the UIWebview's media player a MPMoviewPlayerController?
No.
2. How do i refer to that instance and handle the event it sent out (such as: pause, stop, done, finish playing...)
As drafted in my answer to a very similar question:
How to receive NSNotifications from UIWebView embedded YouTube video playback

Resources