Text to speech using Google Translate :: why won't this code work?

BrilliSoft

New member
Feb 23, 2012
12
0
0
Visit site
Hi everyone. Has anyone had any luck with using Google's text to speech on WP7? Why won't this code work? Am I missing something obvious?

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;


namespace WCTest
{
    public partial class MainPage : PhoneApplicationPage
    {
        string searchString = "http://translate.google.com/translate_tts?tl=en&q=hello"; 

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            WebClient client = new WebClient();

            client.OpenReadCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    Stream audio = e.Result;
                    mediaElement1.SetSource(audio);
                    mediaElement1.Play();
                }

                client.OpenReadAsync(new Uri(searchString));
            };
            
        }
    }
}
 

thed

New member
Jan 6, 2011
992
3
0
Visit site
I don't have any experience with Google Translate so I don't know if I can help, but it might help others if you provide more information. What happens when you run this code? Does it throw any exceptions? Does the response stream have any data in it?
 

thed

New member
Jan 6, 2011
992
3
0
Visit site
Also, looking at your code, it looks like your call to client.OpenReadAsync is placed inside the event handling method, so it's never making the call. Try moving that line outside the event handling method.
 

BrilliSoft

New member
Feb 23, 2012
12
0
0
Visit site
Thanks for the replies. The saga continues...

The following code works on some of my colleagues' computers, but not on mine... What could cause that? Please let me know if this works for you and if you think it's necessary to save the file to IsolatedStorage...

Code:
namespace PhoneApp1 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
        // Constructor 
        public MainPage() 
        { 
            InitializeComponent(); 
        } 
 
        string searchString = "http://translate.google.com/translate_tts?tl=en&q=it+works"; 
         
        private void button1_Click(object sender, RoutedEventArgs e) 
        { 
            WebClient client = new WebClient(); 
 
            client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)" + " (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; 
 
            client.Headers[HttpRequestHeader.Referer] = "http://translate.google.com"; 
 
            client.OpenReadCompleted += (s, ex) => 
            { 
 
                if (ex.Error == null) 
                { 
                    using (var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication()) 
                    { 
                        if (store.FileExists("hello3.mp3")) 
                        { 
                            store.DeleteFile("hello3.mp3"); 
                        } 
                        using (var fs = new System.IO.IsolatedStorage.IsolatedStorageFileStream("hello3.mp3", System.IO.FileMode.Create, store)) 
                        { 
                            byte[] bytesInStream = new byte[ex.Result.Length]; 
                            ex.Result.Read(bytesInStream, 0, (int)bytesInStream.Length); 
                            fs.Write(bytesInStream, 0, bytesInStream.Length); 
                            fs.Flush(); 
 
                            mediaElement1.SetSource(fs); 
                        } 
                    } 
 
                    mediaElement1.Play(); 
 
                } 
 
            }; 
 
            client.OpenReadAsync(new Uri(searchString)); 
        } 
    } 
}
 

BrilliSoft

New member
Feb 23, 2012
12
0
0
Visit site
I don't have any experience with Google Translate so I don't know if I can help, but it might help others if you provide more information. What happens when you run this code? Does it throw any exceptions? Does the response stream have any data in it?
I set a breakpoint right after the if statement and it never files, so Google isn't allowing it...
 

BrilliSoft

New member
Feb 23, 2012
12
0
0
Visit site
Also, looking at your code, it looks like your call to client.OpenReadAsync is placed inside the event handling method, so it's never making the call. Try moving that line outside the event handling method.
Thanks, I spotted that and fixed it, but still no luck :-/.
 

Members online

No members online now.

Forum statistics

Threads
322,915
Messages
2,242,889
Members
428,004
Latest member
hetb