I am playing Music from Url using AVPlayer and clicking on the play button will take some time to play because of buffering. There are two cases that i have explained below.
Success case: I am clicking on the play button and the AVPlayer buffers and starts playing. After the player have started playing i am pressing the home button and the application goes to the background and the playing continues.
Failure case: I am clicking on the play button and the AVPlayer starts buffering. I will press the home button and the application goes to the background while the AVPlayer is buffering and before its starts playing. AVPlayer doesn't play in the background even though play command is already issued.
and suggestions?
Thanks in advance,
Norbert
if the buffering takes more than 2 seconds, the task will be frozen before playback starts.
use
task = [application beginBackgroundTaskWithExpirationHandler:...
the second problem is that AVPlayer's status fails if trying to start playing in the background if you don't acquire the remote controls.
[application beginReceivingRemoteControlEvents];
One of those should fix the issue.
Related
I can't seem to make iOS show the correct play / pause button in the remote audio controls. I do receive the remote control events and set all values of the nowPlayingInfo dictionary.
Everything works fine and I even see a cover photo on the lock screen. Except the pause/play button. It always looks like pause even if my AVAudioPlayer is playing. It sends a pause event regardless of playback state.
How can I notify iOS that AVAudioPlayer is paused and that it should now show a play button in the remote control buttons bar?
Make sure that you're setting the MPNowPlayingInfoPropertyPlaybackRate property. 0.0f to indicate paused, 1.0f to indicate playing. You'll also need to set the MPNowPlayingInfoPropertyElapsedPlaybackTime when you change these values.
Here is example code where updateMetadata is a function that applies those changes to the MPNowPlayingInfoCenter.nowPlayingInfo dictionary. This would indicate to the center that the player is paused.
[self updateMetadata:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:audioFile.player.currentTime],
MPNowPlayingInfoPropertyElapsedPlaybackTime,
[NSNumber numberWithFloat:0.0f],
MPNowPlayingInfoPropertyPlaybackRate,
nil]];
I had this problem today: I found that the fact that I was recording audio as well as playing it caused the button to show the pause symbol.
When I stopped the AVAudioRecorder from recording the pause button became a play button.
Quite often, the problem is simply the iPhone Simulator. As soon as you are using the play() function of your AVAudioPlayer instance, the remote control bar is supposed to toggle pause/play automatically. If you run into problems where this doesn't happen, try to run your program on a device.
To toggle the button, you do not need to set any playingInfo of the MPNowPlayingInfoCenter, neither do you need to hold an active AVAudioSession.
Here is example code where updateMetadata is a function that applies those changes to the MPNowPlayingInfoCenter.nowPlayingInfo dictionary. This would indicate to the center that the player is paused.In swift
self.updateMetadata(NSDictionary.dictionaryWithValuesForKeys(NSNumber(Double(audioFile.player.currentTime))),MPNowPlayingInfoPropertyElapsedPlaybackTime,NSNumber(numberWithFloat:0.0f),MPNowPlayingInfoPropertyPlaybackRate,nil)
I have an app that shows list of movies in a table view. When I play them one after another on device it works just great. But when I switch to Apple TV over AirPlay it doesn't work anymore. It play's the first video on ATV ok but after a switch to the next video the screen on ATV start blinking/flickering and after a few seconds it falls back playing on a device. I am using MPMoviePlayerController for playing stream videos.
I also found out that if previous video finished playing over AirPlay it tries to start the next one over AirPlay also. Is this intended behaviour?
Is this kind a related with property allowsAirPlay?
I think I've found a solution. Before you switch playing another video you should stop the previous one:
[self.moviePlayer stop];
This is not needed if you are not playing via AirPlay cos the next video will automatically stop previous one by nature - you can not play two videos at once.
But If you do play video over AirPlay you need to stop previous one first and than play the next one.
This solution works for me.
I have a UIWebview which will play a YOUtube video playlist, and I need to handle the events when user click DONE button and close the webview.
So far, the working solution i found is to handle UIMoviePlayerControllerDidExitFullscreenNotification, but when UIWebview finish playing a video in the list it will exit the fullscreen mode and enter back to fullscreen mode to play the next one. My app handles the event of exiting fullscreen as pressing DONE button and only the first song of the list get played.
My question is that is there anyway to handle DONE button click events without handling UIMoviePlayerControllerDidExitFullscreenNotification, or to handle those two events separately?
I am using MPMoviePlayerController to play streaming audio. I'm trying to get background audio working correctly. Right now, audio continues to play when you exit the app - the lock-screen and multi-tasking bar controls even work.
When a song finishes, the app is supposed to advance to the next track and play it. It works when the app is open but not when it is in the background (a song finishes but does not advance to the next track). If a song finishes and you re-open the app, however, the next song will start up immediately.
I am currently using NSNotificationCenter to keep track of when tracks end to advance to the next track (in my app delegate). Again, it works like a charm when the app is open. Is there a better way to do this to keep audio playing after a song is done?
I had this issue lately. Hope the answer helps other people.
If you have a playlist for example and want to play the next song while in background mode or lock mode add this line of code on your viewDidLoad:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
This makes the app supports remote control events.
When you're watching a video with MPMoviePlayerController and the user presses the top button to lock the screen, the app goes to sleep and so does the sound from the video.
Is there any way to prevent the lock from stopping the sound? If not, is there a way to intercept the lock, to create a "custom lock", to save some battery but keep playing the video?
Sounds like you haven't set your audio session category. Set the AVAudioSession's category property to AVAudioSessionCategoryPlayback to indicate that the app's major purpose is to play back audio, and it therefore should ignore the screen lock button and ring/silent switch. If you're working with the lower-level C API, you'll be using AudioSessionSetProperty(kAudioSessionCategory_MediaPlayback), but it's the same concept.