user3660264 user3660264 - 5 months ago 179
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

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.Prepare();
            mediaPlayer.Looping = true;
            mediaPlayer.Start();

        }


        public void SurfaceCreated(ISurfaceHolder holder)
        {
            Console.WriteLine("SurfaceCreated");
            mediaPlayer.SetDisplay(holder);
        }
        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
            Console.WriteLine("SurfaceDestroyed");
        }
        public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int w, int h)
        {
            Console.WriteLine("SurfaceChanged");
        }
        public void OnPrepared(MediaPlayer player)
        {

        }
    }
Comments