Click here to Skip to main content
15,892,804 members
Articles / Programming Languages / Javascript

Communicating from the Browser to a Desktop Application

Rate me:
Please Sign up or sign in to vote.
4.84/5 (28 votes)
17 May 2009CPOL15 min read 141.7K   4.8K   107  
A very simple application that uses text to speech to speak out loud the currently selected block of text in your web browser.
// BrowserSpeak
//
// by Mark Gladding
// Copyright 2009 Tumbywood Software
// http://www.text2go.com
//
// You are free to reuse this code in any commercial or non-commercial work.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Speech.Synthesis;

namespace BrowserSpeak.Speech
{
    public class SpeechController
    {
        public static SpeechController Instance
        {
            get { return msInstance; }
        }

        public SynthesizerState State
        {
            get { return mSynthesizer.State; }
        }

        public event EventHandler<StateChangedEventArgs> StateChanged
        {
            add { mSynthesizer.StateChanged += value; }
            remove { mSynthesizer.StateChanged -= value; }
        }

        public void Speak(string text)
        {
            if (mSynthesizer.State != SynthesizerState.Ready)
            {
                StopSpeaking();
            }
            mSynthesizer.SpeakAsync(text);
        }

        public void PauseSpeaking()
        {
            mSynthesizer.Pause();
        }

        public void StopSpeaking()
        {
            mSynthesizer.SpeakAsyncCancelAll();
            if (mSynthesizer.State == SynthesizerState.Paused)
            {
                // Need to resume before the cancel will be processed.
                mSynthesizer.Resume();
            }
        }

        public void ResumeSpeaking()
        {
            mSynthesizer.Resume();
        }

        private SpeechController()
        {
        }

        private SpeechSynthesizer mSynthesizer = new SpeechSynthesizer();
        private static SpeechController msInstance = new SpeechController();
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions