Hello mates, is it posssible to play a music in Golang simple as in C++ function PlaySound(audiofile, NULL, SND_SYNC)
?
I am developinga CLI app and I would like to run a music (mp3/wav) without the need of external dependecies to be downloaded by the user.
1 Like
Ok, I found something that worked very well:
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/hajimehoshi/oto"
"github.com/hajimehoshi/go-mp3"
)
func run() error {
f, err := os.Open("AudioFile.mp3")
if err != nil {
return err
}
defer f.Close()
d, err := mp3.NewDecoder(f)
if err != nil {
return err
}
c, err := oto.NewContext(d.SampleRate(), 2, 2, 8192)
if err != nil {
return err
}
defer c.Close()
p := c.NewPlayer()
defer p.Close()
fmt.Printf("Length: %d[bytes]\n", d.Length())
if _, err := io.Copy(p, d); err != nil {
return err
}
return nil
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
2 Likes
I had same problem . but now i solved my problem
1 Like
Glad you solved the problem
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.