Control the application from control center in iOS - ios

I am using AVAudioPlayer in my iOS application and the following functionalities have been given.
1. Play
2. Pause
3. Next
4. Previous
My requirement is, I want to give these functionalities from control center. Is it possible?

Control Center uses same API as lock screen. The audio controls appear on lock screen when a properly configured AVAudioSession is active. In addition to that you need to use MPNowPlayingInfoCenter API to set correct media information.
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc]initWithImage:albumImage];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = #{
MPMediaItemPropertyTitle : aSong.songTitle,
MPMediaItemPropertyArtist : aSong.artistName,
MPMediaItemPropertyArtwork : artwork,
MPNowPlayingInfoPropertyPlaybackRate : 1.0f
};
See the questions below for more information:
Why do the Lock Screen audio controls disappear when I pause AVAudioPlayer?
AVAudioPlayer on Lock Screen

Related

Build Command-Center like Media Controls

I want to build a control in my app that can play/pause and skip the sound that is played in the background. This should behave the same way as the media control does for all different media sources at the control center.
Screenshot
I played around with various options. AVAudioSession, MPRemoteCommandCenter and also acquiring some Bluetooth profiles, as headphones can play/pause background music as well. Unfortunately nothing was able to play/pause the background music.
Does anyone have an idea how to make this behavior happen.
If you are trying to make your audio app use control center (like spotify does) you need to use the MPNowPlayingInfoCenter to set the now playing item data (like:title, rate, duration, elapsedTime,...) it will be something like that:
MPNowPlayingInfoCenter.default().nowPlayingInfo = [
MPMediaItemPropertyTitle: title,
MPMediaItemPropertyArtist: artist,
MPNowPlayingInfoPropertyElapsedPlaybackTime: position,
MPMediaItemPropertyPlaybackDuration: duration,
MPNowPlayingInfoPropertyPlaybackRate: rate,
]
this will set the data of the played audio item in the media control center now in order to be able to use the controls button need to use the MPRemoteCommandCenter and set the target for each command you want to use for example for play/pause actions it can be done like that:
MPRemoteCommandCenter.shared().playCommand.addTarget(handler: playActionHandler)
MPRemoteCommandCenter.shared().pauseCommand.addTarget(handler: pauseActionHandler)
once all of this is done you will need to call the method bellow in order for your app to be able to receive the remote events and execute the needed action
UIApplication.shared.beginReceivingRemoteControlEvents()

How to allow user to control current audio duration from LockScreen or ControlCenter in iOS?

I allow user to play audio tracks inside my app.I have already set controls but I have problems of setting slider of current audio playback on the lock screen.Here is the screenshot of it :
How to set current song playback and allow user to change it using song slider on the lock screen?
Have you gone through MPRemoteCommandCenter methods and properties, I believe you can use the commands here to skip forward or backword.
I am afraid you won't get a smooth slider similar to iTunes music as they have not exposed anything like that for developers.
And for setting track duration on lock screen you have to set MPMediaItemPropertyPlaybackDuration like below::
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSDictionary *songInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:songDuration],MPMediaItemPropertyPlaybackDuration,
nil];
[center setNowPlayingInfo:songInfo];

youtube-ios-player-helper not showing iOS like video player controls

I am using https://github.com/youtube/youtube-ios-player-helper to play videos from youtube in my app.
I am also using different playvars to control what to and what not to show on the player. But the controls section I see on the player is not like iOS.
I tried in other sample app where I could get the proper controls.
NSDictionary *playerVars = #{
#"playsinline" : #1,
#"showinfo":#0,
#"autohide":#1,
#"modestbranding":#1
};
[self.playerView loadWithVideoId:self.videoId playerVars:playerVars];
I played with few other options with these vars but no luck. I am not sure what controls what type of controls are displayed from apps perspective.
Tracked down the issue. It was the custom user agent I was setting for my app, which was causing youtube player to behave differently.

How to play YouTube video in vertical mode?

YouTube player update allow vertical video can display fullscreen in vertical mode. I was asked to fit YouTube video in a vertical view (not a full screen mode) in our iOS app.
We use YTPlayerView to playing YouTube video, but it doesn't provide any method to check if a video is vertical. I guess we can query such the info from YouTube web API, but I couldn't find any thing.
Has anyone tried to do the same thing like mine?
If it's not fullscreen mode then:
NSDictionary *playerVars = #{
#"playsinline" : #1,
};
[self.playerView loadWithVideoId:#"VIDEO_ID" playerVars:playerVars];
Just adjust the view's position etc, it will play just fine. Edit the html file they provide so the background matches your app's etc if you don't like the black.

How do I remove the airplay from a video player for the ipad?

So I have this player that I am using to video locally. I need to be able to mirror airplay the screen to a tv but every time I set airplay to mirror the player just sends the whole video to the screen instead of just mirroring.
https://github.com/NOUSguide/NGMoviePlayer
I contacted the developers about how to disable the airplay part of it. They told me to remove the airplay layer from the player. Can someone lead me into the right direction into getting this done?
If you want to disable AirPlay, just set the allowsAirPlay property to false in your MoviePlayerController object.
You can access the mirrored screen by using this method :
UIScreen * screen = [[UIScreen screens] objectAtIndex:1];
before calling this method, ensure that the screens property has more than one object

Resources