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