I am developing an app with a background music and the music starts when the first view (INTROViewController) appears (it is implemented in viewDidLoad). I have created a settings page (OPTIONSViewController) where I have implemented a switch to turn the music on and off. In OPTIONSViewController I have created an instance of INTROViewController where all the audio files and the commands to play and stop the music are defined. I don't get any errors but it seems the instance is not able to control the music at all, which continues playing. What am i doing wrong? Cheers
I think u can have just one AVPlayer on your AppDelegate.
Declare it like a property, and then u can get this player from all your application
with something like this:
yourAppDelegate* appDele= (yourAppDelegate*)[[UIApplication sharedApplication]delegate];
AVPlayer * tempPlayer = [appDele yourPlayer];
This should fix your problem :D
Related
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.
here is my problem:
I've got an app playing audio files, updating the lockscreen info via MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo, and this part is working fine.
But in an other view, i'm playing a video with AVPlayerViewController and AVPlayer, and when the video starts playing, it's updating the lock screen automatically, with nothing except the video duration.
I didn't find anything about this behaviour in Apple's documentation, I can't find a way to disable it.
So far, I've tried calling UIApplication.sharedApplication().endReceivingRemoteControlEvents() before the video starts playing, and beginReceivingRemoteControlEvents() after. It doesn't work.
Does anyone know a way to prevent this?
Starting with iOS 10 there is a BOOL property in AVPlayerViewController called updatesNowPlayingInfoCenter, that has the default value: YES. Just change it to NO:
//playerController is an instance of AVPlayerViewController
if ([self.playerController respondsToSelector:#selector(setUpdatesNowPlayingInfoCenter:)])
{
self.playerController.updatesNowPlayingInfoCenter = NO;
}
I have an application that plays video using AVPlayer and AVPlayerLayer. In my app, I'm able to play audio when the app is locked by setting the player property of my AVPlayerLayer to nil when the application enters the background:
-(void)appEnteredBackgroundNotification:(NSNotification *)notification {
[[self playerLayer] setPlayer:nil];
}
However, when I do this, the audio will lag/blip for around 0.5 seconds. This sounds really really bad for the end user. Same goes for when the app enters foreground and I re-set the player property.
How can I avoid this audio blip? As a test I've tried removing the player in a background thread to no avail.
Update: I spoke with an Apple engineer at WWDC and they said that this issue is a bug on their end (so far not fixed in iOS 9) and this approach is the correct approach. Great...
I think may not you call pause before setting to nil and vice versa. And, try calling prepare before play.
after days of research I came to the conclusion that there is no other way than to ask here directly.
I have an tabbed application with several UITableViewControllers inside. When I click on a cell in one of these TableViewControllers there should be played some audio streams. This is working so far using MPMoviePlayerViewController. But when I click on "Done" in the player, the audio stops to play.
What I need is to be able to start the audio track, and when I click "done" it should not stop. Also, there should be a way to get back to the player.
What I was thinking is to make something like a "global view" with an integrated player and all other views in the app should use this global view to play the selected audio.
So, I created an UIView which has a method playAudio that starts playing in a MPMoviePlayerViewController. But what is the way to go to this view from another. Right now I am just calling this method directly and before that I added
[self.navigationController pushViewController:mediaPlayer animated:YES];
but I think I am on the wrong way here. What I want to achieve is to play audio in a player, that stays in background when clicking on "Done" in the player. And this player should be reached from every view (e.g. through a UIButton in each tab bar View) - just like most radio applications and music apps do.
Would appreciate any help.
For this purpose, you should not use MPMoviePlayerViewController. You can create your own audio player with view and controls, or use third party libraries (search on Google there are plenty of them). Good Luck!
You should not use MPMoviePlayerViewController to play audio because it's designed to play a movie. I recommend using AVPlayer to play audio (if you only care about local audio and you're not streaming from the Internet, AVAudioPlayer is OK too.)
When you play your audio with AVPlayer, it will continue to play even when you app goes into the background.
The view hierarchy of your app should look something like this:
Root View Controller
Tab Bar controller
Your view controllers go here
A "Now Playing" view controller
A "now playing" view controller will be a view controller that you can put audio controls and display what is playing to your user. You can make your now playing view controller a property in the app delegate or in your root view controller so that you can access it easily. For instance:
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
[self.navigationController presentViewController:appDelegate.nowPlayingViewController animated:YES completion:nil];
You can add this code to a button for any of your view controllers that needs to display the Now Playing screen.
I'm playing music with an AVPlayer. Now at a certain time a NSTimer fires and I'dlike to fade over to another track. So I start fading out my AVPlayer and create a new AVPlayer instance to play the next song.
When on foreground this works as expected. But when my app is on background. The playing track fades out but the new AVPlayer instance does not start playing. Is it just not possible to create a new AVPlayer instance on background? or how can I make it play? Or is there another way to overlap two tracks?
I could do the playback with AVQeueuPalyer, but then I can't let tracks overlap. Any suggestions?
-- EDIT --
If it was not clear, I am able to play background audio as long as I want. Just creating a new AVPlayer instance in background does not work.
The correct way to do what I wanted seems to be AVMutableComposition. With that I don't need multiple AVPlayers and a few other benefits. For more details: I summarized it in a blogpost: http://www.postblog.me/2012/03/playing-multiple-overlapping-audio-tracks-in-background/
Try adding a key named "UIBackgroundModes" (array) to your app's Info.plist and add the value "audio" inside it. Then call
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
And then you should be able to play audio in the background (you should link to the AVFoundation framework).