-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.go
56 lines (46 loc) · 1.27 KB
/
play.go
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
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"net"
"time"
"github.com/vishen/go-chromecast/application"
)
func Play(sound TtsOutputAttr, settings GoogleHomeSetting) error {
applicationOptions := []application.ApplicationOption{
application.WithDebug(DEBUG),
}
var iface *net.Interface
if settings.Iface != "" {
var err error
if iface, err = net.InterfaceByName(settings.Iface); err != nil {
return fmt.Errorf("unable to find interface %q: %v", settings.Iface, err)
}
applicationOptions = append(applicationOptions, application.WithIface(iface))
}
app := application.NewApplication(applicationOptions...)
err := app.Start(settings.Addr, settings.Port)
if err != nil {
return fmt.Errorf("Start: %v", err)
}
volume := app.Volume().Level
app.SetVolume(settings.Volume)
err = app.Load(sound.FilePath, 0, "audio/wav", false, settings.Detach, settings.ForceDetach)
if err != nil {
return fmt.Errorf("Load: %v", err)
}
timer := time.NewTimer(time.Second * time.Duration(settings.MaxDuration))
stopchan := make(chan bool)
go func() {
app.MediaWait()
stopchan <- true
}()
select {
case <-timer.C:
app.StopMedia()
app.SetVolume(volume)
return fmt.Errorf("The message was too long, so it was interrupted.")
case <-stopchan:
app.SetVolume(volume)
return nil
}
}