How can I read mp4 file to IBuffer RRS feed

  • Question

  • Hi all,

    I am using Windows Store App.  I have a MP4 file . I should read the MP4 file to IBuffer. Then I should use the IBuffer to Stream.

    But it can not work well when I display it in html with video (Display invalid code)

    I do not know what is wrong with this code:

        StorageFile mp4File = item as StorageFile;

        IBuffer newBuffer = await FileIO.ReadBufferAsync(mp4File);

        InMemoryRandomAccessStream inputData = new InMemoryRandomAccessStream();
        IOutputStream outputStream = inputData.GetOutputStreamAt(0);
        DataWriter writer = new DataWriter(outputStream);
        writer.WriteBuffer(newBuffer, 0, newBuffer.Length);
        await writer.StoreAsync();
        await writer.FlushAsync();

    Monday, July 14, 2014 5:12 AM

All replies

  • Please try closing the DataWriter.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, July 14, 2014 12:40 PM
  • Thanks for answering me!
    I have tried to close the DataWriter with DataWriter.Dispose(). But it still can not work well!
    Then I change the other MP4 file to try to display with the video control in html5 by using webView.
    The result is some MP4 file can display well, but some MP4 file can not display.
    I do not know what is wrong with my code.
    Here is my all code. Please help me. Thanks very much.

    // ****** here is the code of xmal.cs

     string idf = "MyTag";
     string path = txtBookPath.Text;
     Uri url = mp4WebView.BuildLocalStreamUri(idf, path);
     // The resolver object needs to be passed in to the navigate call.
     mp4WebView.NavigateToLocalStreamUri(url, myResolver);

    // ****** here is the code of StreamUriWinRTResolver.cs (interface : IUriToStreamResolver)

     // If the item is a file and is the leaf node
     StorageFile mp4file = item as StorageFile;

     IBuffer newBuffer = await FileIO.ReadBufferAsync(mp4file);
     // *****************
     // Here is Decrypt Action
     // I should get the IBuffer to Decrypt the data.
     // *****************

     InMemoryRandomAccessStream inStream = new InMemoryRandomAccessStream();

     DataWriter datawriter = new DataWriter(inStream.GetOutputStreamAt(0));
     datawriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
     //datawriter.ByteOrder = ByteOrder.LittleEndian;

     datawriter.WriteBuffer(newBuffer, 0, newBuffer.Length);

     await datawriter.StoreAsync();
     await datawriter.FlushAsync();


     return inStream;

     catch (Exception ex)

    Tuesday, July 15, 2014 3:04 AM
  • Hi,

    Is there any particular reason why are you reading the file as a byte stream, then trying to pump that back into a Video element source?  Simply set the source on the video element.

    Use the following from the Sample Code:

        function playFromFile() {
            var vid = WinJS.Utilities.query("#playbackVideo")[0],
                Pickers = Windows.Storage.Pickers,
                picker = new Pickers.FileOpenPicker();
            picker.suggestedStartLocation = Pickers.PickerLocationId.videosLibrary;
                function (/*@override*/item) {
                    if (item !== null) {
                        vid.src = URL.createObjectURL(item, { oneTimeOnly: true });


    var vid = WinJS.Utilities.query("#playbackVideo")[0];

    vid.src = URL.createObjectURL(item, { oneTimeOnly: true });

    Is all you should need.





    Monday, August 18, 2014 9:29 PM