user3660264 user3660264 - 1 year ago 453
Android Question

play video in Android using Xamarin

In Xamarin for Android using VideoView I want to play a video from resources

Here is code:

var videoView = (VideoView)FindViewById<VideoView> (Resource.Id.videoView);
var Path = ("/Resources/raw/video.mp4");
var uri = Android.Net.Uri.Parse (Path);
videoView.SetVideoURI (uri);
videoView.Start ();

After calling videoView.Start() video doesn't play and all I get is this message:
Can't play this video

Answer Source

I found the answer:

public class MainActivity : Activity, MediaPlayer.IOnPreparedListener, ISurfaceHolderCallback

        MediaPlayer mediaPlayer;
        protected override void OnCreate (Bundle bundle)
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.Main);

            var videoView = (VideoView)FindViewById<VideoView> (Resource.Id.videoView);

            ISurfaceHolder holder = videoView.Holder;
            holder.SetType (SurfaceType.PushBuffers);
            holder.AddCallback( this );

            var descriptor = Assets.OpenFd("test.ogv");
            mediaPlayer = new MediaPlayer ();
            mediaPlayer.SetDataSource(descriptor.FileDescriptor, descriptor.StartOffset, descriptor.Length);
            mediaPlayer.Looping = true;


        public void SurfaceCreated(ISurfaceHolder holder)
        public void SurfaceDestroyed(ISurfaceHolder holder)
        public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int w, int h)
        public void OnPrepared(MediaPlayer player)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download