plays gigi d'agostino endlessly in under 50 lines of code
This commit is contained in:
BIN
bla_bla_bla.mp3
Normal file
BIN
bla_bla_bla.mp3
Normal file
Binary file not shown.
10
go.mod
Normal file
10
go.mod
Normal file
@ -0,0 +1,10 @@
|
||||
module futureblog.eu/andre/endless_mp3
|
||||
|
||||
go 1.21.1
|
||||
|
||||
require (
|
||||
github.com/ebitengine/purego v0.4.1 // indirect
|
||||
github.com/hajimehoshi/go-mp3 v0.3.4 // indirect
|
||||
github.com/hajimehoshi/oto/v2 v2.4.2 // indirect
|
||||
golang.org/x/sys v0.7.0 // indirect
|
||||
)
|
||||
10
go.sum
Normal file
10
go.sum
Normal file
@ -0,0 +1,10 @@
|
||||
github.com/ebitengine/purego v0.4.1 h1:atcZEBdukuoClmy7TI89amtqAsJUzDQyY/JU7HaK+io=
|
||||
github.com/ebitengine/purego v0.4.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
|
||||
github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68=
|
||||
github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo=
|
||||
github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo=
|
||||
github.com/hajimehoshi/oto/v2 v2.4.2 h1:uPZq5xEnOv8nIy4eMoDkakLb99YxoNv5XHL7Mm6zHwU=
|
||||
github.com/hajimehoshi/oto/v2 v2.4.2/go.mod h1:tINhdh4kCNJ8N19zqp0Lk/wMFv5WQJYkqnnEZ5W5WtE=
|
||||
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
|
||||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
48
main.go
Normal file
48
main.go
Normal file
@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"io"
|
||||
|
||||
"github.com/hajimehoshi/go-mp3"
|
||||
"github.com/hajimehoshi/oto/v2"
|
||||
)
|
||||
|
||||
//go:embed "bla_bla_bla.mp3"
|
||||
var audioFile embed.FS
|
||||
|
||||
func main() {
|
||||
fileBytes, err := audioFile.ReadFile("bla_bla_bla.mp3")
|
||||
if err != nil {
|
||||
panic("reading bla_bla_bla.mp3 failed: " + err.Error())
|
||||
}
|
||||
|
||||
fileBytesReader := bytes.NewReader(fileBytes)
|
||||
decodedMp3, err := mp3.NewDecoder(fileBytesReader)
|
||||
if err != nil {
|
||||
panic("mp3.NewDecoder failed: " + err.Error())
|
||||
}
|
||||
|
||||
// Remember that you should **not** create more than one context
|
||||
otoCtx, readyChan, err := oto.NewContext(44100, 2, oto.FormatSignedInt16LE)
|
||||
if err != nil {
|
||||
panic("oto.NewContext failed: " + err.Error())
|
||||
}
|
||||
// It might take a bit for the hardware audio devices to be ready, so we wait on the channel.
|
||||
<-readyChan
|
||||
|
||||
player := otoCtx.NewPlayer(decodedMp3)
|
||||
defer player.Close()
|
||||
|
||||
// Play starts playing the sound and returns without waiting for it (Play() is async).
|
||||
player.Play()
|
||||
|
||||
// every time the player finishes playing, we rewind the file and play it again
|
||||
for {
|
||||
if !player.IsPlaying() {
|
||||
player.(io.Seeker).Seek(0, io.SeekStart)
|
||||
player.Play()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user