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.
Just Create a Media Player as you need. Add the following code in ViewDidLoad.
- public override void ViewDidLoad()
- {
- base.ViewDidLoad();
- //To listen changes in lock screen
- UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
- //creating an audio player with url
- mediaplayer = new AVPlayer(new Uri("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"));
- //make audio play when app goes background
- AVAudioSession.SharedInstance().SetActive(true);
- AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Playback);
- //Audio player Notification in lock screen
- MPNowPlayingInfo nowPlayingInfo;
- nowPlayingInfo = new MPNowPlayingInfo();
- nowPlayingInfo.Artist = "An OK Band";
- nowPlayingInfo.Title = "Our First Single!";
- // Register for receiving controls from lock screen and controlscreen
- 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)
- {
- //Something you need to do.
- }
- else if (theEvent.Subtype == UIEventSubtype.RemoteControlNextTrack)
- {
- //Something you need to do.
- }
- else
- {
- //Something you need to do.
- }
- }
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.

Philippe DuvivierPosted Apr 8, 2018, 3:40 AM
Hey, thanks for this great article. I have built my own player using an AudioQueue. Step2 works ok. I am wondering where should I override RemoteControlReceived.
Sagar Pandurang KapPosted Dec 29, 2017, 3:35 AM
Cool yarr.nice ae=rticle..