Created
March 27, 2021 04:48
-
-
Save rikkix/acad285d2d23066df9449d40a7f3547c to your computer and use it in GitHub Desktop.
playing mp3 with golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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