Pausing 3rd party media players like Spotify or Youtube Music

JD Dayhuff 1 Reputation point
2021-04-06T20:23:04.67+00:00

I am very new to Xamarin and Visual Studios as I am kind of learning to code while I create this project. I am trying to make an app where you press a start button and it pauses any background media player such as Youtube Music, plays an MP3 file, then skips to the next song and plays it, and repeats this every minute for 60 minutes. Any help would be appreciated on how to code in Xamarin to pause, skip and play any background media.

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

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,711 Reputation points Microsoft Vendor
    2021-04-07T08:01:35.097+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    In xamarin forms, we don't have the common api to achieve this.But we can try to use DependencyService to call the loacal fuction in seperate plarform(android, ios and uwp ).

    In android,

    You can use DependencyService and AudioFocus to achieve it, when you record the audio, you can create interface in PCL.

      public interface IControl  
    {  
       void StopBackgroundMusic();  
    }  
    

    When you record the audio, you can executed the DependencyService with following code.

     private void Button_Clicked(object sender, EventArgs e)  
        {  
            DependencyService.Get<IControl>().StopBackgroundMusic();  
           //record the audio  
        }  
    

    In android folder, you can create a StopMusicService to achieve that.

    [assembly: Dependency(typeof(StopMusicService))]  
    namespace TTSDemo.Droid  
    {  
      public  class StopMusicService : IControl  
        {  
            AudioManager audioMan;  
            AudioManager.IOnAudioFocusChangeListener listener;  
      
      
            public void StopBackgroundMusic()  
            {  
      
                audioMan = (AudioManager)Android.App.Application.Context.GetSystemService(Context.AudioService);  
                listener = new MyAudioListener(this);  
                var ret = audioMan.RequestAudioFocus(listener, Stream.Music, AudioFocus.Gain);  
      
            }  
        }  
      
        internal class MyAudioListener :Java.Lang.Object, AudioManager.IOnAudioFocusChangeListener  
        {  
            private StopMusicService stopMusicService;  
      
            public MyAudioListener(StopMusicService stopMusicService)  
            {  
                this.stopMusicService = stopMusicService;  
            }  
      
            public void OnAudioFocusChange([GeneratedEnum] AudioFocus focusChange)  
            {  
              //  throw new NotImplementedException();  
            }  
        }  
    }  
    

    Refer: https://stackoverflow.com/questions/60461679/pause-background-service-in-xamarin-forms

    In additon, we can also try to use MediaBrowserService to achieve this. When you use a MediaBrowserService, other components and applications with a MediaBrowser can discover your service, create their own media controller, connect to your media session, and control the player. This is how Wear OS and Android Auto Applications gain access to your media application.

    For more details, you can check document :

    https://developer.android.com/guide/topics/media-apps/audio-app/building-an-audio-app

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    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.

    0 comments No comments

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.