如果我不正确地提出问题或发布足够的信息,我很新,请原谅。
我是创建移动应用程序的新手,我使用Xamarin.Forms
创建自定义视图。有了这个视图我使用Android ViewRenderer
播放内置android MediaPlayer/VideoView
的音频/视频。
几乎与Android渲染器发布并接受为Renderer I copied and is working的答案完全相同
我的问题是当视频开始并且您单击主页/后退按钮时,音频会继续播放几秒钟然后停止。我想音频立即停止。
方法我尝试过:
在我的ViewRenderer
,我试图让override SurfaceDestroyed
打电话给player.stop()
。这没有用,没有错误或任何东西只是音频继续像这个代码不存在。大约3-5秒后音频停止。
在ViewRenderer
我试图使用Control.SystemUiVisibilityChange
事件来调用player.stop()
。没有错误或任何东西。音频持续3-5秒。
我无法将player
实例传递给主要活动onPause()
方法,因为我是新来的Xamarin.Forms
和android ViewRenderers
以了解如何。可能在player.stop()
方法上调用这个onPause()
会起作用,但我无法找到如何做到这一点。有人可以帮忙吗?我已经搜索了很多论坛数周,最后放弃发布一个问题。
对于后退按钮,您只需要覆盖当前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,并根据杰克的回答制作了一个Xamax版本的HomeWatcher
:
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
});
...
}
}