notification when AVPlayerItem is not stalled - ios

There is a notification for when the player has stalled, called AVPlayerItemPlaybackStalled, but there doesn't seem to be one for when the video is playing again. What's a good way to know that the video has unstalled? I could look for when the player current time changes but that sounds not great...

Related

How to play longer music as Notification alert?

I want to set a music which is longer than 30sec as notification soound. But after searching about this I came to know that it is not possible to add these type of music as notification sound. notification sound must be in 30sec longer otherwise it will not play music. Now how can I add more than 30sec longer music as notification sound?
I had a similar issue for my alarm app as well. What I did was a workaround when your app is in background. You play a music file in your code in background and increase the volume of the device to maximum. You can play as much longer music file as you want. Stop playing the music when someone clicks on the notification.
The only problem with this is that your app should not be killed or terminated.

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.

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.

iphone-loading sound files quickly

I have a space shooter game I am making. I want to play a sound every time a user hits the button fire. This is a short .wav file of about half a second in length. Also, when the user dies, I have an explosion .mp3 file to play. Since the fire button can be clicked very rapidly, I need to play sounds quickly. I used some avaudio player code previously where I make a new audioplayer every time I need to fire, but this crashes occasionally if I fire very quickly. Any suggestions on ways to play sound files very quickly? (including some example code in you answer would be great)
if you using MPMediaPlayback you can try prepareToPlay

MPMusicPlayerController: combine iPodMusicPlayer and applicationMusicPlayer to prevent continuing to next track

I have a bit of a strange problem. I have a music app that uses the [MPMusicPlayerController iPodMusicPlayer]. Everything is fine, notifications are fired for track changes and changes in playback state.
I have one screen where the user needs to review one single song, I don't want him to go on to the next song in his queue. Since there is no delegate method for when a track WILL change (only DID change), to prevent the music player from continuing to the next track I use a new [MPMusicPlayerController applicationMusicPlayer], give it iPodMusicPlayer's currently playing song and all is well. No new tracks to continue to, and I'm not touching the original iPodMusicPlayer queue so in theory, when I close this screen and use the iPodMusicPlayer again, all should be perfectly fine.
However, when the user is done on this screen and closes it, iPodMusicPlayer is now suddenly broken, notifications are not called and when I put the app to the background, music stops playing, causing me to believe that iPodMusicPlayer is now actually applicationMusicPlayer.
Okay so my question is basically: I need a way to prevent the music player to continue on to the next track in the queue. Switching to applicationMusicPlayer with one track seems to break stuff, as explained above. What's the best solution?
EDIT: because this might be a bit difficult to understand, I created a small project to show the problem: https://github.com/kevinrenskers/MPMusicPlayerControllerTest. Open the app while music is playing, see that the play button behaves correctly. Now open the popup, close it again and the play button is broken.
I found a solution to my problem: set the repeatMode to MPMusicRepeatModeOne and then catch the MPMusicPlayerControllerNowPlayingItemDidChangeNotification notification. You can stop the playback and you never continue to the next track. Once I'm done with the second screen I reset the repeatMode to the original value.

Resources