split player
This commit is contained in:
parent
ea1f6a380a
commit
70c56336e4
34
main.go
34
main.go
|
@ -8,11 +8,9 @@ import (
|
|||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/faiface/beep"
|
||||
"github.com/faiface/beep/mp3"
|
||||
"github.com/faiface/beep/speaker"
|
||||
"player/server"
|
||||
)
|
||||
|
||||
type StreamURL struct {
|
||||
|
@ -30,9 +28,8 @@ var StreamURLs = []StreamURL{
|
|||
|
||||
func main() {
|
||||
|
||||
// setting sample rate
|
||||
sr := beep.SampleRate(48000)
|
||||
if err := speaker.Init(sr, sr.N(time.Second/10)); err != nil {
|
||||
player := server.NewPlayer()
|
||||
if err := player.Init(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
@ -42,15 +39,7 @@ func main() {
|
|||
fmt.Printf("%d: %s\n", i, stream.Name)
|
||||
}
|
||||
|
||||
mix := &beep.Mixer{}
|
||||
|
||||
speaker.Play(mix)
|
||||
|
||||
var oldStreamer beep.StreamCloser
|
||||
var oldCtrl *beep.Ctrl
|
||||
|
||||
for {
|
||||
|
||||
fmt.Println("Press a number to change stations, 9 to quit")
|
||||
|
||||
number, err := getNext()
|
||||
|
@ -75,23 +64,8 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer streamer.Close()
|
||||
ctrl := &beep.Ctrl{Streamer: streamer, Paused: false}
|
||||
mix.Add(ctrl)
|
||||
speaker.Lock()
|
||||
if oldCtrl != nil {
|
||||
oldCtrl.Paused = true
|
||||
oldCtrl.Streamer = nil
|
||||
oldStreamer.Close()
|
||||
}
|
||||
player.Play(streamer)
|
||||
|
||||
ctrl.Paused = true
|
||||
ctrl.Streamer = streamer
|
||||
ctrl.Paused = false
|
||||
speaker.Unlock()
|
||||
|
||||
oldCtrl = ctrl
|
||||
oldStreamer = streamer
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/faiface/beep"
|
||||
"github.com/faiface/beep/speaker"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
oldStreamer beep.StreamCloser
|
||||
oldCtrl *beep.Ctrl
|
||||
mixer *beep.Mixer
|
||||
}
|
||||
|
||||
func NewPlayer() *Player {
|
||||
return &Player{
|
||||
mixer: &beep.Mixer{},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Player) Init() error {
|
||||
sr := beep.SampleRate(48000)
|
||||
if err := speaker.Init(sr, sr.N(time.Second/10)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
speaker.Play(p.mixer)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Player) Play(streamer beep.StreamCloser) {
|
||||
ctrl := &beep.Ctrl{
|
||||
Streamer: streamer,
|
||||
}
|
||||
p.mixer.Add(ctrl)
|
||||
|
||||
speaker.Lock()
|
||||
|
||||
if p.oldCtrl != nil {
|
||||
p.oldCtrl.Paused = true
|
||||
p.oldCtrl.Streamer = nil
|
||||
p.oldStreamer.Close()
|
||||
}
|
||||
|
||||
ctrl.Paused = true
|
||||
ctrl.Streamer = streamer
|
||||
ctrl.Paused = false
|
||||
|
||||
speaker.Unlock()
|
||||
|
||||
p.oldCtrl = ctrl
|
||||
p.oldStreamer = streamer
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package server
|
||||
|
||||
//
|
||||
//type App struct {
|
||||
// *Player
|
||||
//}
|
||||
//
|
||||
//func NewApp() *App {
|
||||
// return &App{
|
||||
// Player: NewPlayer(),
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//func (a *App) Run() error {
|
Loading…
Reference in New Issue