-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.go
More file actions
45 lines (39 loc) · 721 Bytes
/
Copy pathaudio.go
File metadata and controls
45 lines (39 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"io"
"os"
"github.com/hajimehoshi/go-mp3"
"github.com/hajimehoshi/oto"
)
func PlayMP3File(filename string, pContext *oto.Context) error {
// Open the MP3 file
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
// Decode the MP3 file
d, err := mp3.NewDecoder(file)
if err != nil {
return err
}
// Initialize the player
p := pContext.NewPlayer()
if err != nil {
return err
}
defer p.Close()
// Play the MP3 file
if _, err := io.Copy(p, d); err != nil {
return err
}
return nil
}
func testPlayer(pContext *oto.Context) {
err := PlayMP3File("text-to-speech.mp3", pContext)
if err != nil {
fmt.Println(err.Error())
return
}
}