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.

Is it possible for MediaPlayer setDataSource to play incomming stream?

I was wondering, if it would be possible for the setDataSource to play incomming stream of parts of music each time in a loop, and not a whole file?

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    If you want to play continue playing media, start a Service and control the MediaPlayer instance from there. You need to embed the MediaPlayer in a MediaBrowserServiceCompat service and have it interact with a MediaBrowserCompat in another activity.

    MediaPlayer player = new MediaPlayer();
    player.SetAudioStreamType(Stream.Music);
    
    player.Prepared += (sender, args) => player.Start();
    await player.SetDataSourceAsync(ApplicationContext, Android.Net.Uri.Parse(Mp3));
    player.PrepareAsync();
    

    Tutorials:
    https://developer.android.com/guide/topics/media/mediaplayer#mpandservices
    https://devblogs.microsoft.com/xamarin/background-audio-streaming-with-xamarin-android/

  • mtsABCmtsABC Member ✭✭

    But this is to play music in background, I get a System.IO.Stream from another device and would like to play it directly into the setDataSource. Something like this

    System.IO.Stream mmInStream;
    player.SetDataSource(mmInStream);
    
  • mtsABCmtsABC Member ✭✭

    I tried reading with MediaDataSource class like this

    byte[] myReadBuffer = new byte[10000];
    player.Prepared += (sender, e) =>
    {
        player.Start();
    };
    player.SetDataSource(new StreamMediaDataSource(new System.IO.MemoryStream(mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length))));
    player.Prepare();
    

    But this returns the

    12-20 15:55:51.429 W/dalvikvm(32750): Unable to resolve superclass of Lmd5c539bdc79f76d0c80e6cd44011eba829/StreamMediaDataSource; (388)
    12-20 15:55:51.429 W/dalvikvm(32750): Link of class 'Lmd5c539bdc79f76d0c80e6cd44011eba829/StreamMediaDataSource;' failed
    
Sign In or Register to comment.