Expand media key support: add play/pause

This commit is contained in:
Adriano Caloiaro 2024-01-28 08:16:31 -07:00
parent 4c1d5b1111
commit c2a31860fb
No known key found for this signature in database
GPG key ID: C2BC56DE73CE3F75
3 changed files with 28 additions and 5 deletions

View file

@ -89,12 +89,13 @@ func convertToAscii(img image.Image, w, h int) []byte {
return buf.Bytes()
}
// PlayChannel begins streaming the provided channel after fetching its playlist
// Play begins streaming the provided channel after fetching its playlist
// If a channel is already playing, the old stream is stopped first, clearing up resources.
// This function is *asynchronous* and creates a single streaming resource: the audio stream held by the application
// context. To clean up resources created by this function, Close() the application's audio stream.
func PlayChannel(ctx *context.AppContext, chn *components.ChannelItem) {
func Play(ctx *context.AppContext) {
player.Stop(ctx)
chn := ctx.HighlightedChannel
if chn == nil {
ctx.SetStatusMessage("Unable to play channel. Try again.")
@ -125,6 +126,11 @@ func PlayChannel(ctx *context.AppContext, chn *components.ChannelItem) {
}()
}
// Stop stops playback
func Stop(ctx *context.AppContext) {
player.Stop(ctx)
}
// TogglePause pauses/unpauses audio when a channel is playing
func TogglePause(ctx *context.AppContext) {
// nothing to do if nothing has been streamed
@ -137,7 +143,7 @@ func TogglePause(ctx *context.AppContext) {
ctx.AudioStream.Close()
player.Stop(ctx)
} else {
PlayChannel(ctx, ctx.CurrentChannel)
Play(ctx)
}
}

View file

@ -107,7 +107,7 @@ func configureEventHandling() {
} else {
ctx.HighlightedChannel = &ctx.ChannelList[current]
}
app.PlayChannel(ctx, ctx.HighlightedChannel)
app.Play(ctx)
case tcell.KeyRune:
switch event.Rune() {
case 'c':
@ -199,4 +199,10 @@ func FetchFavoritesAndChannels() {
}
ctx.ChannelList = channels
ctx.FavoriteList = favorites
// default the highlighted channel to the first favorite; even before users select a channel manually. This way,
// when di-tui starts and the user presses the "Play" media key, di-tui will start playing the first favorite
// instead of requiring them to choose the channel to be played
highlightedFavorite := ctx.FavoriteList[0]
ctx.HighlightedChannel = difm.FavoriteItemChannel(ctx, highlightedFavorite)
}

View file

@ -84,10 +84,22 @@ func (p Player) PlayPause() error {
}
func (p Player) Stop() error {
app.Stop(p.ctx)
status := map[string]dbus.Variant{
"PlaybackStatus": dbus.MakeVariant(types.PlaybackStatusStopped),
}
s.Conn.Emit("/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties.PropertiesChanged", "org.mpris.MediaPlayer2.Player", status, []string{})
return nil
}
func (p Player) Play() error {
app.Play(p.ctx)
status := map[string]dbus.Variant{
"PlaybackStatus": dbus.MakeVariant(types.PlaybackStatusPlaying),
}
s.Conn.Emit("/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties.PropertiesChanged", "org.mpris.MediaPlayer2.Player", status, []string{})
return nil
}
@ -181,7 +193,6 @@ func Start(ctx *context.AppContext) {
r := Root{}
p := Player{ctx: ctx, metaData: metaData}
s = server.NewServer("di-tui", r, p)
// _ = events.NewEventHandler(s)
go s.Listen()
go func() {
for {