Global Audio Player for tabbed App - ios

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.

Related

Play the video in two view controllers from where the previous video stopped

I have a table view controller that has a video view in each cell.
Each cell has a play button, slider and a full screen button.
I have implemented logic for playing , pausing and seeking the video using AVPlayerLayer.
When I click on the full screen button I am moving to another view controller where I will play the video in full screen in portrait and landscape mode
When I move to the full screen controller I want the video to play from where it was playing in the previous view controller.
And when I move back to the previous view controller the video should play from where it played in the full screen controller.
How can I achieve this? Any help will be appreciated. Thank you
I have implemented same for an audio player which has a mini player and tapping on it will open in full screen.
I have detached audio player from UI. Created singleton class of player which will update UI. So there will be only one instance of player which will update multiple UI in sync.
You can implement same logic for your player.

How to hide only Next and Previous button in AVPlayer?

Is there any way to hide only Next and Previous buttons in AVPlayer while playing a video
You can only either hide them all or show them all via the AVPlayerViewController showsPlaybackControls property, so what I would recommend doing is using an open source or third party library or framework that controls AVPlayer for you.
Such as what's suggested by this related question, which ultimately points to this GitHub project. You can then remove or hide the controls you don't want to be visible.
Alternatively, you could create your own controls that call into, and control, AVPlayer.
=> you can get notification when ply or pause and stop
when you got notification of play at that time you can hide both next and preivios button

How to control background music

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

Using multiple MPMoviePlayerController instances in a UITabBarController based iOS app

In my iPad app, the user can open one MPMoviePlayerController in each tab (total of 5).
In each viewcontroller containing a movieplayer instance, I play the movie in viewDidAppear: method, and I pause it in viewWillDisappear: method. I also add/remove the viewcontroller as an observer for notifications so that only the viewcontroller currently visible is receiving the notifications. The movieplayer is setup in the init method, and there is nothing really special about it.
Note: Although you can create multiple MPMoviePlayerController objects and present their views in your interface, only one movie player at a time can play its movie.
Besides the note above, I cannot find anything about this in the docs, but I´m guessing that the movieplayer is shared between viewcontrollers somehow, becasuse when I force one movieplayer in each tab it works sometimes, and sometimes the movieplayer is finished when I return to the previous tab.
Is it a fact that MPMoviePlayerController are meant only handle one video at a time, no matter if the instances are in different classes/tabs?

How do I display video on external monitor while keeping controls on iPad?

My iPad app has the option to play videos. I use the MPMoviePlayerViewController class to play my videos.
My question is: if I want to play the videos on an attached external monitor, how do I keep the playback controls on the iPad like YouTube does? If I add the view of the MPMoviePlayerViewController 's player to the external screen's hierarchy I can play the video fine, but I now have no control over it. Is there a way to move or duplicate the view where the controls lie and place it on a view which resides on the iPad?
I'm not aware of an officially supported way of pulling out the original UI in this way. The MPMoviePlayerViewController only exposes the MPMoviePlayerController object it uses via its moviePlayer property. The MPMoviePlayerController in turn only exposes view and backgroundView, which aren't helpful for such a purpose. You could in theory inspect the subviews of the movie player's main view, find the playback controls and try to move them to the other screen. I have a feeling this will not end well though, as they're anything but static. You also never know what will happen in later iOS versions, or if they'll let your hack on the app store. It's probably less trouble to just re-do the UI yourself.
Actually controlling the video playback programmatically is straightforward - the view controller's moviePlayer implements the MPMediaPlayback protocol.

Resources