MediaElement当前状态关闭Unreason

时间:2016-11-07 05:05:54

标签: c# windows-phone win-universal-app mediaelement

我的应用程序已经运行了很多个月,但现在它无法运行。当我处理异常时,我得到:MediaElement.currentState is Closed。得到结果:“媒体播放器不可用”。这是我的代码:

if (mediaElement.CurrentState.Equals(MediaElementState.Playing)) {
    mediaElement.Stop();
}
else {
    try {
        SpeechSynthesisStream stream = await sin.SynthesizeTextToStreamAsync(texto);
        // Send the stream to the media object.
        mediaElement.AutoPlay = true;
        mediaElement.SetSource(stream, stream.ContentType);
        mediaElement.Play();
    }
    catch (System.IO.FileNotFoundException) {
        var messageDialog = new Windows.UI.Popups.MessageDialog("Media Player not avaliable");
        await messageDialog.ShowAsync();
    }
}

1 个答案:

答案 0 :(得分:1)

我已经测试了你的代码,我无法重现你的问题。由于您的代码没有完成,我自己添加了剩余代码,现在可以成功运行。请比较代码段以查找代码是否有问题。您还可以在您的计算机上运行以下简单演示,该演示可以在我的计算机上正常运行,以查看它是否是计算机环境问题。我的测试环境是windows 10 build 14393。

XAML代码

  <MediaElement   x:Name="mediaElement"
        CurrentStateChanged="MediaElement_CurrentStateChanged" Height="200" Width="300" AutoPlay="False"/>
  <Button x:Name="btntest" Click="btntest_Click" Content=" media close test"></Button>

背后的代码

  private async void btntest_Click(object sender, RoutedEventArgs e)
  {
      if (mediaElement.CurrentState.Equals(MediaElementState.Playing))
      {
          mediaElement.Stop();
      }
      else
      {
          try
          { 
              var sin = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
              string texto = "hello world";
              SpeechSynthesisStream stream= await sin.SynthesizeTextToStreamAsync(texto);                  
              // Send the stream to the media object.
              mediaElement.AutoPlay = true;
              mediaElement.SetSource(stream, stream.ContentType);
              mediaElement.Play();
          }
          catch (System.IO.FileNotFoundException)
          {
              var messageDialog = new Windows.UI.Popups.MessageDialog("Media Player not avaliable");
              await messageDialog.ShowAsync();
          }
      }