Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin Forms w/ .NET 2.0 Standard - Play Background Audio?

PodsterPodster USMember ✭✭

Is there a way to play Audio (and background playing) Files in Xamarin Forms with code in the .NET 2.0 standard project? Without coding each iOS or Android platform specific?

Thank you.


  • AlexPAlexP Member ✭✭

    I don't think that there is a built in way to do that,
    But I think that what you are looking for might be this article by Martijn van Dijk:
    Play Audio and Video with the MediaManager Plugin for Xamarin

  • PodsterPodster USMember ✭✭

    I tried that Plugin unfortunately it does not work with Xamarin .NET Standard yet. Thanks.

  • AlexPAlexP Member ✭✭

    Sorry about that,
    I didn't realize it is not suitable for .Net Standard.

    Anyway, as I've mentioned, I don't think that there is a built in way to do that,
    But if you are willing to give it a go on each platform,
    I've wrote a blog post about using ExoPlayer in Xamarin Forms Android.

    The demo I'm providing is for video, but I don't see a reason it shouldn't work for audio as well.
    You'll probably just need a different source for the media.
    The good thing is it that it continues to play in the background by default.

    For iOS I would recommend you to use AVAudioPlayer,
    But unfortunately, I don't have a working example of that.

    Good luck! :smile:

  • PodsterPodster USMember ✭✭
    edited February 2018

    Thanks I will check out your Android blog post.

    For the iOS specific I am using below code:

    var url = NSUrl.FromFilename("Sounds/Coolplay.mp3");
    NSError err;
    var audioPlayer = AVAudioPlayer.FromUrl(url, out err);
    // Play Audio while in Background
    var audioSession = AVAudioSession.SharedInstance();
    try {
  • SamPakbazSamPakbaz USMember, University ✭✭

    I am also looking into for an online radio app I am wring and have exact same issue as Podster has. I need audio streaming and background music features for both iOS and android. a simple tutorial for background streaming (which includes Metadata support, background audio and media notification manager the same way MediaManager handles it seems to be non-existent)
    While platform specific implementation seems like the only way to go for the moment (until MediaManager plugin version 1 comes out) do you think AVPlayer has network streaming limitation? because in the xamarin developer portal says use this unless you want to stream remotely from network. and exoplayer does it need any dependency for Xamarin.Forms?

  • AlexPAlexP Member ✭✭

    Hi @SamPakbaz ,
    I'm really not sure about AVPlayer, but if the xamarin developer portal says so, who am I to argue? ^^

    Regarding the ExoPlayer, I don't recall any dependency on Xamarin Forms. It is a pure Android implementation which is wrapped in a C# library.

  • SamPakbazSamPakbaz USMember, University ✭✭

    @AlexP I could manage to use AVPlayer for remote streaming thanks to some help from some swift code I found on some other forums and great documentation on Xamarin.iOS api and it seems to be working very well. I also added few tweaks to handle network interruption. I will post the whole source code on github wheneve I finish the android part.
    Speaking of android part I looked into different solutions including @JamesMontemagno source codes for part 1 through 3 of android streaming . they seem to be the way to go except number 3 which seems to be the most thoroughly implemented have some performance issues on the device I am running. not sure its because of using appcompat or what and again I am running .Net standard 2.0 environment and don't want to change that to old PCL profiles frankly because I already implemented UWP and iOS natively and works well.
    My question for you is your ExoPlayer Code which seem to use @Martijn00 ExoPlayer nuget is also written for PCL Profile not Net Standard. IS there a work around you know to use ExoPlayer C# codes without using that nuget?

  • AlexPAlexP Member ✭✭

    Hi @SamPakbaz ,
    I'm not sure that I understand the problem you are facing with the Exo Player library.

    In my example, I am using a .Net Standard project for my main Xamarin.Forms project,
    And the ExoPlayer package is used only in the Android project.
    It should be working fine. :smile:

  • SamPakbazSamPakbaz USMember, University ✭✭
    edited March 2018

    @AlexP Thanks for quick reply. Sure when I tried to install exoPlayer I was getting nuget version error specifically for Xamarin.AppCompat libraries. I even went ahead and manually update project file to include those versions but didn't work for me. I don't know why I was getting those issues. but good news is I managed to finish my own version and I uploaded code to github for others like myself until MediaManager Plugin version with .NET Standard 2.0 comes out:

    (I can't post the link for some reason)

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭


    Hi! I am interested in learning how to play music and audio on iOS. I have downloaded and am reading the solution you have kindly posted on GitHub.

    So far, I have looked only briefly at the code, but I can't see where you handle being able to play when the application is sent to the background (e.g. when the screen goes off). According to the page linked below, if audio plays longer than ten minutes, the application must register as a Background Necessary Application. Does your code handle these things?

    iOS Backgrounding Guidance

    Your advice would be greatly appreciated.

    Kind wishes ~ Patrick

  • SamPakbazSamPakbaz USMember, University ✭✭


    For your question answer is yes it does. if you look at info.plist this line will take care of that:


    Also additionally, whats important is to receive notifications from lockscreen playback controls (play, pause in this case only because its live stream) which my application does that as well using MPRemoteCommandCenter.Shared (line 34 audioplayer.cs)

  • AlenTomaAlenToma USMember ✭✭

    @SamPakbaz, Is it possible to modify it to play mp4 or dose it already support it

Sign In or Register to comment.