Tuesday, January 29, 2013

SL media element won't play my wave file after the first time

SL media element won't play my wave file after the first time

I use WaveMediaStreamSource (WAVmss) library to play wave files on silverlight media element.
It plays the first time I load the file into the element, but when I try and play it a second time it doesn't play.
I used the sample from here.

Maybe I'm missing something, here is my code where I'm opening wave file and play it successfully for the first time, Here is the file reading and setting as source in my media element:

OpenFileDialog openFileDialog = new OpenFileDialog();     MemoryStream audioSource = new MemoryStream();  if (openFileDialog.ShowDialog() == true)     {        using (FileStream fileStream = openFileDialog.File.OpenRead())        {            audioSource.SetLength(fileStream.Length);            fileStream.Read(audioSource.GetBuffer(), 0, (int)fileStream.Length);        }     }  WaveMSS.WaveMediaStreamSource audioStreamSource =                   new WaveMSS.WaveMediaStreamSource(audioSource);  mediaElement1.SetSource(audioStreamSource); 

And when the first play over (I know it since I'm getting _MediaEnded event) I can't play this video again.

I tried to set the position of the MediaElement but failed to play it again:

mediaElement1.Position = TimeSpan.FromSeconds(0); mediaElement1.Play(); 

I debugged it and the Position property does get set to zero but when I'm trying to click play again position property moves to the end and the _MediaEnded event pops up again.

What can I do?

Answers & Comments...

Answer: 1

In the MediaEnded event, cast "sender" to a MediaElement and call Stop(). This resets the object so you can play it again.

by : LordTakkerahttp://stackoverflow.com/users/1783619

Answer: 2

Thanks to Gilles Khouzam quick response the problem has been solved.

Ok, I found the issue. The implementation of SeekAsync, the position in the stream was changed but the remaining count in the chunk was not. In WaveMediaStreamSource.cs, look for the SeekAsync method and change the code to this (add the MoveToChunkOffest line):

this.currentPosition = this.wavParser.WaveFormatEx.BufferSizeFromAudioDuration( seekToTime ) + this.startPosition; this.wavParser.MoveToChunkOffset( ( uint ) this.wavParser.WaveFormatEx.BufferSizeFromAudioDuration( seekToTime ) ); this.currentTimeStamp = seekToTime; ReportSeekCompleted( seekToTime ); 
by : Dor Cohenhttp://stackoverflow.com/users/871672




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog