button stop, pause, resume, exit not function when audio played.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dillmn
    New Member
    • Jun 2015
    • 3

    #1

    button stop, pause, resume, exit not function when audio played.

    I want to make my program read word by word so i use .playsync();

    but why all button cannot functioning? please help me to solve this problem.

    below is my code.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using mshtml;
    using System.Diagnostics;
    using System.Text.RegularExpressions;
    using System.Speech.Synthesis;
    using System.Data.OleDb;
    using System.Windows.Media;
    using System.Media;
    
    
    namespace MalayVersion
    {
        public partial class MalayVersion : Form
        {
            //SpeechSynthesizer reader; //declare the object 
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();
    
    
            public MalayVersion()
            {
                InitializeComponent();
                
                //this.player.LoadCompleted += new AsyncCompletedEventHandler(player_LoadCompleted);
    
                // ---  pronounce word from txtWord  ---
                //connect to database - microsoft access  -- DONE
                //looking for the words  -- DONE
                //take path for the word  -- DONE
                //open file (path)  -- DONE
                //play audio - open file from path(database), read!  -- DONE
            }
    
            private void MalayVersion_Load(object sender, EventArgs e)
            {
                btnStop.Enabled=false;
                webBrowser1.Navigate(txtAddress.Text);
            }
    
            //btnExit
            private void button3_Click(object sender, EventArgs e)
            {
                Environment.Exit(0);
            }
    
            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                System.Threading.Thread.Sleep(1000);
            }
    
            private void btnGo_Click(object sender, EventArgs e)
            {
                //progressBar1.Style = ProgressBarStyle.Marquee;
                //progressBar1.MarqueeAnimationSpeed = 30;
                webBrowser1.Navigate(txtAddress.Text);
            }
    
            // ----  segment the text  ----
            //paragraphs to sentences : find punctuation marks   -- DONE
            //sentences to words : find blank space  -- DONE
            //convert everything into readable text -- DONE
    
            string GetTextRange()
            {
                if (webBrowser1.Document != null)
                {
                    IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
                    if (document != null)
                    {
                        IHTMLBodyElement bodyElement = document.body as IHTMLBodyElement;
                        if (bodyElement != null)
                        {
                            IHTMLTxtRange range = bodyElement.createTextRange();
    
                            if (range != null)
                            {
                                return range.text;
                            }
                        }
                    }
                }
                return null;
            }
    
            void GetSentence()
            {
                string source = webBrowser1.DocumentText;
    
                string[] stringSeparators = { "@", "<p>", "</p>", ".", ":", "\r", "\n", "\t", "?", "!", "<br>", "\"" };
                string[] result;
                
                result = source.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
    
                // Return total elements of the array
                int ttl = result.Length;
    
                for (int i = 0; i < ttl; i++)
                {
                    txtSentence.Text = result[i] ;
                    //MessageBox.Show(result[i]);
                    GetWord();
                }
            }
    
            public static string NumberToWords(int number)
            {
                if (number == 0)
                    return "kosong";
    
                if (number < 0)
                    return "negatif " + NumberToWords(Math.Abs(number));
    
                string words = "";
    
                if ((number / 1000000) > 0)
                {
                    words += NumberToWords(number / 1000000) + " juta ";
                    number %= 1000000;
                }
    
                if ((number / 1000) > 0)
                {
                    words += NumberToWords(number / 1000) + " ribu ";
                    number %= 1000;
                }
    
                if ((number / 100) > 0)
                {
                    words += NumberToWords(number / 100) + " ratus ";
                    number %= 100;
                }
    
                if (number > 0)
                {
                    if (words != "")
                        words += "dan ";
    
                    var unitsMap = new[] { "kosong", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "lapan", "sembilan", "sepuluh", "sebelas", "dua belas", "tiga belas", "empat belas", "lima belas", "enam belas", "tujuh belas", "lapan belas", "sembilan belas" };
                    var tensMap = new[] { "kosong", "sepuluh", "dua puluh", "tiga puluh", "empat puluh", "lima puluh", "enam puluh", "tujuh puluh", "lapan puluh", "sembilan puluh" };
    
                    if (number < 20)
                        words += unitsMap[number];
                    else
                    {
                        words += tensMap[number / 10];
                        if ((number % 10) > 0)
                            words += " " + unitsMap[number % 10];
                    }
                }
    
                return words;
            }
    
            void GetWord()
            {
                string[] delimiterChars = new string[] { " " };
    
                string sentence;
                
                if (txtSentence.Text != "")
                {
                    sentence = txtSentence.Text;
    
                    string[] words = sentence.Split(delimiterChars,
                         StringSplitOptions.RemoveEmptyEntries);
    
                     int countWord = words.Length;
                    //string[] words = sentence.Split(delimiterChars);
    
                     for (int i = 0; i < countWord; i++)
                     {
                         lblWord.Text = words[i];
                         
                         Int32 intValue;
                         if (Int32.TryParse(words[i], out intValue))
                         {
                             int numVal = Convert.ToInt32(words[i]);
                             string str = NumberToWords(numVal);
                             
                             lblWord.Text = str;                         
                             ReadWord();
                         }
                         else
                         {
                             lblWord.Text = words[i];
                             //MessageBox.Show(words[i]);
                             ReadWord();
                         }
                     }
                }
            }
    
            void ReadWord()
            {
                if (txtSentence.Text != "")    //if text area is not empty 
                {
                  /*
                   * search word from lblWord.Text in db
                   * after found the word, take path for that word in path col
                   * open audio file
                   * read the word
                  */
    
                    //set up the connection string
                    string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\Visual Studio 2013\Projects\MalayVersion\MalayDict.accdb";
                    //set up the select statement
                    string SelectCommand = "SELECT path FROM Word where word = '" + lblWord.Text + "'";
                    //set up the connection object&nbsp;using the conn string
                    OleDbConnection Connection = new OleDbConnection(ConnectionString);
                    //set up the data adapter using the select statement and the connection object
                    OleDbDataAdapter Adapter = new OleDbDataAdapter(SelectCommand, Connection);
                    //a new empty dataset
                    DataSet ds = new DataSet();
                    //fill the dataset with a new datatable of all the results
                    Adapter.Fill(ds, "Word");//string is the DataTable name, can be anything
                    //now, let "Table" point to the datatable with our results
                    DataTable Table = ds.Tables["Word"];
                    //get the path into string
                    String strPath = "";
                    strPath = ds.Tables[0].Rows[0]["path"].ToString();
    
                    //System.Media.SoundPlayer player = new System.Media.SoundPlayer();
                    player.SoundLocation = strPath;
                    //player.LoadCompleted += new AsyncCompletedEventHandler(player_LoadCompleted);
                    player.Load();
                    player.PlaySync();
    
                    //player.Open(new Uri(strPath));
                    //player.Play();
    
                   /* reader = new SpeechSynthesizer();
                    reader.Speak(lblWord.Text);
                    //reader.SpeakAsync(lblWord.Text);
                    reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(reader_SpeakCompleted); */
                }
            }
    
    
            private void btnRead_Click(object sender, EventArgs e)
            {
                //GetTextRange();
                btnStop.Enabled = true;
                GetSentence();
            }
    
            private void btnStop_Click(object sender, EventArgs e)
            {
                player.Stop();
            }
    
            private void btnPause_Click(object sender, EventArgs e)
            {
                //player.pause();
            }
    
        }
    }
    Last edited by dillmn; Jun 28 '15, 04:53 PM. Reason: state there is a code
Working...