From 963aae78d8ec70feef8d373419abe87f5569f00a Mon Sep 17 00:00:00 2001
From: 1hitsong <3330318+1hitsong@users.noreply.github.com>
Date: Sat, 21 May 2022 11:45:38 -0400
Subject: [PATCH] Add Now Playing scene
---
components/ItemGrid/LoadItemsTask2.brs | 2 +-
components/music/MusicAlbumDetails.xml | 2 +-
components/music/NowPlaying.brs | 101 +++++++++++++++++++++++++
components/music/NowPlaying.xml | 30 ++++++++
source/ShowScenes.brs | 27 ++++---
source/api/Items.brs | 2 +-
6 files changed, 152 insertions(+), 12 deletions(-)
create mode 100644 components/music/NowPlaying.brs
create mode 100644 components/music/NowPlaying.xml
diff --git a/components/ItemGrid/LoadItemsTask2.brs b/components/ItemGrid/LoadItemsTask2.brs
index 1dab4f06..1cfdaef0 100644
--- a/components/ItemGrid/LoadItemsTask2.brs
+++ b/components/ItemGrid/LoadItemsTask2.brs
@@ -53,7 +53,7 @@ sub loadItems()
url = Substitute("Users/{0}/Items/", get_setting("active_user"))
end if
resp = APIRequest(url, params)
- data = getJson(resp)
+ data = getJson(resp)
if data <> invalid
diff --git a/components/music/MusicAlbumDetails.xml b/components/music/MusicAlbumDetails.xml
index 0634f4c8..7e4dc35a 100644
--- a/components/music/MusicAlbumDetails.xml
+++ b/components/music/MusicAlbumDetails.xml
@@ -9,7 +9,7 @@
-
+
diff --git a/components/music/NowPlaying.brs b/components/music/NowPlaying.brs
new file mode 100644
index 00000000..3205f89e
--- /dev/null
+++ b/components/music/NowPlaying.brs
@@ -0,0 +1,101 @@
+sub init()
+ m.top.optionsAvailable = false
+ main = m.top.findNode("toplevel")
+ main.translation = [96, 175]
+
+ m.buttons = m.top.findNode("buttons")
+ m.buttons.buttons = [tr("Previous"), tr("Play/Pause"), tr("Next")]
+ m.buttons.observeField("focusedIndex", "buttonFocusChanged")
+
+ m.buttons.selectedIndex = 1
+ m.buttons.focusedIndex = 1
+ m.buttons.setFocus(true)
+end sub
+
+sub audioIndexChanged()
+ itemContentChanged()
+end sub
+
+' Switch menu shown when button focus changes
+sub buttonFocusChanged()
+ if m.buttons.focusedIndex = m.selectedItem then return
+ m.selectedItem = m.buttons.focusedIndex
+end sub
+
+sub playClicked()
+ if m.top.audio.state = "playing"
+ m.top.audio.control = "pause"
+ else if m.top.audio.state = "paused"
+ m.top.audio.control = "resume"
+ end if
+end sub
+
+sub previousClicked()
+ if m.top.audio.contentIndex > 0
+ m.top.audio.nextContentIndex = m.top.audio.contentIndex - 1
+ m.top.audio.control = "skipcontent"
+ end if
+end sub
+
+sub nextClicked()
+ m.top.audio.control = "skipcontent"
+end sub
+
+' Set values for displayed values on screen
+sub itemContentChanged()
+ item = m.top.itemContent[m.top.audio.contentIndex]
+
+ m.top.findNode("musicartistPoster").uri = item.posterURL
+ m.top.overhangTitle = item.json.AlbumArtist + " / " + item.json.name
+
+ m.top.audio.observeField("contentIndex", "audioIndexChanged")
+ setFieldText("numberofsongs", "Track " + stri(m.top.audio.contentIndex + 1) + "/" + stri(m.top.itemContent.count()))
+ setFieldText("artist", item.json.AlbumArtist)
+ setFieldText("album", item.json.album)
+ setFieldText("song", item.json.name)
+end sub
+
+sub setFieldText(field, value)
+ node = m.top.findNode(field)
+ if node = invalid or value = invalid then return
+
+ ' Handle non strings... Which _shouldn't_ happen, but hey
+ if type(value) = "roInt" or type(value) = "Integer"
+ value = str(value).trim()
+ else if type(value) = "roFloat" or type(value) = "Float"
+ value = str(value).trim()
+ else if type(value) <> "roString" and type(value) <> "String"
+ value = ""
+ end if
+
+ node.text = value
+end sub
+
+function onKeyEvent(key as string, press as boolean) as boolean
+
+ if press and key = "play"
+ playClicked()
+ return true
+ else if press and key = "back"
+ m.top.audio.control = "stop"
+ else if press and key = "rewind"
+ previousClicked()
+ return true
+ else if press and key = "fastforward"
+ nextClicked()
+ return true
+ else if key = "OK" and m.top.findNode("buttons").hasFocus()
+ if m.buttons.buttons[m.selectedItem] = tr("Play/Pause")
+ playClicked()
+ return true
+ else if m.buttons.buttons[m.selectedItem] = tr("Previous")
+ previousClicked()
+ return true
+ else if m.buttons.buttons[m.selectedItem] = tr("Next")
+ nextClicked()
+ return true
+ end if
+ end if
+
+ return false
+end function
diff --git a/components/music/NowPlaying.xml b/components/music/NowPlaying.xml
new file mode 100644
index 00000000..6b45bb7b
--- /dev/null
+++ b/components/music/NowPlaying.xml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/source/ShowScenes.brs b/source/ShowScenes.brs
index a790a767..051dc230 100644
--- a/source/ShowScenes.brs
+++ b/source/ShowScenes.brs
@@ -416,6 +416,9 @@ end function
' Play Audio
function CreateAudioPlayerGroup(audiodata)
+ group = CreateObject("roSGNode", "NowPlaying")
+ songMetaDataArray = CreateObject("roArray", 0, true)
+
if type(audiodata) = "roArray"
' Passed data is an array of audio, setup playback as a playlist
@@ -428,6 +431,8 @@ function CreateAudioPlayerGroup(audiodata)
songContent = audioPlaylistContent.CreateChild("ContentNode")
songData = AudioItem(song.id)
+ songMetaDataArray.push(ItemMetaData(song.id))
+
params = {}
params.append({
@@ -444,10 +449,6 @@ function CreateAudioPlayerGroup(audiodata)
m.audio.content = audioPlaylistContent
- m.audio.control = "stop"
- m.audio.control = "none"
- m.audio.control = "play"
-
else if type(audiodata) = "roSGNode"
' Passed data is a single node
@@ -459,6 +460,8 @@ function CreateAudioPlayerGroup(audiodata)
songData = AudioItem(audiodata.id)
+ songMetaDataArray.push(ItemMetaData(audiodata.id))
+
params = {}
params.append({
@@ -471,14 +474,20 @@ function CreateAudioPlayerGroup(audiodata)
m.audio.content.url = buildURL(Substitute("Audio/{0}/stream", audiodata.id), params)
m.audio.content.title = audiodata.title
m.audio.content.streamformat = songData.mediaSources[0].container
-
- m.audio.control = "stop"
- m.audio.control = "none"
- m.audio.control = "play"
end if
end if
- return ""
+ group.musicArtistAlbumData = audiodata
+ group.audio = m.audio
+ group.audio.control = "stop"
+ group.audio.control = "none"
+ group.audio.control = "play"
+
+ group.itemContent = songMetaDataArray
+
+ m.global.sceneManager.callFunc("pushScene", group)
+
+ return group
end function
function CreatePersonView(personData as object) as object
diff --git a/source/api/Items.brs b/source/api/Items.brs
index 398189dc..b48435ea 100644
--- a/source/api/Items.brs
+++ b/source/api/Items.brs
@@ -211,7 +211,7 @@ function AudioItem(id as string)
tmp.json = data
results.push(tmp)
end if
-
+
data.Items = results
return data
end function