Answered by:
Trouble Implementing a Custom IRandomAccessStream to source MediaElement

Question
-
I'm trying to write a class that implements IRandomAccessStream that I can feed to a MediaElement via SetSource, but no matter what I do the MediaElement either rejects the stream format or crashes.
As a control, I tried making a simple wrapper that takes a Stream and implements IRandomAccessStream. The implementation of ReadAsync just does this:
IAsyncOperationWithProgress<IBuffer, uint> IInputStream.ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) { Func<CancellationToken, IProgress<uint>, Task<IBuffer>> taskProvider = (token, progress) => { Func<IBuffer> readFunc = () => { int bytesRead; byte[] result = new byte[count]; bytesRead = this._Stream.Read(result, 0, (int)count); this.Position += bytesRead; result.CopyTo(buffer); buffer.Length = (uint)bytesRead; return buffer; }; return Task.Run(readFunc); }; return AsyncInfo.Run(taskProvider); }
The code works perfectly fine to set the source for BitmapImage's, but for inexplicable reasons, not for MediaElement.
I've read elsewhere that MediaElement cannot work with an in-memory stream (which makes no sense...) but this is not even that case. Even if I seed my custom IRandomAccessStream with a normal Stream obtained from a StorageFile, the MediaElement still fails to accept the stream source.
I should also add that debugging shows the MediaElement is acting very oddly. For example, it first reads 1024 bytes, then seeks back to position 0, then reads 2048 bytes, then again seeks back to position 0, then reads 4108 bytes, then AGAIN seeks back to position 0, etc...
Again, the class works fine in other contexts requiring an IRandomAccessStream, including BitmapImage, so it has to be something unique to MediaElement.
Can anyone explain what's going wrong here? Thanks much!
- Edited by peter_legistek Saturday, November 22, 2014 5:37 PM
Saturday, November 22, 2014 5:29 PM
Answers
-
Oops, please disregard for now. There was a bug elsewhere that was not properly updating the stream position.
- Marked as answer by peter_legistek Sunday, November 23, 2014 8:43 PM
Saturday, November 22, 2014 8:25 PM
All replies
-
Oops, please disregard for now. There was a bug elsewhere that was not properly updating the stream position.
- Marked as answer by peter_legistek Sunday, November 23, 2014 8:43 PM
Saturday, November 22, 2014 8:25 PM -
Hi Peter_legistek,
I am so glad you have solved your issue. But it is more thankful if you can post how you solve it. This will help other community members who has similar questions.
Regards,
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.
Monday, November 24, 2014 2:05 AMModerator -
Sorry Herro I should have been more specific. The bug was totally unrelated to the code I posted. Specifically it was in my IRandomAccessStream.Seek method. I don't even remember what it was now. :) But the above code works and indeed you CAN pass a (correct!) custom implementation of IRandomAccessStream to a MediaElement and it works beautifully.
Wednesday, January 28, 2015 3:38 PM