使用go语言编写一个播放器

使用go语言编写一个播放器,直接看效果
2022-03-11 13-55-03屏幕截图.png
整体思路是使用ffmpeg解码,然后使用xui进行渲染,音频播放暂时还没调通,以下是源码

package main

import (
	"github.com/nareix/joy4/av"
	"github.com/nareix/joy4/av/avutil"
	"github.com/nareix/joy4/cgo/ffmpeg"
	"github.com/nareix/joy4/format"
	"github.com/tenny1225/xui"
	"image"
	"math"
	"time"
)

type TestView struct {
	*xui.View
	img *image.YCbCr
}

func (v *TestView) Draw(canvas xui.XCanvas) {
	if v.img == nil {
		return

	}

	iw, ih := float64((*v.img).Bounds().Dx()), float64((*v.img).Bounds().Dy())
	w, h := v.Window.GetSize()
	s := math.Min(float64(w)/iw, float64(h)/ih)
	canvas.Save()
	canvas.SetScale(s, s)

	canvas.DrawImage(-(iw - float64(w))/2, -(ih - float64(h))/2, (*v.img).SubImage((*v.img).Bounds()))
	canvas.Restore()

}

//func (v *TestView) MeasureSize(w, h float64) (float64, float64) {
//	return math.MaxFloat64, math.MaxFloat64
//}
func NewTestView(v *xui.View) xui.Viewer {

	view := &TestView{v, nil}
	view.Drawer = view.Draw
	//view.Measurer = view.MeasureSize
	return view
}

type TestPage struct {
	xui.BasePage
	Root xui.Viewer
}

func (p *TestPage) GetContentView() xui.Viewer {

	p.Root = NewTestView(&xui.View{

		Top:           10,
		Left:          10,
		FontPath:      "OPPOSans-M.ttf",
		FontSize:      15,
		Title:         "hello world",
		PaddingLeft:   8,
		PaddingTop:    8,
		PaddingRight:  8,
		PaddingBottom: 8,
	})
	return p.Root
}
func (p *TestPage) Create(data map[string]interface{}) {
	go func() {
		avfile, _ := avutil.Open("/home/xz/桌面/movie.mp4")
		streams, _ := avfile.Streams()
		var videoDecoder *ffmpeg.VideoDecoder
		for _, stream := range streams {
			if stream.Type().IsVideo() {
				vstream := stream.(av.VideoCodecData)
				videoDecoder, _ = ffmpeg.NewVideoDecoder(vstream)
				//fmt.Println(vstream.Type(), vstream.Width(), vstream.Height())
			}
		}
		for {

			pkt, e := avfile.ReadPacket()
			if e != nil {
				return
			}

			vframe, e := videoDecoder.Decode(pkt.Data)
			if e == nil {
				v := p.Root.(*TestView)
				v.img = &vframe.Image
			}

			time.Sleep(time.Millisecond * 22)
			//}
		}
		avfile.Close()
	}()

}
func main() {
	ctx := xui.NewXContext()
	ctx.Run(func() {
		w := xui.NewWindow("测试", 400, 400, 500, 500, true, ctx)
		w.AddRoute("test", &TestPage{})
		w.StartPage("test", nil, false)
	})
}

func init() {
	format.RegisterAll()
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值