Step 1
Create a project in Xamarin iOS. New Project => Single View App. Give the Name
Step 2
Just Create a Media Player as you need. Add the following code in ViewDidLoad.
- public override void ViewDidLoad()
- {
- base.ViewDidLoad();
-
-
- UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
-
-
- mediaplayer = new AVPlayer(new Uri("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"));
-
-
- AVAudioSession.SharedInstance().SetActive(true);
- AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Playback);
-
-
- MPNowPlayingInfo nowPlayingInfo;
- nowPlayingInfo = new MPNowPlayingInfo();
- nowPlayingInfo.Artist = "An OK Band";
- nowPlayingInfo.Title = "Our First Single!";
-
-
- MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = nowPlayingInfo;
- }
Step 3
Now we have to listen to changes in that media player notification as the user taps on next, play/pause, or previous.
- public override void RemoteControlReceived(UIEvent theEvent)
- {
- base.RemoteControlReceived(theEvent);
- if (theEvent.Subtype == UIEventSubtype.RemoteControlPreviousTrack)
- {
-
- }
- else if (theEvent.Subtype == UIEventSubtype.RemoteControlNextTrack)
- {
-
- }
- else
- {
-
- }
- }
Step 4
For each time the audio or video has changed, we have to update in "nowPlayingInfo"
Step 5
That's all set. Now we are able to control our audio from the lock screen.