I am unable to add video controls (i.e. play, stop, fast-forward) to the video player for a Xamarin.Forms app.
The video plays fine. However, they're no controls for a user to control the video player.
Specifically, I receive a null exception when I attempt to execute the following statement:
_mediaController.Show(5000);
Any ideas on how to implement this trivial functionality?
Video Renderer
public class VideoViewRenderer : ViewRenderer<VideoView, Android.Widget.VideoView>, ISurfaceHolderCallback
{
Android.Widget.VideoView _videoview = null;
MediaPlayer _player = null;
CustomMediaController _mediaController = null;
public void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
{
}
public void SurfaceCreated(ISurfaceHolder holder)
{
_player.SetDisplay(holder);
_player.Prepare();
_player.Start();
}
public void SurfaceDestroyed(ISurfaceHolder holder)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<VideoCommentary.VideoView> e)
{
base.OnElementChanged(e);
e.NewElement.StopAction = () =>
{
_player.Stop();
this.Control.StopPlayback();
};
_videoview = new Android.Widget.VideoView(Context);
_mediaController = new CustomMediaController(Forms.Context, false);
_mediaController.SetAnchorView(_videoview);
_mediaController.SetMinimumWidth(_videoview.Width);
_mediaController.RequestFocus(); //will make it display as soon as the video starts
_videoview.SetMediaController(_mediaController);
_mediaController.Show(5000);
base.SetNativeControl(_videoview);
Control.Holder.AddCallback(this);
_player = new MediaPlayer();
play(e.NewElement.FileSource);
}
void play(string fullPath)
{
var assetFileDescriptor = Forms.Context.Assets.OpenFd(fullPath);
if (assetFileDescriptor != null)
{
_player.SetDataSource(assetFileDescriptor.FileDescriptor, assetFileDescriptor.StartOffset, assetFileDescriptor.Length);
_player.Prepare();
_player.Start();
Control.Layout(0, 200, _player.VideoHeight, _player.VideoWidth);
}
}
}
Custom Media Controller
public class CustomMediaController : MediaController
{
public CustomMediaController(Context context, bool b) : base(context, false) { }
public override void Hide() { } //this will make controller not to disappear after interaction
public override void Show()
{
base.Show();
}
}