Search code examples
c#androidxamarinmvvmcross

How to Stream Audio (Radio ) MvvmCross


I am having some difficulty stream online radio in Android Using MvvmCross. I found the plugin Xamarin MediaManager and tried to go that route.

Here's the code:

 public IMvxCommand ListenCommand => new MvxCommand(Play);
    private void Play()
    {
        CrossMediaManager.Current.Play("http://ic2.christiannetcast.com/wayg-fm");
    }

In my axml, there is a button bound to that command and pressing it outputs this in the console:

[MediaPlayer] Couldn't open http://ic2.christiannetcast.com/wayg-fm: java.io.FileNotFoundException: No content provider: http://ic2.christiannetcast.com/wayg-fm

I've tested it with several different links just to make sure that wasn't't the case. Also, I've made sure to have <uses-permission android:name="android.permission.INTERNET" /> in my AndroidManifest.xml

I've also tried using Android's MediaPlayer, but I get the exact same result.

Please let me know if there is something I am missing. I haven't found any solutions online regarding this issue. Thanks!

EDIT

I am running this on an Android emulator, not a real phone. Don't know if this makes a difference.


Solution

  • [MediaPlayer] Couldn't open xxx java.io.FileNotFoundException: No content provider: xxx

    void setDataSource(String path):

    Sets the data source (file-path or http/rtsp URL) to use.

    @param path the path of the file, or the http/rtsp URL of the stream you want to play

    setDataSource(Context context, Uri uri):

    Sets the data source as a content Uri.

    @param uri the Content URI of the data you want to play

    which assumes URI to be of some form of ContentProvider

    Solution:

    Change your MediaPlayer SetDataSource method from:

    mediaPlayer.SetDataSource(context, Android.Net.Uri.Parse(url));
    

    To:

    mediaPlayer.SetDataSource(url);
    

    I test it on my side and it works fine:

    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.Reset();
    mediaPlayer.SetDataSource("https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3");
    mediaPlayer.Prepare();
    mediaPlayer.Start();