Skip to content

Commit

Permalink
Core API update
Browse files Browse the repository at this point in the history
Added mime unsupported error
  • Loading branch information
dondish committed Oct 18, 2019
1 parent 302df28 commit c67910a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
1 change: 1 addition & 0 deletions core/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Track interface {
GetBitrate() int
GetChannels() int
GetCodec() string
GetDuration() time.Duration
}

// An interface for a track that can be seeked
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module lionplayer
go 1.13

require (
github.com/bwmarrin/discordgo v0.0.0-20191018135108-0364b15ee4ec
github.com/dondish/lionplayer/core v0.0.2-newname
github.com/dondish/lionplayer/youtube v0.0.3-newname
github.com/bwmarrin/discordgo v0.20.0
github.com/dondish/lionplayer/core v0.0.3
github.com/dondish/lionplayer/youtube v0.0.4
github.com/gorilla/websocket v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/sys v0.0.0-20191018095205-727590c5006e // indirect
Expand Down
30 changes: 19 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func init() {
var token string

var ytsrc = youtube.NewSource()
var track *core.PlaySeekable
var track core.PlaySeekable
var lastpacket core.Packet

func main() {
Expand Down Expand Up @@ -240,6 +240,7 @@ func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {

// playSound plays the current buffer to the provided channel.
func playSound(s *discordgo.Session, guildID, channelID, videoId, msgchannel string) (err error) {

// Join the provided voice channel.
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
if err != nil {
Expand All @@ -251,12 +252,28 @@ func playSound(s *discordgo.Session, guildID, channelID, videoId, msgchannel str

// Start speaking.
vc.Speaking(true)

defer func() {
// Stop speaking
vc.Speaking(false)

// Sleep for a specificed amount of time before ending.
time.Sleep(250 * time.Millisecond)

// Disconnect from the provided voice channel.
vc.Disconnect()
}()

trac, err := ytsrc.PlayVideo(videoId)
if err != nil {
return err
}

_, err = s.ChannelMessageSend(msgchannel, fmt.Sprintf("Now Playing - %s - %s [%s]", trac.Title, trac.Author, trac.Duration))
if trac.IsStream {
_, err = s.ChannelMessageSend(msgchannel, fmt.Sprintf("Now Playing - %s - %s [LIVE]", trac.Title, trac.Author))
} else {
_, err = s.ChannelMessageSend(msgchannel, fmt.Sprintf("Now Playing - %s - %s [%s]", trac.Title, trac.Author, trac.Duration))
}
if err != nil {
return err
}
Expand All @@ -283,14 +300,5 @@ func playSound(s *discordgo.Session, guildID, channelID, videoId, msgchannel str

track = nil

// Stop speaking
vc.Speaking(false)

// Sleep for a specificed amount of time before ending.
time.Sleep(250 * time.Millisecond)

// Disconnect from the provided voice channel.
vc.Disconnect()

return nil
}
2 changes: 1 addition & 1 deletion youtube/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/dondish/lionplayer/youtube

go 1.13

require github.com/dondish/lionplayer/core v0.0.2-newname
require github.com/dondish/lionplayer/core v0.0.3

require github.com/dondish/lionplayer/webm v0.0.5-newname

Expand Down
21 changes: 14 additions & 7 deletions youtube/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func (t Track) GetChannels() int {
}

func (t Track) GetCodec() string {
return strings.Split(t.Format.Type, "; ")[1]
return strings.Trim(strings.Split(t.Format.Type, "=")[1], "\"")
}

func (t Track) GetDuration() time.Duration {
return t.Duration
}

// Return a playable of this track that can be played.
Expand All @@ -75,13 +79,16 @@ func (t Track) GetPlaySeekable() (core.PlaySeekable, error) {
} else if size == 0 {
return nil, errors.New("got an empty request")
}
if strings.Split(t.Format.Type, ";")[0] == "audio/webm" {
parser, err := webm.NewParser(res)

parser, err := webm.NewParser(res)
if err != nil {
return nil, err
}

if err != nil {
return nil, err
file, err := parser.Parse()
return file, nil
} else {
return nil, errors.New("mime type not supported")
}

file, err := parser.Parse()
return file, nil
}

0 comments on commit c67910a

Please sign in to comment.