Skip to content

Commit

Permalink
Add Media Session support (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcraigk authored Oct 18, 2023
1 parent 88d963e commit 53e5942
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions app/javascript/src/coffeescript/player.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ class Player
this._updatePlaylistMode()
this._highlightActiveTrack(true)

# Support next/prev buttons on mobile lock screens
navigator.mediaSession.setActionHandler 'previoustrack', =>
this.previousTrack()
navigator.mediaSession.setActionHandler 'nexttrack', =>
this.nextTrack()
navigator.mediaSession.setActionHandler 'play', =>
this.togglePause()
navigator.mediaSession.setActionHandler 'pause', =>
this.togglePause()
navigator.mediaSession.setActionHandler 'stop', =>
this.togglePause()

# Play first track on the page if no audio playing, not a playlist page, and no track slug in URL
unless @active_track_id or @playlist_mode or this._handleAutoPlayTrack()
if track_id = $('.playable_track').first().data('id')
Expand Down Expand Up @@ -98,8 +110,10 @@ class Player
if @active_track_id
if @audioElement.paused
@audioElement.play()
navigator.mediaSession.playbackState = 'playing'
else
@audioElement.pause()
navigator.mediaSession.playbackState = 'paused'
this._updatePlayButton()
else
this._playRandomShowOrPlaylist()
Expand Down Expand Up @@ -220,6 +234,7 @@ class Player
success: (r) =>
if r.success
this._updateDisplay r
this._updateMediaSession r
this._loadAndPlayAudio r.mp3_url
else
@Util.feedback { alert: "Error retrieving track info" }
Expand Down Expand Up @@ -251,6 +266,45 @@ class Player
else
@$player_detail.html "<a class=\"show_date\" href=\"#{r.show_url}\">#{r.show}</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#{r.venue_url}\">#{@Util.truncate(r.venue)}</a>"

_updateMediaSession: (r) ->
console.log(r)
navigator.mediaSession.metadata = new MediaMetadata
title: r.title,
artist: 'Phish',
album: r.show,
artwork: [
{
src: 'https://phish.in/static/logo-96.png',
sizes: '96x96',
type: 'image/png'
},
{
src: 'https://phish.in/static/logo-128.png',
sizes: '128x128',
type: 'image/png'
},
{
src: 'https://phish.in/static/logo-192.png',
sizes: '192x192',
type: 'image/png'
},
{
src: 'https://phish.in/static/logo-256.png',
sizes: '256x256',
type: 'image/png'
},
{
src: 'https://phish.in/static/logo-384.png',
sizes: '384x384',
type: 'image/png'
},
{
src: 'https://phish.in/static/logo-512.png',
sizes: '512x512',
type: 'image/png'
}
]

_updatePlayButton: ->
if @audioElement.paused
@$playpause.removeClass 'playing'
Expand All @@ -277,6 +331,7 @@ class Player
# alert('Press play button to listen')
else
@audioElement.play()
navigator.mediaSession.playbackState = 'playing'
this._updatePlayButton()

export default Player
Binary file added public/static/logo-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/static/logo-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/static/logo-256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/static/logo-384.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/static/logo-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/static/logo-96.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 53e5942

Please sign in to comment.