gpt4 book ai didi

c# - 使用 Xamarin 在 Android 中播放视频

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:59:16 25 4
gpt4 key购买 nike

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

最佳答案

我找到了答案:

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

}
}

关于c# - 使用 Xamarin 在 Android 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com