Xamarin CT MediaElement在加载时 Flink 黑色,有没有办法在显示页面之前等待加载时间?

xqnpmsa8  于 2022-12-07  发布在  Flink
关注(0)|答案(1)|浏览(231)

尝试修正XCT MediaElement这个非常烦人的问题。整个屏幕加载正常,但是视频背景 Flink 黑色,因为视频还没有完全加载。有没有办法在视频完全加载之前停止屏幕显示?

忘了添加-我也试过使用Task.Deplay()延迟打开屏幕,并慢慢放松媒体播放器的Opacity,但我只是得到了一个奇怪的排序工作 Flink 后,约400毫秒后,屏幕加载,所以这并不真正工作,不觉得流畅,只是笨重。我也试过将IsVisible设置为false等待250- 350毫秒,并设置回true同样笨重的感觉。

提前致谢。

<StackLayout BackgroundColor="{StaticResource PrimaryBackgroundColor}"
             Grid.Row="1"
             Grid.ColumnSpan="3">
<xct:MediaElement x:Name="mediaPlayer"
                  Source="{Binding MediaSource}"
                  VerticalOptions="FillAndExpand"
                  HorizontalOptions="FillAndExpand"
                  Aspect="AspectFill"
                  ShowsPlaybackControls="False"
                  AutoPlay="True" />
</StackLayout>

GameScreen1.xaml.csCTOR + PreLoad方法-我使用Preload方法阻止视频播放50次,因为加载问题...并且在屏幕出现之前启动游戏设置所有属性。我认为这将阻止屏幕。

public GameScreen1() {
  InitializeComponent();
  NavigationPage.SetHasNavigationBar(this, false);

  this.PageViewModel = (GameScreen1Model) this.BindingContext;

  PreLoad();
}

private void PreLoad() {
  PageViewModel.StartGame();

  mediaPlayer.Pause();
  mediaPlayer.Play();
}

GameScreen 1 Model-仅添加了启动游戏方法。

public void StartGame() {
  MaxWordsInCurrentGame = GetMaxWordsInCurrentGame();
  CurrentWord = GetNewCurrentWord(MaxWordsInCurrentGame);
  //ADD: - Get MaxGames to be able to fish the game after all games have been played.

  if (CheckIfNextGame() == true) {
    CurrentGame += 1;
    CurrentWord = 1;
    IsQuizCompletedForCurrentGame = false;
  } else if (CurrentWord == 99 && IsQuizCompletedForCurrentGame == false) {
    CurrentWord = MaxWordsInCurrentGame;
  }

  var gameResources = _GameResources.GetGameResourcesFromConfig(
      CurrentGame, CurrentWord, false, false, false, true, false, true, false);

  ImageSource = gameResources["image"];
  MediaSource = gameResources["video"];
}
sg3maiej

sg3maiej1#

在这个问题上折腾了一段时间后,我找到了一个足够平滑(不完美)的解决方案,以一种好的方式加载视频。计时可以用更多的时间来播放,但我认为这对我目前来说已经足够好了。
XAML代码-使用额外的MediaElement

<StackLayout BackgroundColor="{StaticResource PrimaryBackgroundColor}"
             Grid.Row="1"
              Grid.ColumnSpan="3">
<xct:MediaElement x:Name="mediaPlayer"
                  Source="{Binding MediaSource}"
                  VerticalOptions="FillAndExpand"
                  HorizontalOptions="FillAndExpand"
                  Aspect="AspectFill"
                  ShowsPlaybackControls="False"
                  AutoPlay="True" />
<xct:MediaElement x:Name="mediaPlayer2"
                  VerticalOptions="FillAndExpand"
                  HorizontalOptions="FillAndExpand"
                  Aspect="AspectFill"
                  ShowsPlaybackControls="False"
                  AutoPlay="True" />
</StackLayout>

C#程式码-在OnAppearing方法内

protected async override void OnAppearing() {
  base.OnAppearing();

  PageViewModel.Start();

  mediaPlayer.IsVisible = false;
  mediaPlayer2.IsVisible = true;
  await Task.Delay(50);
  mediaPlayer.Opacity = 0;
  await Task.Delay(15);
  mediaPlayer2.IsVisible = false;
  mediaPlayer.IsVisible = true;
  await Task.Delay(15);
  mediaPlayer.Opacity = 1;
  mediaPlayer.Stop();
  await Task.Delay(2);
  mediaPlayer.Play();
  mediaPlayer.Stop();
  mediaPlayer.Play();
}

相关问题