From 158b7623ba83fd34644a0fa422245d825287a7e7 Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Sat, 14 May 2022 22:30:29 -0400 Subject: [PATCH] Play song when selected --- source/Main.brs | 10 ++++++---- source/ShowScenes.brs | 38 ++++++++++++++++++++++++++++++++++++++ source/api/Items.brs | 28 ++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/source/Main.brs b/source/Main.brs index 2806b530..ef1c83f1 100644 --- a/source/Main.brs +++ b/source/Main.brs @@ -158,6 +158,8 @@ sub Main (args as dynamic) as void group = CreateMusicArtistDetailsGroup(selectedItem.json) else if selectedItem.type = "MusicAlbum" group = CreateMusicAlbumDetailsGroup(selectedItem.json) + else if selectedItem.type = "Audio" + group = CreateAudioPlayerGroup(selectedItem.json) else ' TODO - switch on more node types message_dialog("This type is not yet supported: " + selectedItem.type + ".") @@ -185,10 +187,10 @@ sub Main (args as dynamic) as void node = albums.musicArtistAlbumData.items[ptr[1]] group = CreateMusicAlbumDetailsGroup(node) else if isNodeEvent(msg, "musicSongSelected") - - ' Enjoy the silence - message_dialog("Playing songs is not yet supported.") - + ' If you select a Song from ANYWHERE, follow this flow + selectedIndex = msg.getData() + songs = msg.getRoSGNode() + group = CreateAudioPlayerGroup(songs.MusicArtistAlbumData.items[selectedIndex]) else if isNodeEvent(msg, "episodeSelected") ' If you select a TV Episode from ANYWHERE, follow this flow node = getMsgPicker(msg, "picker") diff --git a/source/ShowScenes.brs b/source/ShowScenes.brs index 817c816d..2196f489 100644 --- a/source/ShowScenes.brs +++ b/source/ShowScenes.brs @@ -342,6 +342,7 @@ function CreateMusicAlbumDetailsGroup(album) group.itemContent = ItemMetaData(album.id) group.musicArtistAlbumData = MusicSongList(album.id) + ' Watch for user clicking on a song group.observeField("musicSongSelected", m.port) return group @@ -398,6 +399,43 @@ function CreateVideoPlayerGroup(video_id, mediaSourceId = invalid, audio_stream_ return video end function +sub controlaudioplay() + if (m.audio.state = "finished") + m.audio.control = "stop" + m.audio.control = "none" + end if +end sub + +' Play Audio +function CreateAudioPlayerGroup(audio) + print "[INFO] Playing ", audio.title + + songData = AudioItem(audio.id) + + m.audio = createObject("RoSGNode", "Audio") + m.audio.observeField("state", "controlaudioplay") + m.audio.content = createObject("RoSGNode", "ContentNode") + + params = {} + + params.append({ + "Static": "true", + "Container": songData.mediaSources[0].container, + }) + + params.MediaSourceId = songData.mediaSources[0].id + + m.audio.content.url = buildURL(Substitute("Audio/{0}/stream", audio.id), params) + m.audio.content.title = audio.title + m.audio.content.streamformat = songData.mediaSources[0].container + + m.audio.control = "stop" + m.audio.control = "none" + m.audio.control = "play" + + return "" +end function + function CreatePersonView(personData as object) as object person = CreateObject("roSGNode", "PersonDetails") m.global.SceneManager.callFunc("pushScene", person) diff --git a/source/api/Items.brs b/source/api/Items.brs index 5951e12b..6ae96a21 100644 --- a/source/api/Items.brs +++ b/source/api/Items.brs @@ -131,8 +131,11 @@ function ItemMetaData(id as string) tmp.json = data return tmp else if data.type = "Audio" - print "Items.brs::ItemMetaData for Audio not yet supported" - return data + ' User clicked on a song and wants it to play + tmp = CreateObject("roSGNode", "MusicSongData") + tmp.image = PosterImage(data.id) + tmp.json = data + return tmp else print "Items.brs::ItemMetaData processed unhandled type: " data.type ' Return json if we don't know what it is @@ -184,6 +187,27 @@ function MusicSongList(id as string) return data end function +' Get Songs that are on an Album +function AudioItem(id as string) + url = Substitute("Users/{0}/Items/{1}", get_setting("active_user"), id) + resp = APIRequest(url, { + "UserId": get_setting("active_user"), + "includeitemtypes": "Audio" + "sortBy": "SortName" + }) + + data = getJson(resp) + results = [] + for each item in data.Items + tmp = CreateObject("roSGNode", "MusicSongData") + tmp.image = PosterImage(item.id) + tmp.json = item + results.push(tmp) + end for + data.Items = results + return data +end function + ' Seasons for a TV Show function TVSeasons(id as string) url = Substitute("Shows/{0}/Seasons", id)