0

I access a web service by HttpClient, and get a response that is supposed to contain a audio data (it is a TTS web service called Voice RSS).

I proceed this way :

response = await httpClient.GetAsync("http://api.voicerss.org?key=97a912d2574c4538afbf0919ad1f5402&hl=fr-fr&src=hello");

Then i take the content of the response :

content = response.Content;

stream = content.ReadAsStreamAsync();

result = stream.Result;

Then I really hesitate for the rest : I saw in many forums that I should use a MediaElement and set its source to the stream then call the "play" method, but it doesn't work for me. Others say that I should create a StreamReader to be able to read the stream ...

My goal is to play the sound contained in the response (which can be mp3 , wav etc..).

I don't know if I have to create a MediaElement , an IRandomAccessStream , a IIOStream , or anything else.

I am so confused because I've never found the same issue i'm having.

If you could please help me.

Thanks in advance.

--

Meima

Meima
  • 3
  • 4

1 Answers1

0

Check this blog post: How to Convert byte Array to IRandomAccessStream

He implemented a class MemoryRandomAccessStream : IRandomAccessStream

So you can use it, here is my working code:

private async void Button_Click( object sender, RoutedEventArgs e )
{
    var httpClient = new HttpClient();
    var response = await httpClient.GetAsync("http://api.voicerss.org?key=97a912d2574c4538afbf0919ad1f5402&hl=fr-fr&src=hello");
    var content = await response.Content.ReadAsStreamAsync();
    ME.SetSource(new MemoryRandomAccessStream(content), "");
    ME.Play();
}

where ME is the MediaElement in the XAML code:

<MediaElement x:Name="ME" ../>

PS. After some more search, I found that a BCL developer said that an extension method will be added in Windows 8.1 so that you can do

ME.SetSource(content.AsRandomAccessStream(), "");
Community
  • 1
  • 1
Paul Chen
  • 1,873
  • 1
  • 16
  • 28
  • Hello! You just have made my day !!!! Thanks alot , now it works :) I will upgrade to Windows 8.1 soon , I hope they will add that extension as for other features (like speech synthetiser wich is missing for now) – Meima Jul 23 '13 at 08:26