Xamarin Forms 中的视频播放器在最小化和恢复时消失了
Video player in Xamarin Forms is gone when minimized and resumed
我已经在 xamarin 表单中实现了一个视频播放器,作为我的登录屏幕的 背景视频 播放。视频在开始时成功加载并播放时没有声音和循环(按配置)。但问题是当我最小化应用程序并再次恢复时,然后视频消失了而且我甚至无法播放它回来了 喜欢 video.play if paused or stopped
我试过的link如下
- http://makanda.io/how-to-create-a-background-video-in-xamarin-forms/(我先试了这个link)
- https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/video-player/(然后完全换成这个)
两个 link 都按预期工作,除了两个 link 中相同的问题。
我尝试过什么来解决这个问题?
- 我尝试在
OnAppearing()
方法中将视频恢复为 if(videoPlayer.Status == Renderers.VideoStatus.Paused) videoPlayer.Play()
。但是没用
- 我目前正在尝试从堆栈布局中的代码后面动态加载视频。但我不知道如何从代码后面设置视频源。我是这样做的
string source = "";
switch (Device.RuntimePlatform)
{
case Device.iOS:
source = "Videos/walkthroughvideo9_16.mp4";
break;
case Device.Android:
source = "walkthroughvideo9_16.mp4";
break;
default:
source = "walkthroughvideo9_16.mp4";
break;
}
VideoPlayer video = new VideoPlayer()
{
Source = (UriVideoSource)source
};
An error says, cannot convert string to UriVideoSource
帮助 LINK:
https://developer.android.com/reference/android/widget/VideoView
当应用程序进入后台时,视频视图似乎无法保持其状态。
I am currently trying to dynamically load the video from code behind inside a stack layout. But I don't know how to set the video source from code behind. I am doing it as follows
我注意到你的路径是本地资源,你可以使用下面的代码来加载视频。如果你的路径是 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);
I tried resuming the video as if(videoPlayer.Status == Renderers.VideoStatus.Paused) videoPlayer.Play() inside my OnAppearing() method. But it doesn't work
这是我的 运行 演示结果,当我回到我的应用程序时,它可以工作。我也实现了寻找上一个时间跨度的功能。
这是我的演示。
https://drive.google.com/file/d/1kpYbMV5mA3UdbGD7r6tLv1S2ix7AwM5B/view?usp=sharing
我使用计时器作为解决方法来恢复在应用进入睡眠模式时暂停的视频。
protected override void OnAppearing()
{
base.OnAppearing();
shouldTimerRun = true;
Device.StartTimer(new TimeSpan(0, 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;
}
我已经在 xamarin 表单中实现了一个视频播放器,作为我的登录屏幕的 背景视频 播放。视频在开始时成功加载并播放时没有声音和循环(按配置)。但问题是当我最小化应用程序并再次恢复时,然后视频消失了而且我甚至无法播放它回来了 喜欢 video.play if paused or stopped
我试过的link如下
- http://makanda.io/how-to-create-a-background-video-in-xamarin-forms/(我先试了这个link)
- https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/video-player/(然后完全换成这个)
两个 link 都按预期工作,除了两个 link 中相同的问题。
我尝试过什么来解决这个问题?
- 我尝试在
OnAppearing()
方法中将视频恢复为if(videoPlayer.Status == Renderers.VideoStatus.Paused) videoPlayer.Play()
。但是没用 - 我目前正在尝试从堆栈布局中的代码后面动态加载视频。但我不知道如何从代码后面设置视频源。我是这样做的
string source = "";
switch (Device.RuntimePlatform)
{
case Device.iOS:
source = "Videos/walkthroughvideo9_16.mp4";
break;
case Device.Android:
source = "walkthroughvideo9_16.mp4";
break;
default:
source = "walkthroughvideo9_16.mp4";
break;
}
VideoPlayer video = new VideoPlayer()
{
Source = (UriVideoSource)source
};
An error says, cannot convert string to UriVideoSource
帮助 LINK:
https://developer.android.com/reference/android/widget/VideoView
当应用程序进入后台时,视频视图似乎无法保持其状态。
I am currently trying to dynamically load the video from code behind inside a stack layout. But I don't know how to set the video source from code behind. I am doing it as follows
我注意到你的路径是本地资源,你可以使用下面的代码来加载视频。如果你的路径是 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);
I tried resuming the video as if(videoPlayer.Status == Renderers.VideoStatus.Paused) videoPlayer.Play() inside my OnAppearing() method. But it doesn't work
这是我的 运行 演示结果,当我回到我的应用程序时,它可以工作。我也实现了寻找上一个时间跨度的功能。
这是我的演示。
https://drive.google.com/file/d/1kpYbMV5mA3UdbGD7r6tLv1S2ix7AwM5B/view?usp=sharing
我使用计时器作为解决方法来恢复在应用进入睡眠模式时暂停的视频。
protected override void OnAppearing()
{
base.OnAppearing();
shouldTimerRun = true;
Device.StartTimer(new TimeSpan(0, 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;
}