使用Xamarin在Android中播放视频

时间:2014-10-20 12:53:21

标签: c# android video xamarin xamarin.android

在Xamarin for Android中使用VideoView我想播放来自资源的视频

这是代码:

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 ();

在调用videoView.Start()之后,视频无法播放,我得到的就是这条消息: 无法播放此视频

2 个答案:

答案 0 :(得分:4)

我找到了答案:

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)
        {

        }
    }

答案 1 :(得分:1)

var uri= Android.Net.Uri.Parse("android.resource://" + Application.PackageName + "/" + Resource.Raw.demo);
video.SetVideoURI(uri);
video.Start();