使用Xamarin在Android中播放视频作为Splash Screen

时间:2016-06-28 15:23:24

标签: android video xamarin.android splash-screen

我正在尝试使用Xamarin在Android中播放视频作为启动画面。我找到了下面的链接,但我得到了file not found exception。我尝试了几种不同的方式,但没有取得任何成功。

我的视频Splash.mp4位于资源中的可绘制文件夹中,SplashActivity.cs文件位于项目级别。

play video in Android using Xamarin

 [Activity(Label = "SplashVideo", MainLauncher = true, NoHistory = true)]
public class SplashVideo : Activity, MediaPlayer.IOnPreparedListener, ISurfaceHolderCallback
{
    VideoView videoView;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.VideoLayout);
        videoView = FindViewById<VideoView>(Resource.Id.SampleVideoView);

        play();
    }
    MediaPlayer player = new MediaPlayer();
    void play()
    {
        ISurfaceHolder holder = videoView.Holder;
        holder.SetType(SurfaceType.PushBuffers);
        holder.AddCallback(this);

        Android.Content.Res.AssetFileDescriptor afd = Resources.Assets.OpenFd("Splash.mp4");
        if (afd != null)
        {
            player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
            player.Prepare();
            player.Start();
        }
    }

    public void SurfaceCreated(ISurfaceHolder holder)
    {
        Console.WriteLine("SurfaceCreated");
        player.SetDisplay(holder);
    }

    public void SurfaceDestroyed(ISurfaceHolder holder)
    {
        Console.WriteLine("SurfaceDestroyed");
        player.SetDisplay(null);
    }

    public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int w, int h)
    {
        Console.WriteLine("SurfaceChanged");
    }

    public void OnPrepared(MediaPlayer mp)
    {
        throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:1)

视频必须位于资源文件夹中,而不是可绘制的