extract player
This commit is contained in:
parent
70c56336e4
commit
7e737b864d
34
main.go
34
main.go
|
@ -3,22 +3,16 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/faiface/beep/mp3"
|
"player/player"
|
||||||
"player/server"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type StreamURL struct {
|
func main() {
|
||||||
Name string
|
|
||||||
URL string
|
|
||||||
}
|
|
||||||
|
|
||||||
var StreamURLs = []StreamURL{
|
stations := []player.Station{
|
||||||
{Name: "KEXP", URL: "https://kexp-mp3-128.streamguys1.com/kexp128.mp3"},
|
{Name: "KEXP", URL: "https://kexp-mp3-128.streamguys1.com/kexp128.mp3"},
|
||||||
{Name: "StuBru", URL: "http://icecast.vrtcdn.be/stubru-high.mp3"},
|
{Name: "StuBru", URL: "http://icecast.vrtcdn.be/stubru-high.mp3"},
|
||||||
{Name: "StuBru Bruut", URL: "http://icecast.vrtcdn.be/stubru_bruut-high.mp3"},
|
{Name: "StuBru Bruut", URL: "http://icecast.vrtcdn.be/stubru_bruut-high.mp3"},
|
||||||
|
@ -26,16 +20,14 @@ var StreamURLs = []StreamURL{
|
||||||
{Name: "StuBru Hooray", URL: "http://icecast.vrtcdn.be/stubru_hiphophooray-high.mp3"},
|
{Name: "StuBru Hooray", URL: "http://icecast.vrtcdn.be/stubru_hiphophooray-high.mp3"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
radio := player.NewPlayer()
|
||||||
|
if err := radio.Init(); err != nil {
|
||||||
player := server.NewPlayer()
|
|
||||||
if err := player.Init(); err != nil {
|
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Stations:")
|
fmt.Println("Stations:")
|
||||||
for i, stream := range StreamURLs {
|
for i, stream := range stations {
|
||||||
fmt.Printf("%d: %s\n", i, stream.Name)
|
fmt.Printf("%d: %s\n", i, stream.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,20 +44,14 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
stream := StreamURLs[number]
|
station := stations[number]
|
||||||
fmt.Printf("Playing %s\n", stream.Name)
|
fmt.Printf("Playing %s\n", station.Name)
|
||||||
res, err := http.Get(stream.URL)
|
|
||||||
if err != nil {
|
if err := radio.Select(station); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
streamer, _, err := mp3.Decode(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
player.Play(streamer)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,19 @@
|
||||||
package server
|
package player
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/faiface/beep"
|
"github.com/faiface/beep"
|
||||||
|
"github.com/faiface/beep/mp3"
|
||||||
"github.com/faiface/beep/speaker"
|
"github.com/faiface/beep/speaker"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Station struct {
|
||||||
|
Name string
|
||||||
|
URL string
|
||||||
|
}
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
oldStreamer beep.StreamCloser
|
oldStreamer beep.StreamCloser
|
||||||
oldCtrl *beep.Ctrl
|
oldCtrl *beep.Ctrl
|
||||||
|
@ -30,7 +37,23 @@ func (p *Player) Init() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) Play(streamer beep.StreamCloser) {
|
func (p *Player) Select(station Station) error {
|
||||||
|
res, err := http.Get(station.URL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
streamer, _, err := mp3.Decode(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
p.PlayStream(streamer)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Player) PlayStream(streamer beep.StreamCloser) {
|
||||||
ctrl := &beep.Ctrl{
|
ctrl := &beep.Ctrl{
|
||||||
Streamer: streamer,
|
Streamer: streamer,
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package server
|
package player
|
||||||
|
|
||||||
//
|
//
|
||||||
//type App struct {
|
//type App struct {
|
Loading…
Reference in New Issue