【问题标题】:System.UnauthorizedAccessException with text-to-speech in Windows Phone 8Windows Phone 8 中带有文本转语音的 System.UnauthorizedAccessException
【发布时间】:2014-08-23 14:00:58
【问题描述】:

我有以下代码用于在 Windows Phone 8 中使用文本到语音功能。我正在使用带有书签的 ssml。但是当在 Bookmark 事件调用函数中更改任何 UI 元素时,会引发 Unauthorized Exception。

private void Initialise_synthesizer()
        {
            this.synthesizer = new SpeechSynthesizer();

            synthesizer.BookmarkReached += new TypedEventHandler<SpeechSynthesizer, SpeechBookmarkReachedEventArgs>
                (BookmarkReached);
        }

void BookmarkReached(object sender, SpeechBookmarkReachedEventArgs e)
        {
            Debugger.Log(1, "Info", e.Bookmark + " mark reached\n");

            switch (e.Bookmark)
            {
                case "START":
                    cur = start;
                    break;
                case "LINE_BREAK":
                    cur++;
                    break;
                }
**error here**  t1.Text = cur.ToString();
            }

但运行时出现以下错误

A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
Invalid cross-thread access.

知道如何解决此错误,或任何解决方法。

【问题讨论】:

    标签: windows-phone-8 text-to-speech


    【解决方案1】:

    刚刚得到答案。

    由于synthesizer.SpeakSsmlAsync() 是一个异步函数,因此必须使用 Dispatcher 来执行 UI 操作,类似这样 -

    Dispatcher.BeginInvoke(() =>
                    t1.Text = cur.ToString());
    

    【讨论】:

      【解决方案2】:

      这与语音识别几乎无关。似乎它与从不同线程访问 UI 线程上的元素有关。

      试试这个:

      Dispatcher.BeginInvoke(() => 
          {
              t1.Text = cur.ToString();
          }
      );
      

      【讨论】:

        【解决方案3】:

        从 AppManifest.xml 开启能力 ID_CAP_SPEECH_RECOGNITION。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-18
          • 1970-01-01
          相关资源
          最近更新 更多