Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I simultaneously read and write to a stream for playing media file in Silverlight 4 MediaStreamSource?
    text
    copied!<p><strong>BACKGROUND</strong></p> <p>I have a media file that I am progressively downloading to my Silverlight 4 application, using WebClient.OpenReadAsync/OpenReadCompleted, and Stream.BeginRead/AsyncCallback. The goal is to play the file in a MediaElement by calling the SetSource method, passing in an instance of our custom MediaStreamSource, so that the file can start playing before the entire contents of the file have been downloaded. The media file is using custom encoding/decoding, which is why we are using a custom MediaStreamSource. Our MediaStreamSource is built to accept a Stream and begin parsing track information and play back in a MediaElement. I have confirmed I am progressively downloading the file contents. Here is a summary of the download code:</p> <pre><code>public void SetSource(string sourceUrl) { var uriBuilder = new UriBuilder(sourceUrl); WebClient webClient = new WebClient(); // AllowReadStreamBuffering = false allows us to get the stream // before it's finished writing to it. webClient.AllowReadStreamBuffering = false; webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); webClient.OpenReadAsync(uriBuilder.Uri); } void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { _inboundVideoStream = e.Result; BeginReadingFromStream(); } private void BeginReadingFromStream() { if (_inboundVideoStream.CanRead) { _chunk = new byte[_chunkSize]; _inboundVideoStream.BeginRead(_chunk, 0, _chunk.Length, new AsyncCallback(BeginReadCallback), _inboundVideoStream); } } private void BeginReadCallback(IAsyncResult asyncResult) { Stream stream = asyncResult.AsyncState as Stream; int bytesRead = stream.EndRead(asyncResult); _totalBytesRead += bytesRead; if (_playableStream == null) _playableStream = new MemoryStream(); _playableStream.Write(_chunk, 0, _chunk.Length); if (!_initializedMediaStream &amp;&amp; _playableStream.Length &gt;= _minimumToStartPlayback) { _initializedMediaStream = true; // Problem: we can't hand the stream source a stream that's still being written to // It's Position is at the end. Can I read and write from the same stream or is there another way MP4MediaStreamSource streamSource = new MP4MediaStreamSource(_playableStream); this.Dispatcher.BeginInvoke(() =&gt; { mediaElement1.SetSource(streamSource); }); } if (_totalBytesRead &lt; _fileSize) { ReadFromDownloadStream(); } else { // Finished downloading } } </code></pre> <p>I've tried both writing/reading to a MemoryStream simultaneously, as listed above, as well as writing to an IsolatedStorageFile and reading from that file as I'm writing to it. So far I can't find a way to make either approach work.</p> <p><strong>QUESTION:</strong></p> <p>Is there a way to read and write to the same stream? Or is there a standard way to implement this with a stream and MediaStreamSource?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload