有关Media Player出现阻止UI输入大约30秒的原因的任何信息

问题描述 投票:0回答:1

我正在我的UWP应用程序中运行一个短视频,而其他一些任务正在后台完成。如果用户不想观看视频,则会有一个“跳过视频”按钮。但是,该按钮在视频中大约30秒后才会激活。

我想也许是我的后台任务阻止了UI,但它们在5-7秒内就完成了。我也想也许是导致问题的视频加载,但它只有3分钟,所以我认为这需要很长时间来缓冲。

这是我用于页面的XAML:

<Page
    x:Class="Cgs.Ux.Views.MainMenu"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Cgs.Ux"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:userControls="using:Cgs.Ux.UserControls"
    xmlns:help="using:Cgs.Ux.UserControls.Help"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <RelativePanel RequestedTheme="Light">
        <Image Source="{x:Bind Vm.PlainBackgroundImage, Mode=OneWay, Converter={StaticResource ImageConverter}}"
               Stretch="Fill"/>
        <Viewbox RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignTopWithPanel="True"
                 RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True">
            <Grid>
                <Image Source="{x:Bind Vm.BackgroundImage, Mode=OneWay, Converter={StaticResource ImageConverter}}"
                       RelativePanel.AlignVerticalCenterWithPanel="True" RelativePanel.AlignHorizontalCenterWithPanel="True"
                       Height="1080" Width="1920" Opacity=".5"/>
                <!--Media Player-->
                <MediaPlayerElement
                    Name="MediaSimple"
                    Source="ms-appx:///Assets/Videos/Transfer Items Alpha 2.mp4"
                    Width="1920" Height="1080" AutoPlay="False" Visibility="Collapsed">
                </MediaPlayerElement>
                <Button
                    Name="SkipVideo"
                    VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="30"
                    Content="Skip Video" Height="100" Width="400" Background="White"
                    Visibility="{x:Bind Vm.SkipVideoVisible, Mode=OneWay}"
                    PointerEntered="OnPointerEntered"
                    Click="{x:Bind Vm.SkipVideo}"/>
            </Grid>
        </Viewbox>
        <ProgressRing
            RelativePanel.AlignHorizontalCenterWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True"
            Width="100" Height="100" Foreground="NavajoWhite" IsActive="{x:Bind Vm.IsBusy, Mode=OneWay}" />
    </RelativePanel>
</Page>

以下是相关代码:

public void PlayMedia()
{
    MediaSimple.MediaPlayer.Play();
    MediaSimple.Visibility = Visibility.Visible;
}

public void SkipVideo()
{
    View.StopVideo();
    _introVideoFinished = true;
    if (_soloGameLoaded && _introVideoFinished) GotoGame();
}

private void OnPointerEntered(object sender, PointerRoutedEventArgs e)
{
    Debug.WriteLine("Pointer Entered");
}

我希望按钮应立即可用,或几乎立即可用。

uwp
1个回答
1
投票

我试图创建一个独立的项目来复制问题。可悲的是,我的独立项目都完美无缺。我的独立项目和失败的项目之间的区别是我在视频启动时启动的后台任务。

我最初的想法是后台任务阻止了用户界面的目标。我修改后台任务以在单独的线程上运行,媒体播放器和跳过视频按钮完美地工作。

在将后台任务移动到自己的线程后,应用程序开始在编组错误上失败,因为后台任务正在尝试与UI通信。这解释了为什么后台任务阻止UI最多30秒,当我预计它在5-7秒内完成时。

故事的寓意,不要在你的UI线程上运行后台任务,并期望你的UI行为正常。

© www.soinside.com 2019 - 2024. All rights reserved.