使用Xamarin中的Exoplayer自定义渲染器进行导航

问题描述 投票:0回答:1

我在主页面上有一个列表视图,当我点击列表视图时,它会进入第二页,其中Exoplayer与自定义渲染器集成,所以问题是如何正确释放播放器,因为当前_player总是等于null,播放多个音频,如果多次从第一页导航到第二页。

这是自定义渲染器的代码 -

public class VideoPlayerRenderer : ViewRenderer<VideoPlayer, SimpleExoPlayerView>, IExoPlayerEventListener
{
    private SimpleExoPlayerView _playerView;
    private SimpleExoPlayer _player;
    private DefaultTrackSelector trackSelector;
    DefaultHttpDataSourceFactory defaultHttpDataSourceFactory;
    DefaultDataSourceFactory defaultDataSourceFactory ;
    ExtractorMediaSource extractorMediaSource ;
    public VideoPlayerRenderer(Context context) : base(context)
    {

    }


    protected override void OnElementChanged(ElementChangedEventArgs<VideoPlayer> e)
    {
        base.OnElementChanged(e);

        if (_player == null)
        {
            InitializePlayer();
        }


        Play();
    }


    private void InitializePlayer()
    {

        trackSelector = new DefaultTrackSelector();

        _player = ExoPlayerFactory.NewSimpleInstance(Context,trackSelector);
        _player.PlayWhenReady = true;
        _playerView = new SimpleExoPlayerView(Context) { Player = _player };
        SetNativeControl(_playerView);
        SetBackgroundColor(Android.Graphics.Color.Green);
    }

    private void Play()
    {

            Uri sourceUri = Uri.Parse(Element.SourceUrl);
            var mediaUri = Android.Net.Uri.Parse(Element.SourceUrl);
            var userAgent = Util.GetUserAgent(Context, "App1");
             defaultHttpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
             defaultDataSourceFactory = new DefaultDataSourceFactory(Context, null, defaultHttpDataSourceFactory);
             extractorMediaSource = new ExtractorMediaSource(mediaUri, defaultDataSourceFactory, new DefaultExtractorsFactory(), null, null);
            _player.Prepare(extractorMediaSource);
            _player.AddListener(this);

    }
c# xamarin exoplayer
1个回答
0
投票

在自定义渲染器中,覆盖OnDetachedFromWindow以停止播放器:

   protected override void OnDetachedFromWindow()
    {
        base.OnDetachedFromWindow();
        _player.Stop();
        _player.Release();
    }

参考:onDetachedFromWindow

更新:

要控制播放器,您应该创建一个SimpleExoPlayer单例,确保整个项目中只有一个_player

class ExoPlayerInstance
{

    private static SimpleExoPlayer _player;

    public static SimpleExoPlayer player
    {
        get
        {
            if (_player == null)
            {
                _player = ExoPlayerFactory.NewSimpleInstance(Application.Context, new DefaultTrackSelector());
                _player.PlayWhenReady = true;
            }
            return _player;
        }
    }
}

然后在自定义渲染中,删除InitializePlayer部分并使用ExoPlayerInstance

protected override void OnElementChanged(ElementChangedEventArgs<VideoPlayer> e)
{
    base.OnElementChanged(e);

    //remove the InitializePlayer part
    //if (_player == null)
    //    InitializePlayer();

    _player = ExoPlayerInstance.player;

    _playerView = new SimpleExoPlayerView(Context) { Player = _player };
    SetNativeControl(_playerView);

    if (_player.IsPlayingAd || _player.IsLoading)
    {
        //maybe something more need to do , stop() here is an example
        _player.Stop();
    }

    Play();
}
© www.soinside.com 2019 - 2024. All rights reserved.