It has been along time since I have last posted a blog post. Sorry about that. Today I'm going to post a small part of my final year research project.
My final year research project which was a group project, was to build a web browser for windows mobile 7 which is capable of doing some predefined commands according to the human voice, reading out a web page upon a users request and resume capable downloading. Also it had the other features like bookmarks, history etc like the other normal browsers had.
So my part in the project was to build the "reading out a web page upon users request". To start off I had no clue on how to do it. As in my previous blog posts i knew how to do it in the windows mode, so I thought it might work for windows mobile 7 as well. Surprisingly the windows phone 7.1 SDK didn't have the System.Speech. So I had to think about another way solve this problem. Therefore by trying out different things and reading lots of articles on the net I came up with my own version of doing it.
Solution Summary
I used a WCF service to extract the HTML from the web page that the user is requesting to read out and then I wrote a recursive function which will clean up the unwanted stuff (images,banners,flash) in the extracted HTML and then read out the cleaned HTML into a byte stream and return it back to the phone and convert the byte stream to WMA format and play it out.
I will explain code I have written for it below.
Step 1 : (Click to enlarge the image)
![]() |
HTML Extraction Method |
To briefly explain the above code,
- The method expects the the URL of the web page that needs to be read out.
- I created a object from the "WebClient" class which is in the System.Net Namespace and use the "DownloadString" method to download the HTML of the requested web page as a string.
- Then I created a object out of the "SpeechSynthesizer" class which is in the System.Speech.Synthesis Namespace to help me with the voice part.
- Then I use the memory stream to hold on to the voice which will be read out by the "SpeechSynthesizer".
- Then the output of the "SpeechSynthesizer" is set to the wave stream by calling the method "SetOutputToWaveStream" and by passing it the memory stream object.
- In this case I'm only reading out to the user the content what are available in the paragraph tags of the extracted HTML. Therefor I take a sub string of the extracted HTML and then send it in to the recursive function for further to be cleaned up.
- Then for every paragraph the sub string method gives me, I call the "filterString" method which is the recursive method to clean up the unnecessary HTML tags within the extracted paragraph line. The "filterString" method will return the cleaned up string and it will be appended to the "toSpeak" object which is of type "PromptBuilder".
- Once the loop is done the "Speak" method is called and the voice is read out to the memory stream.
- Then I call the "ToArray" method of the memory stream object which converts the voice to a byte array.
- Remember to Flush and Close the memory stream.
- Finally I return the byte stream back to the phone.
Step 2 : (Click to Enlarge)
![]() |
The Recursive Method |
- The filterString method which is the recursive method, expects the string that needs to be cleaned up. The point to keep in mind is that the string which is passed into the method is the content within the paragraph tags of the extracted HTML.
- It will simply remove the content within the "<" and ">" signs and the signs itself.
- If no "<" sign is contained in the string then it is considerd that the string dosent have any HTML tags in it.
- Finally after the recursive method ends it returns the string without any HTML tags.
This is the way how I did it. There may be other more accurate solutions than mine. Just put the thinking cap on and think.
Thats all for this post.
Happy Coding.