Skip to content

Instantly share code, notes, and snippets.

@rikkix
Created March 27, 2021 04:48
Show Gist options
  • Save rikkix/acad285d2d23066df9449d40a7f3547c to your computer and use it in GitHub Desktop.
Save rikkix/acad285d2d23066df9449d40a7f3547c to your computer and use it in GitHub Desktop.
playing mp3 with golang
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/hajimehoshi/oto"
"github.com/tosone/minimp3"
)
func main() {
// open file
file, err := os.Open("example.mp3")
if err != nil {
log.Fatalln(err)
}
defer file.Close()
// new a decoder
dec, err := minimp3.NewDecoder(file)
if err != nil {
log.Fatalln(err)
}
defer dec.Close()
<-dec.Started()
// new a context and a player
var context *oto.Context
if context, err = oto.NewContext(dec.SampleRate, dec.Channels, 2, 1024); err != nil {
log.Fatal(err)
}
var player = context.NewPlayer()
defer player.Close()
// start playing
fmt.Println("Starting!")
io.Copy(player, dec)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment