Add audio controls to the lock screen and notification panel (Android and iOS).

Elizabeth James 1 Reputation point
2022-03-28T07:31:41.063+00:00

I'm using AVAudioplayer for iOS and Media Player for android, for playing audio. How can I add audio controls to the lock screen and notification panel in the device?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,334 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 41,601 Reputation points Microsoft Vendor
    2022-04-01T02:25:01.713+00:00

    Hello,

    For Android, you can refer to Lock Screen Music Controls in Xamarin.Android and Background Audio Streaming with Xamarin.Android, written by James.

    For IOS, you can refer to the following steps:

    Step1. Set the background mode for your player App.

    Step2. Setting your player in your ViewDidLoad method:

       public override void ViewDidLoad(){  
       	base.ViewDidLoad();  
       	UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();  
               mediaplayer = new AVPlayer(your URI);  
       	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. 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.  
       	}  
       }  
    

    In fact, Xamarin has a more simple way to implement this function.

    You can refer to Play Audio and Video with the MediaManager Plugin for Xamarin which was written by Xamarin MVP, Martijn van Dijk.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.