I have Blazor server project for test how to play hls video. I created the video using ffmpeg (test.m3u8 and list of .ts files) and it plays without problems using VLC. To avoid Cors Policy error all video I load to wwwroot/3D folder. Additinally I try this .m3u8 file
with the same result
I use this code :
_hosts.cshtml :
@page "/"
@namespace TestHLS.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = "_Layout";
}
<base href="~/" />
<script src="~/js/video.min.js"></script>
@*<script src="~/js/videojs-contrib-hls.js"></script>*@
<script src="~/js/videojs-http-streaming.js"></script>
<link href="~/js/video-js.css" rel="stylesheet" />
<component type="typeof(App)" render-mode="ServerPrerendered" />
<script>
document.addEventListener('DOMContentLoaded', function () {
var player = videojs('my-video');
player.play();
});
</script>
Index.razor:
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
<video id="my-video" controls
muted="muted"
autoplay="autoplay">
<source src=@videoSrc type="application/x-mpegURL" />
Your browser does not support the video tag
</video>
@code{
string videoSrc = "http://localhost:5293/3D/test.m3u8"; //string videoSrc = "http://localhost:5293/3D/bbk.mp4";
}
and program.cs:
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using TestHLS.Data;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddCors(options =>
{
options.AddPolicy("NewPolicy", builder =>
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".m3u8"] = "application/x-mpegURL"; //application/x-mpegURL
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider
});
var fileProvider3D = new PhysicalFileProvider("D:/3D");
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
FileProvider = fileProvider3D,
RequestPath = "/3D",
});
app.UseRouting();
app.UseCors("NewPolicy");
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
Now I see the initial image and get an error
Uncaught (in promise) DOMException: The play() request was interrupted because the media was removed from the document.
How to solve an issue?
Using below code, then you can play
.m3u8file normally.