user5699258
user5699258

Reputation:

Play audio url using xamarin MediaPlayer

Why xamarin MediaPlayer (on Xamarin.Android) can play audio as a stream from a link like this (mediaUrl1) : https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3

But can't do it from a link like this (mediaUrl2): http://api-streaming.youscribe.com/v1/products/2919465/documents/3214936/audio/stream

private MediaPlayer player;
//..
player = new MediaPlayer();
player.SetAudioStreamType(Stream.Music);
//..
await player.SetDataSourceAsync(ApplicationContext, Android.Net.Uri.Parse(mediaUrl));
//..
player.PrepareAsync();
//..

Is there a way to play the link above (mediaUrl2) without (of course) downloading the file first?

Here is the full source of the sample i am using. Any help would be appreciated.

Upvotes: 2

Views: 2530

Answers (1)

SushiHangover
SushiHangover

Reputation: 74209

http://api-streaming.youscribe.com/v1/products/2919465/documents/3214936/audio/stream

That is an HTTP mpga stream and is not directly supported by any of the Android APIs that I know of and thus is not supported by MediaPlayer (consult the Android Support Media Formats for further reading).

You can review the logcat output of your MediaPlayer code and you will see output like:

[MediaPlayerNative] start called in state 4, mPlayer(0x8efb7240)
[MediaPlayerNative] error (-38, 0)
[MediaPlayer] Error (-38,0)
[MediaHTTPConnection] readAt 1161613 / 32768 => java.net.ProtocolException
[MediaHTTPConnection] readAt 1161613 / 32768 => java.net.ProtocolException
[MediaPlayerNative] error (1, -2147483648)
[MediaPlayer] Error (1,-2147483648)

Google's Android ExoPlayer can stream that media format properly.

This is a really simple and very crude example of ExoPlayer, but it will show you that it does play that stream:

ExoPlayer Example:

var mediaUrl = "http://api-streaming.youscribe.com/v1/products/2919465/documents/3214936/audio/stream";
var mediaUri = Android.Net.Uri.Parse(mediaUrl);

var userAgent = Util.GetUserAgent(this, "ExoPlayerDemo");
var defaultHttpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
var defaultDataSourceFactory = new DefaultDataSourceFactory(this, null, defaultHttpDataSourceFactory);
var extractorMediaSource = new ExtractorMediaSource(mediaUri, defaultDataSourceFactory, new DefaultExtractorsFactory(), null, null);
var defaultBandwidthMeter = new DefaultBandwidthMeter();
var adaptiveTrackSelectionFactory = new AdaptiveTrackSelection.Factory(defaultBandwidthMeter);
var defaultTrackSelector = new DefaultTrackSelector(adaptiveTrackSelectionFactory);

exoPlayer = ExoPlayerFactory.NewSimpleInstance(this, defaultTrackSelector);
exoPlayer.Prepare(extractorMediaSource);
exoPlayer.PlayWhenReady = true;

Note: exoPlayer is a class-level variable of SimpleExoPlayer type

Note: this is using the Xamarin.Android binding libraries from the Xam.Plugins.Android.ExoPlayer package

ExoPlayer Docs:

Upvotes: 4

Related Questions