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