I want to play video in browser using asp.net core
in html I have
<video width="320" height="240" controls>
<source src="http://localhost:55193/api/VideoPlayer/Download" type="video/mp4">
Your browser does not support the video tag.
</video>
and in asp.net core 2
[HttpGet]
[Route("Download")]
public async Task<IActionResult> Download()
{
var path = @"d:\test\somemovie.mp4";
var memory = new MemoryStream();
using (var stream = new FileStream(@"d:\test\somemovie.mp4", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 65536, FileOptions.Asynchronous | FileOptions.SequentialScan))
{
await stream.CopyToAsync(memory);
}
memory.Position = 0;
return File(memory, "application/octet-stream", Path.GetFileName(path));
}
Does this code play file by streaming( I mean buffer file chunk by chunk and play)?
And if I want to play file from any position which user set the player's progress, How I can do it?
Just use the normal return PhysicalFile
here:
public class HomeController : Controller
{
public IActionResult Download()
{
return PhysicalFile(@"d:\test\somemovie.mp4", "application/octet-stream");
}
Because it supports range headers which are necessary for streaming and resuming a file download:
Also return File
, FileStreamResult
and VirtualFileResult
support partial range requests
too. Even static files middleware
supports that too.
Something is wrong. My sample doesn't support resume
[HttpGet]
[Route("Download2")]
public IActionResult Download2()
{
return PhysicalFile(@"d:\test\somemovie.mp4", "application/octet-stream");
}
and there is no accept-ranges in response headers
but when I use
[HttpGet]
[Route("Download")]
public async Task<IActionResult> Download()
{
var path = @"d:\test\somemovie.mp4";
var memory = new MemoryStream();
using (var stream = new FileStream(@"d:\test\somemovie.mp4", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 65536, FileOptions.Asynchronous | FileOptions.SequentialScan))
{
await stream.CopyToAsync(memory);
}
memory.Position = 0;
return File(memory, "application/octet-stream", Path.GetFileName(path),true); //enableRangeProcessing = true
}
with "enableRangeProcessing" parameter true
Can you provide more explanation why the case is this? And which solution I should use? I got confused.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With