在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()之后,视频无法播放,我得到的就是这条消息: 无法播放此视频
答案 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();