Question

I have the following piece of code for using text to speech feature in Windows Phone 8. I am using ssml, with bookmarks. But when changing any UI element in the Bookmark event called function, raises 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();
            }

But on running it gives the following error

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.

Any idea how to fix this error, or any work around.

Was it helpful?

Solution

Just got the answer.

Since the synthesizer.SpeakSsmlAsync() is an async function, to perform UI operations Dispatcher has to be used, something like this -

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

OTHER TIPS

It's pretty much unrelated to the speech recognition. It seems that it's related to accessing elements which are on the UI thread from a different thread.

Try this:

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

From AppManifest.xml turn on capability ID_CAP_SPEECH_RECOGNITION.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top