Xamarin.Forms ViewRenderer音频不会停留在主页/后退按钮上

时间:2018-02-28 04:13:57

标签: android xamarin xamarin.forms android-mediaplayer viewrendering

我是新来的,如果我不提问题或发布足够的信息,请原谅。

我是创建移动应用程序的新手,我使用!age ageOfPerson创建自定义视图。在此视图中,我使用Android Xamarin.Forms播放内置android ViewRenderer的音频/视频。

与Android渲染器发布并接受为Renderer I copied and is working

的答案几乎完全相同

我的问题是当视频启动并且您单击主页/后退按钮时,音频会继续播放几秒钟然后停止。我想立即停止音频。

我尝试的方法:

在我的MediaPlayer/VideoView我尝试ViewRenderer致电override SurfaceDestroyed。这没有用,没有错误或任何东西只是音频继续像这个代码不存在。音频在大约3-5秒后停止。

player.stop()我尝试使用ViewRenderer事件来呼叫Control.SystemUiVisibilityChange。没有错误或任何东西。音频持续3-5秒。

我无法将player.stop()实例传递给主要活动player方法,因为我刚接触onPause()和android Xamarin.Forms以了解如何操作。可能会在ViewRenderers方法上调用此player.stop(),但我无法找到如何执行此操作。有人可以帮忙吗?我已经搜索了很多论坛数周,最后放弃发布一个问题。

1 个答案:

答案 0 :(得分:0)

对于后退按钮,您只需覆盖当前OnBackButtonPressed页面的Xamarin.Forms

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    protected override bool OnBackButtonPressed()
    {
        //stop the videoview
        videoview.Stop();
        return base.OnBackButtonPressed();
    }
   ...
}

对于主页按钮,我提到this thread,并根据杰克的回答制作了HomeWatcher的Xamarin版本:

public interface IOnHomePressedListener
{
    void OnHomePressed();
    void OnHomeLongPressed();
}

public class HomeWatcher
{
    static readonly String TAG = "hg";
    private Context mContext;
    private IntentFilter mFilter;
    private IOnHomePressedListener mListener;
    private InnerRecevier mRecevier;

    public HomeWatcher(Context context)
    {
        mContext = context;
        mFilter = new IntentFilter(Intent.ActionCloseSystemDialogs);
    }

    public void SetOnHomePressedListener(IOnHomePressedListener listener)
    {
        mListener = listener;
        mRecevier = new InnerRecevier(mListener);
    }

    public void StartWatch()
    {
        if (mRecevier != null)
        {
            mContext.RegisterReceiver(mRecevier, mFilter);
        }
    }

    public void StopWatch()
    {
        if (mRecevier != null)
        {
            mContext.UnregisterReceiver(mRecevier);
        }
    }


    private class InnerRecevier : BroadcastReceiver
    {
        readonly String SYSTEM_DIALOG_REASON_KEY = "reason";
        readonly String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
        readonly String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        readonly String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
        IOnHomePressedListener _listener;

        public InnerRecevier(IOnHomePressedListener listener)
        {
            _listener = listener;
        }

        public override void OnReceive(Context context, Intent intent)
        {
            String action = intent.Action;
            if (action.Equals(Intent.ActionCloseSystemDialogs))
            {
                String reason = intent.GetStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (reason != null)
                {
                    //Log.e(TAG, "action:" + action + ",reason:" + reason);
                    if (_listener != null)
                    {
                        if (reason.Equals(SYSTEM_DIALOG_REASON_HOME_KEY))
                        {
                            _listener.OnHomePressed();
                        }
                        else if (reason.Equals(SYSTEM_DIALOG_REASON_RECENT_APPS))
                        {
                            _listener.OnHomeLongPressed();
                        }
                    }
                }
            }
        }
    }
}

并在VideoViewRenderer(视频开始播放时为StartWatch(),清除视频视频时StopWatch())中使用它:

public class VideoViewRenderer : ViewRenderer<VideoView, Android.Widget.VideoView>, ISurfaceHolderCallback,IOnHomePressedListener
{
    ...

    private MediaPlayer _player;

    private HomeWatcher _homeWatcher;



    public VideoViewRenderer(Context context) : base(context)
    {
        _context = context;
        _homeWatcher = new HomeWatcher(context);
        _homeWatcher.SetOnHomePressedListener(this);

    }

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


        e.NewElement.CleanAction = new Action(() =>
        {
            #region Clean video player action (player no more used)

            if (_player == null)
                return;
            //stop watch home button
            _homeWatcher.StopWatch();


            _player.Release();

            #endregion
        });



        e.NewElement.PlayAction = new Action(() =>
        {
            #region Play video if it was stopped


            if (_player == null)
                return;
            //start watch home button
            _homeWatcher.StartWatch();
            if (!_player.IsPlaying)
            {
                _player.Start();
            }

            #endregion
        });

       ...
     }

  }