视频 android 处理方法,Android MediaPlayer正确处理视频

我正在尝试创建一个Xamarin Forms应用程序,该应用程序显示启动页面的全屏视频 . 加载完成后,启动页面将带您进入主屏幕 . 主屏幕在旋转木马中显示一系列视频 .

在Android上,旋转木马视频可以自行工作,但是当我添加启动画面视频时,它会显示在旋转木马上 . 目前我的应用程序中有一个名为VideoView的视图,它具有视频源的BindableProperty .

渲染是在下面的自定义渲染器中专门完成的 . 它使用适用于Android的原生VideoView和MediaPlayer控件 . 我已经在启动屏幕的OnDisappearing功能中尝试了各种dispose和release函数,但它们似乎都没有发布视频 .

[assembly: ExportRenderer(typeof(App.Renderers.VideoView),

typeof(VideoViewRenderer))]

namespace App.Droid.Renderers

{

public class VideoViewRenderer :

ViewRenderer, ISurfaceHolderCallback

{

private VideoView videoview;

private MediaPlayer player;

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

{

}

public void SurfaceDestroyed(ISurfaceHolder holder)

{

}

protected override void OnElementChanged(ElementChangedEventArgs e)

{

base.OnElementChanged(e);

e.NewElement.StopAction = () =>

{

player.Pause();

};

e.NewElement.StartAction = () =>

{

player.Start();

};

e.NewElement.DisposeAction = () =>

{

Control.StopPlayback();

Control.Dispose();

player.Stop();

player.Reset();

player.Release();

};

if (Control == null)

{

videoview = new VideoView(Context);

base.SetNativeControl(videoview);

Control.Holder.AddCallback(this);

player = new MediaPlayer();

play(e.NewElement.FileSource);

}

}

void play(string fullPath)

{

AssetFileDescriptor afd = Forms.Context.Assets.OpenFd(fullPath);

if (afd != null)

{

try

{

player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);

player.Prepare();

player.Looping = true;

Control.Layout(0, 200, player.VideoHeight, player.VideoWidth);

player.SetVideoScalingMode(VideoScalingMode.ScaleToFit);

}

catch

{

}

}

}

public void SurfaceCreated(ISurfaceHolder holder)

{

player.SetDisplay(holder);

}

}

}

VideoView类扩展了View,如下所示:

public class VideoView : View

{

public Action StopAction;

public Action StartAction;

public Action DisposeAction;

public VideoView()

{

FileSource = string.Empty;

}

public static readonly BindableProperty FileSourceProperty =

BindableProperty.Create(

p => p.FileSource, string.Empty);

public string FileSource

{

get { return (string)GetValue(FileSourceProperty); }

set { SetValue(FileSourceProperty, value); }

}

public void Stop()

{

if (StopAction != null)

StopAction();

}

public void Dispose()

{

if (DisposeAction != null)

DisposeAction();

}

public void Start()

{

if (StartAction != null)

StartAction();

}

}

最后使用视图如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值