使用Xamarin在Android中播放视频

使用Xamarin在Android中播放视频

本文介绍了使用Xamarin在Android中播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用VideoView的Android Xamarin中,我想从资源中播放视频

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

这是代码:

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()后,视频无法播放,我得到的只是以下消息:无法播放此视频

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

推荐答案

我找到了答案:

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

        }
    }

这篇关于使用Xamarin在Android中播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:52