我正在尝试将我的 WP7 应用程序移植到 android。我正在使用 Bing 翻译服务来下载和播放特定单词/短语的音频。我怎么能在android中做到这一点?在 bing 中,流以 .wav 文件的形式出现。这是我的 WP7 代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
this.Speak();
}
public void Speak()
{
string appId = "Your ID";
string text = "Speak this for me";
string language = "en";
string uri = "http://api.microsofttranslator.com/v2/Http.svc/Speak?appId=" + appId +
"&text=" + text + "&language=" + language + "&file=speak.wav";
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(uri));
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null) return;
var sound = e.Result;
Player.Source = null;
string filename = "MyAudio";
using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
{
bool fileExists = userStoreForApplication.FileExists(filename);
if (fileExists)
{
userStoreForApplication.DeleteFile(filename);
}
var isolatedStorageFileStream = userStoreForApplication.CreateFile(filename);
using (isolatedStorageFileStream)
{
SaveFile(e.Result, isolatedStorageFileStream);
if (e.Error == null)
{
Player.SetSource(isolatedStorageFileStream);
}
}
}
}
public static void SaveFile(System.IO.Stream input, System.IO.Stream output)
{
try
{
byte[] buffer = new byte[32768];
while (true)
{
int read = input.Read(buffer, 0, buffer.Length);
if (read <= 0)
{
return;
}
output.Write(buffer, 0, read);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
void mysound_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show(e.ErrorException.Message);
}
void mysound_MediaOpened(object sender, RoutedEventArgs e)
{
Player.Play();
}