问题描述
我已经实现了xamarin格式的视频播放器,可以作为登录屏幕的背景视频播放。视频在开始时成功加载,并且没有声音和循环播放(已配置)。但是问题是,当我缩小应用程序大小并再次恢复,然后视频消失,而我甚至无法播放时,像{ {1}}
我尝试的链接如下
- http://makanda.io/how-to-create-a-background-video-in-xamarin-forms/(我首先尝试了此链接)
- https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/video-player/(然后完全替换为此)
两个链接均按预期工作,但两个链接中的问题相同。
我试图解决什么问题?
- 我尝试在我的
video.play if paused or stopped
方法中将视频恢复为if(videoPlayer.Status == Renderers.VideoStatus.Paused) videoPlayer.Play()
。但这不起作用 - 我目前正在尝试从堆栈布局后面的代码动态加载视频。但是我不知道如何从后面的代码中设置视频源。我正在按照以下步骤进行操作
OnAppearing()
错误提示,无法将字符串转换为UriVideoSource
帮助链接:
https://developer.android.com/reference/android/widget/VideoView
当应用程序进入后台时,视频视图似乎无法保持其状态。
解决方法
我目前正在尝试从堆栈布局后面的代码动态加载视频。但是我不知道如何从后面的代码中设置视频源。我正在按照以下步骤进行操作
我注意到您的路径来自本地资源,您可以使用以下代码加载视频。如果您的路径是URL,则可以使用 videoPlayer.Source = VideoSource.FromUri(source);
进行加载。
var videoPlayer=new VideoPlayer();
string source = "";
switch (Device.RuntimePlatform)
{
case Device.iOS:
source = "Videos/iOSApiVideo.mp4";
break;
case Device.Android:
source = "AndroidApiVideo.mp4";
break;
default:
source = "AndroidApiVideo.mp4";
break;
}
videoPlayer.Source = VideoSource.FromResource(source);
我尝试将视频恢复为OnAppearing()方法中的if(videoPlayer.Status == Renderers.VideoStatus.Paused)videoPlayer.Play()。但这不起作用
这是我正在运行的演示结果,当我回到我的应用程序时,它可以工作。我还实现了寻找上一个时间跨度功能。
这是我的演示。
https://drive.google.com/file/d/1kpYbMV5mA3UdbGD7r6tLv1S2ix7AwM5B/view?usp=sharing
,我使用计时器作为解决方法来恢复视频,该视频在应用程序进入睡眠模式时会暂停。
protected override void OnAppearing()
{
base.OnAppearing();
shouldTimerRun = true;
Device.StartTimer(new TimeSpan(0,1),() =>
{
Device.BeginInvokeOnMainThread(() =>
{
if (videoPlayer.Status == VideoStatus.Paused)
{
//videoPlayer.TimeToEnd = timeSpan;
videoPlayer.Position = timeSpan;
videoPlayer.Play();
}
});
return shouldTimerRun;
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
shouldTimerRun = false;
timeSpan = videoPlayer.Position;
}