AVPlayer not play the next song when screen goes lock - ios

Im using AVPlayer to play song from URLs. I need to play songs continously when user play one song (all songs in the list one after the other) even when the screen doing to lock. But my problem is, when I play a song, after screen is lock current song is playing but the next song is not plays. This is my code.
Appdelegate.m
`
// Registers this class as the delegate of the audio session.
[[AVAudioSession sharedInstance] setDelegate: self];
// Allow the app sound to continue to play when the screen is locked.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);`
also set the info-plist Required background modes to App plays audio
If screen is not lock it plays allsongs one after the other. Only screen lock it plays the currently playing song only. How can I correct this issue.
Please help me,
Thanks

Related

Play music in background and only stop in certain instances

I have an app that shows videos in a timeline. The videos autoplay and by default they should be silent. If the user is playing music in the background, music should not stop at this time. It is only when tapping the video that it should actually pause the background music and let the video play. And once the video is done playing, the user's previous audio should continue where it left off.
I've tried using
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
but this lets the music mix with the audio even if the video is in full screen mode. Of course, I could ensure that when the play button is tapped, the music pauses, but that might not be the cleanest solution.
Any better ideas?

iOS background audio gets stopped when playback is finished

There are couple of questions already on SOW,about playing audio in background, but mine is a little different(as far as I could see!)
So I've a playlist in my app, where user can choose to play an audio in a loop, or a random/continuous playback for all the audios available in the app.
Using below code, and the required Info.plist setting:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
Info.plist setting:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
The audio works properly in the background, and even when screen is locked, but the problem arises when the audio playback gets finished.
Using NSLogs, I can see that the code reaches again the AudioPlayer's "play" method, but the audio doesn't play the next song, or the song which was to be played in the loop.
Does anyone have pointers?
Reading the documentation, the relevant paragraph:
My audios are stored locally in the app.
When the UIBackgroundModes key contains the audio value, the system’s media frameworks automatically prevent the corresponding app from being suspended when it moves to the background. As long as it is playing audio or video content or recording audio content, the app continues to run in the background.
However, if recording or playback stops, the system suspends the app.
So essentially, to hack around the setup, I used two AVAudioPlayers, and instead of waiting for the delegate callback for audioPlayerDidFinishPlaying:, I worked this out:
if ((audioPlayer1.duration - audioPlayer1.currentTime) < 1) {
//play Audio 2
}
Not too sure if there's any better suggestion than this?

MPMusicPlayerController Play music while app is in background

Hope this question doesn't get down voted - I haven't seen anything in the documentation.
I want to play music from the ipod library while the app is in the back ground.
I am creating the music player using the default mechanism.
-(void)viewWillAppear:(BOOL)animated
{
MPMusicPlayerController *myPlayer =
[MPMusicPlayerController applicationMusicPlayer];
// assign a playback queue containing all media items on the device
[myPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];
// start playing from the beginning of the queue
[myPlayer play];
}
As soon as the app enters the background the music stops playing. I would like it to continue playing though.
I do not want to use the iPodMusicPlayer option
How can I do this?
You should set background audio mode in the project settings (Project settings > Capabilities > Background modes > Audio and AirPlay) like it's shown on the screenshot below:
Also you should set Audio Session Category in the appDelegate (in the didFinishLaunching method). Example is shown below:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
I have same issue. I searched even after added flag in plist. It will not work because :
The applicationMusicPlayer does not support background music. Use
MPMusicPlayerController's systemMusicPlayer instead. It shares the
state with the built-in System player and music will continue to play
when your app enters the background.
Other Solution is :
You can use AVAudioPlayer to play either streamed or local(bundle) audio. It even allows you to play multiple audio file at the same time having an AVAudioPlayer instance for each.
Note : iPodMusicPlayer is deprecated. so Instead of this use systemMusicPlayer.

Playing a silent movie with AVFoundation and keeping iPod music playing

I have an app that plays silent Movies using AVFoundation. Is there a way to allow the music to play and your iPod (if you're playing music) to continue playing? If I play the movie will my iPod is on, the iPod pauses so the movie can play, and vice versa - if I try to replay the music while the movie is playing, the movie pauses. Basically, I want them to play continue playing seamlessly....
Let me know if you need applicable code to answer.
In my experience, for standard AVAudioPlayer based sound in an app, managing the AVAudioSession is required to allow background music:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
But I can't tell you if that's the same as attempting to play encoded/compressed Video and encoded/compressed audio at the same time.

Play video in UIWebview background on Airplay

I have an app that plays several webvideos like Youtube and Vimeo in a UIWebView. When the video plays it is possible to send it to an Apple TV over AirPlay.
Currently, during playback over AirPlay the video stops when I pause the app and move it to the background.
My desired result is that the video will keep playing on the Apple TV.
After some research I found out that I have to set the Required background modes in the Info.plist to App plays audio. Unfortunately this did not work.
So what does need to be set to keep the video playing over AirPlay when the app is moved to the background.
i've tested this on iOS6 with UIWebView loading video webpage, and it does the job. the audio session needed to be set as AVAudioSessionCategoryPlayback in order to be played in background.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

Resources