当前位置: > > > > 当golang服务器提供服务时,html视频标签不播放m3u8文件
当golang服务器提供服务时,html视频标签不播放m3u8文件
来源:stackoverflow
2024-04-24 21:27:29
0浏览
收藏
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《当golang服务器提供服务时,html视频标签不播放m3u8文件》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
问题内容
我已经从视频生成了 m3u8 文件 (index.m3u8),我想在 html 上播放它。 基本上,我有一个 golang 服务器,它会将 index.m3u8 发送到 html5 中的视频标签,以便在调用 http://127.0.0.1:8200/play 时播放它。
我的 golang 文件:
package main
import(
"fmt"
"net/http"
"html/template"
)
func servehandler(w http.responsewriter, r *http.request){
tmpl := template.must(template.parsefiles("index.html"))
tmpl.execute(w, "videosource/index.m3u8")
}
func main(){
fmt.println("begin listening to port 8200")
server := http.server{
addr: "127.0.0.1:8200",
}
http.handlefunc("/play",servehandler)
server.listenandserve()
}
这是我的 html 文件:
<html>
<body>
<script src="https://cdn.jsdelivr.net/npm/hls.js@canary"></script>
<video id="video" controls autoplay></video>
<script>
if(hls.issupported())
{
var video = document.getelementbyid('video');
var hls = new hls();
hls.loadsource('{{.}}');
hls.attachmedia(video);
hls.on(hls.events.manifest_parsed,function()
{
video.play();
});
}
else if (video.canplaytype('application/vnd.apple.mpegurl'))
{
video.src = '{{.}}';
video.addeventlistener('canplay',function()
{
video.play();
});
}
</script>
当我访问网址(http://127.0.0.1:8200/play)时,控制台中出现的错误是
videosource/index.m3u8:1 Failed to load resource: the server responded with a status of 404 (Not Found)
为了检查该错误不是由路径错误引起的,我尝试将 html 中的 '{{.}}' 替换为完整路径(’videosource/index.m3u8’),并且效果完美。
请指导我并告诉我我的代码有什么问题。
谢谢。
解决方案
您必须将标题设置为正确的类型。 尝试这样的事情:
func serveHandler(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/x-mpegURL")
tmpl := template.Must(template.ParseFiles("index.html"))
tmpl.Execute(w, "videosource/index.m3u8")
}
今天关于《当golang服务器提供服务时,html视频标签不播放m3u8文件》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注米云公众号!
