gpt4 book ai didi

c# - 使用 Xamarin Forms 播放视频

转载 作者:太空狗 更新时间:2023-10-29 15:26:11 25 4
gpt4 key购买 nike

我们正在使用 Xamarin.forms 在 Xamarin 中构建应用程序。在应用程序中,我们需要播放视频,因此我们为此编写了一些代码。但是,视频没有播放,并且在 Android 上应用程序崩溃并抛出一般错误。

这是代码:

VideoContainer.cs

using System;
using Xamarin.Forms;
using System.Collections.Generic;

namespace MyApp
{
public class VideoView : View
{
public Action StopAction;
public VideoView ()
{
Console.WriteLine("VideoView loaded");
}

public static readonly BindableProperty FileSourceProperty =
BindableProperty.Create<VideoView,string>(
p => p.FileSource,string.Empty);

public string FileSource {
get { return (string)GetValue (FileSourceProperty); }
set { SetValue (FileSourceProperty, value); }
}

public void Stop(){
if(StopAction != null)
StopAction ();
}
}
}

VideoViewRender.cs

using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Media;
using Android.Content.Res;
using Java.Lang;
using MyApp.Droid;

[assembly: ExportRenderer(typeof(VideoView), typeof(VideoViewRenderer))]
//
namespace MyApp.Droid
{
public class VideoViewRenderer : ViewRenderer
{
VideoView videoview;
MediaPlayer player;
MediaController mediaController;
Handler handler = new Handler();
public VideoViewRenderer()
{
Console.WriteLine("VideoViewRenderer loaded");
}

public void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
{

}

public void SurfaceDestroyed(ISurfaceHolder holder)
{

}

void play(string fullPath)
{
AssetFileDescriptor afd = Forms.Context.Assets.OpenFd(fullPath);
if (afd != null)
{

player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
player.Prepare();
player.Start();
Control.Layout(0, 200, 400, 600);
player.Pause();


}
}

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

public override bool OnTouchEvent(MotionEvent e)
{
mediaController.Show();
return false;
}

//--MediaPlayerControl methods----------------------------------------------------
public void Start()
{
player.Start();
}


public void Pause()
{
player.Pause();
}

public int Duration
{
get
{
return player.Duration;
}
}

public int CurrentPosition
{
get
{
return player.CurrentPosition;
}
}

public void SeekTo(int i)
{
player.SeekTo(i);
}

public bool IsPlaying
{
get
{
return player.IsPlaying;
}
}

public int BufferPercentage
{
get
{
return 0;
}
}

public int AudioSessionId
{
get
{
return 0;
}
}

public bool CanPause()
{
return true;
}

public bool CanSeekBackward()
{
return true;
}

public bool CanSeekForward()
{
return true;
}
//--------------------------------------------------------------------------------

}
}

发生的情况是 VideoView loaded 被记录到控制台,但 VideoViewRenderer loaded 没有。我们从 the Xamarin forum 获得了这段代码, 但未能成功实现。我们做错了什么?

最佳答案

我认为您的 [assembly:...] 弄错了 VideoView

有可能 ExportRenderer 得到 Android.VideoView

你应该得到 [assembly: ExportRenderer(typeof(MyApp.VideoView), typeof(MyApp.Droid.VideoView))]

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

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