Baseflow / ExoPlayerXamarin

Xamarin bindings library for the Google ExoPlayer library

Home Page:https://baseflow.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to play UDP Multicast stream

earkania opened this issue · comments

commented

Hi.
I'm using ExoPlayerXamarin v2.9.7 and VS2019 with latest update. I'm struggling to play UDP multicast stream. I can't managed how to do it.
I have done lots of google search but I have not found any example for ExoPlayerXamarin. I have found some examples for java. This one is one of them. but I failed to port it to C#. Specifically I have not found DataSource.Factory class used there.
Can you provide an example how to play UDP stream?
this is a UDP stream I'm trying to play udp://@239.192.2.2:1234 it works well with VLC player.

commented

Finally I have managed to play UDP multicast stream using ExoPlayerXamarin.
I have created UdpDataSourceFactory class. This class implements IDataSourceFactory interface.

using Com.Google.Android.Exoplayer2.Upstream;
public class UdpDataSourceFactory : Java.Lang.Object, IDataSourceFactory
{
    private UdpDataSource UdpDataSourceField { get; set; }

    public UdpDataSourceFactory(UdpDataSource dataSource)
    {
        this.UdpDataSourceField = dataSource;
    }

    public IDataSource CreateDataSource()
    {
        return UdpDataSourceField;
    }
}

Then I initialized player using following code

private void InitPlayer() 
{
    Uri uri = Uri.Parse("udp://226.1.1.23:10123");
    DefaultTrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory());
    SimpleExoPlayer player = ExoPlayerFactory.NewSimpleInstance(this, trackSelector);
    PlayerView playerView = FindViewById<PlayerView>(Resource.Id.simpleExoPlayerView1);
    playerView.Player = player;
    playerView.RequestFocus();
    UdpDataSourceFactory factory =  new UdpDataSourceFactory(new UdpDataSource(3000, 100000));
    IMediaSource mediaSource = new ExtractorMediaSource.Factory(factory).CreateMediaSource(uri);
    player.Prepare(mediaSource);
    player.PlayWhenReady = true;
}