登录
首页 >  Golang >  Go问答

当golang服务器提供服务时,html视频标签不播放m3u8文件

来源:stackoverflow

时间:2024-04-24 21:27:29 456浏览 收藏

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 文件:


  


当我访问网址(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文件》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>