0

我正在开发一个 TTS(文本到语音)应用程序,我需要展示每个语音的持续时间。问题是它总是返回零。任何帮助都是最受欢迎的。谢谢。

    SpeechSynthesizer synthesizer = new SpeechSynthesizer();
    MediaPlayerElement mediaPlayerElement = new MediaPlayerElement();
    SpeechSynthesisStream stream = await synthesizer.SynthesizeTextToStreamAsync("Some text...");
    mediaPlayerElement.Source = MediaSource.CreateFromStream(stream, stream.ContentType);
    mediaPlayerElement.MediaPlayer.Play();
    string strDuration = mediaPlayerElement.MediaPlayer.PlaybackSession.NaturalDuration.Milliseconds.ToString();
    MessageDialog msgDlg = new MessageDialog(strDuration);
    await msgDlg.ShowAsync();
4

1 回答 1

1

MediaPlayerElement.MediaPlayer.PlaybackSession.NaturalDuration 始终返回零

我看到了,看来我们无法 MediaPlayer.PlaybackSession.NaturalDuration.Milliseconds获取方法。我将报告说,目前我们有一个解决方法来获得NaturalDuration事件 NaturalDurationChanged

private async void Button_Click(object sender, RoutedEventArgs e)
{
    SpeechSynthesizer synthesizer = new SpeechSynthesizer();
    MediaPlayerElement mediaPlayerElement = new MediaPlayerElement();
    SpeechSynthesisStream stream = await synthesizer.SynthesizeTextToStreamAsync("say hello ");
    mediaPlayerElement.Source = MediaSource.CreateFromStream(stream, stream.ContentType);
    mediaPlayerElement.MediaPlayer.Play();
    mediaPlayerElement.MediaPlayer.PlaybackSession.NaturalDurationChanged += PlaybackSession_NaturalDurationChanged; ;

}

private async void PlaybackSession_NaturalDurationChanged(Windows.Media.Playback.MediaPlaybackSession sender, object args)
{
    var duration = sender.NaturalDuration;
    MessageDialog msgDlg = new MessageDialog(duration.TotalSeconds.ToString()+"s");
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
     {
         await msgDlg.ShowAsync();

     });

}
于 2021-06-29T03:22:37.873 回答