【问题标题】:Navigation with custom renderer of Exoplayer in xamarin在 xamarin 中使用 Exoplayer 的自定义渲染器进行导航
【发布时间】:2019-04-15 07:51:02
【问题描述】:

我在主页中有一个列表视图,当我单击列表视图时,它会转到第二页,其中有 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】:

    在您的自定义渲染器中,覆盖 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();
    }
    

    【讨论】:

    • 感谢回复,我之前试过了,但问题是,当我点击第二页的后退按钮时,歌曲停止播放,但我想播放歌曲,即使我点击后退按钮或在后台,这对于音频播放器来说足够公平
    • @uncle_scooge 好的,所以您想要的是始终播放歌曲,直到用户暂停它或准备好播放下一首歌曲?你现在的问题是两首歌同时播放吗?
    • 感谢您抽出宝贵时间...我只是使用了 Android.App.Application.Context 而不是上下文,这导致无法从本机句柄激活实例...可能是由于抽象泄漏..将进一步研究您的代码
    • @uncle_scooge 没问题。我只是提供一个想法给你。我认为使用单例将引导您找到解决此问题的正确方法。
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2021-04-04
    相关资源
    最近更新 更多