C# ';MediaElement.SetSource()和#x27;isn';不一致更新

C# ';MediaElement.SetSource()和#x27;isn';不一致更新,c#,winrt-xaml,mediaelement,C#,Winrt Xaml,Mediaelement,我的程序应该在用户按下“播放”按钮时播放视频。正常情况下,当他们第一次按下“播放”按钮时,什么也不会发生 我将此错误追溯到以下代码,该代码设置了我的MediaElement“VideoPlayer”: public void playVideo_Tapped(object sender, TappedRoutedEventArgs e) { setUpVideo(); VideoPlayer.Play(); } public async void setUpVideo() { if

我的程序应该在用户按下“播放”按钮时播放视频。正常情况下,当他们第一次按下“播放”按钮时,什么也不会发生

我将此错误追溯到以下代码,该代码设置了我的MediaElement“VideoPlayer”:

public void playVideo_Tapped(object sender, TappedRoutedEventArgs e)
{
  setUpVideo();
  VideoPlayer.Play();
}

public async void setUpVideo()
{
  if(vm == null) return;
  StorageFile videoFile = vm.videoFile;

  if (videoFile == null || !videoFile.ContentType.Equals("video/mp4")) return;

    using (IRandomAccessStream fileStream = await videoFile.OpenAsync(FileAccessMode.Read))
    {
      VideoPlayer.SetSource(fileStream, videoFile.ContentType);
    }
}
罪魁祸首似乎是最后的“SetSource()”方法。从第一次单击“播放”到下一次单击的唯一变量是变量“VideoPlayer.PlayToSource”,它从空值更改为实值

(作为旁注,变量'VideoPlayer.CurrentState'也从'Closed'更改为'Opening',但在第二次单击之前将自身重置为'Closed'。只有'PlayToSource'更改功能。)

我想我可以通过在我的第一个方法中这样做来快速修复:

  setUpVideo();
  setUpVideo();
  VideoPlayer.Play();
不是很好的代码,但它应该把事情弄清楚,对吗?不!这会导致NullReferenceException。在第二次调用“setUpVideo()”时,我发现“PlayToSource”仍然有一个值,“VideoPlayer.CurrentState”仍然设置为“Opening”。。。它以某种方式触发了NullReferenceException

我希望解决方案是以下事项之一:

1.)在调用“SetSource”之前,在第一次单击时设置“VideoPlayer.PlayToSource”

2.)在快速修复中,在通话之间将“VideoPlayer.CurrentState”设置回“Closed”

3.)模仿第一次点击所做的其他事情

当然,我的两个想法都涉及更改只读变量。这就是我被卡住的地方。我将包括.xaml代码,以便更好地度量,但我相信“SetSource”方法是我的问题的根源:

<Grid x:Name="VideoViewerParentGrid" Background="DarkGreen" Height="{Binding VideoViewerParentGridHeight }" Width="{Binding VideoViewerParentGridWidth}">
    <MediaElement x:Name="VideoPlayer" HorizontalAlignment="Center" VerticalAlignment="Bottom" Stretch="Uniform"
                  Visibility="{Binding VideoVisibility, Converter={StaticResource visibilityConverter}}"/>
    <Button Style="{StaticResource BackButtonStyle}" Tapped="VideoViewerClose_Tapped" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    <Button Name="Play_Button" Content="Play Video" FontSize="26" Tapped="playVideo_Tapped"
            VerticalAlignment="Top" HorizontalAlignment="Left" Height="60" Width="180" Margin="0,80,0,0"/>
</Grid>

----更新----

更多的调查显示,在第一次点击“VideoPlayer.CurrentState”时,从未达到“播放”状态,而是从“打开”状态返回到“关闭”状态。只要程序正在运行,它就不会在后续的任何单击中执行此操作。仍在调查原因。

您缺少“等待”关键字。这样做:-

await setUpVideo();
您缺少“等待”关键字。这样做:-

await setUpVideo();

短版本,此问题通过更改以下内容得到修复:

using (IRandomAccessStream fileStream = await videoFile.OpenAsync(FileAccessMode.Read))
{
  VideoPlayer.SetSource(fileStream, videoFile.ContentType);
}
…就是这样:

IRandomAccessStream fileStream = await videoFile.OpenAsync(FileAccessMode.Read);
VideoPlayer.SetSource(fileStream, videoFile.ContentType);
更长的版本,我的代码失败,因为出现错误“mf_media_engine_err_src_not_supported hresult-0xc00d36c4”,它关闭了我的MediaElement而不是播放它。发生这种情况是因为当我离开“使用”代码块时,“Irand OffStaveStudio”将在我读取文件的中间关闭。我不是100%清楚为什么在第一次运行代码后它就完成了整个过程,但至少现在它可以可靠地工作了


我还必须在应该得到表扬的地方给予表扬,我在这里找到了这个答案:

简短版本,通过更改以下内容解决了这个问题:

using (IRandomAccessStream fileStream = await videoFile.OpenAsync(FileAccessMode.Read))
{
  VideoPlayer.SetSource(fileStream, videoFile.ContentType);
}
…就是这样:

IRandomAccessStream fileStream = await videoFile.OpenAsync(FileAccessMode.Read);
VideoPlayer.SetSource(fileStream, videoFile.ContentType);
更长的版本,我的代码失败,因为出现错误“mf_media_engine_err_src_not_supported hresult-0xc00d36c4”,它关闭了我的MediaElement而不是播放它。发生这种情况是因为当我离开“使用”代码块时,“Irand OffStaveStudio”将在我读取文件的中间关闭。我不是100%清楚为什么在第一次运行代码后它就完成了整个过程,但至少现在它可以可靠地工作了


我还必须在应该得到表扬的地方给予表扬,我在这里找到了答案:

我添加了它,但不幸的是它的工作方式没有任何变化。我添加了它,但不幸的是它的工作方式没有任何变化。