0
votes

I am reading a wav file saved as a byte stream from a web service and want to play it back when my record is displayed. Phone 7 app.

My approach has been to save the byte stream to a wav file in isolated storage upon navigating to the record and subsequently set the source of my media player (MediaElement1) to that source when a button is clicked and play it back.

Below is my current code in my "PlayButton". (size matches byte stream but no audio results). If I set the stream to a WAV file stored as a resource it does work so perhaps I just need to know how to set the Uri to the Isolated storage file.

(e.g. following code works)

Mediaelement1.Source = new Uri("SampleData\\MyMedia.wav",UriKind.Relative) Works
Mediaelement1.Position = new TimeSpan(0,0,0,0) ;
Mediaelement1.Play() ;

Here is my code sample... any ideas?

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication() ;
IsolatedStorageFileStream str = new IsolatedStorageFileStream(
    "MyMedia.wav", FileMode.Open, isf) ;
long size = str.Length;
mediaelement mediaelement = new MediaElement() ;
mediaelement.SetSource(str) ;
mediaElement1.Source = mediaelement.Source ;
mediaElement1.Position = new TimeSpan(0, 0, 0, 0);
mediaElement1.Play();
2
I dont really know what is the question.Lukasz Madon

2 Answers

2
votes

You shouldn't have to create 2 media elements. Just call .SetSource on mediaElement1 directly.

I have similar code which sets the MediaElement source to a movie in isolated storage and that works fine:

using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var isfs = new IsolatedStorageFileStream("trailer.wmv", FileMode.Open, isf))
    {
        this.movie.SetSource(isfs);
    }
}

With the above, movie is a MediaElement I've already created in XAML and set autoPlay to true.

I did have a few issues with the above when first getting it working.

I suggest trying the following to help debug:

  1. Ensure that the file has been written to isolated storage correctly and in it's entirety.

  2. Handle the MediaFailed event to find out why it isn't working.

0
votes

One thing I noticed is that when the device is tethered to the computer the Audio doesn't work... Spent a couple hours with this one when trying to listen to mp3 files.