Can't scrub through audio/video playing in WebView from MemoryStream on UWP

scb 1 Reputation point
2021-01-11T09:47:11.71+00:00

Hi,
I'm building a Xamarin.Forms app and want to play video and audio files from MemoryStream in a WebView (background: all content will actually be stored encrypted on the device in FileSystem.CacheDirectory).

On UWP, I'm using a UriToStreamResolver to provide a MemoryStream for the loaded resources to my WebView. This works fine except that scrubbing/skipping through audio and video does not work.

I compiled a small test project (without the decryption part) where the UriToStreamResolver looks like this:

public class UriToStreamResolver : IUriToStreamResolver 
    {
        public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
        {
            if (uri == null)
            {
                throw new Exception();
            }
            var path = uri.AbsolutePath;

            return GetContent(path).AsAsyncOperation();
        }

        private async Task<IInputStream> GetContent(string path)
        {
            try
            {
                var localUri = new Uri("ms-appdata://" + path); // e.g. path = "/LocalCache/video.mp4"
                var storageFile = await StorageFile.GetFileFromApplicationUriAsync(localUri);
                using var fileStream = await storageFile.OpenAsync(FileAccessMode.Read);
                // return fileStream .GetInputStreamAt(0);

                using var memoryStream = new InMemoryRandomAccessStream();
                await fileStream.AsStream().CopyToAsync(memoryStream.AsStream());

                return memoryStream.GetInputStreamAt(0);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Invalid path:" + e.Message);
                throw new Exception("Invalid path");
            }
        }
    }

Even returning the file's IRandomAccessStream directly (line 21) prevents scrub/skip ability.

Is this a bug or just the wrong approach?
(I noticed that, when navigating to a video/audio file directly, the method will be called twice for some reason which is, of course, suboptimal when loading the file to memory. When embedding media files with audio/video tag in html, it will only be called once, as expected. The scrubbing problem persists even in this case, however.)

Thanks for your support!
Simon

Developer technologies | .NET | Xamarin
Developer technologies | Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2021-01-11T14:34:07.447+00:00

    Hello @scb Welcome to Microsoft Q&A

    Can't scrub through audio/video playing in WebView from MemoryStream on UWP

    I could reproduce your problem, the src uri cause the problem, In UWP platform, we suggest you use ms-appx:/// uri scheme. During my testing, the progress bar could be seek with the following src.

    <video src="ms-appx:///Assets/files/video.mp4" controls></video>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.