Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
Alexanderrk
Level 7

[Solved] Custom Playback Screen loops after three streams

Hi there,

I've been trying to use Roku to make an app for a relative who's vision is pretty impaired (ironic, i know). 
I stumbled upon the Custom Playback Screen template and was able to strip out most of the UI so it would basically just be like channel flipping.
Using the left and right keys i'm able to change streams but i'm not able to figure out why it will only loop through three channels.

If anyone is able to figure what's going on, it'd be greatly appreciated.
Here's the main.brs file

Sub RunUserInterface()
    o = Setup()
    o.setup()
    o.paint()
    o.eventloop()
End Sub

Sub Setup() As Object
    this = {
        port:      CreateObject("roMessagePort")
        progress:  0 'buffering progress
        position:  0 'playback position (in seconds)
        paused:    false 'is the video currently paused?
        feedData:  invalid
        playing:   0
        playingPrev: 0
        playlistSize: 0
        canvas:    CreateObject("roImageCanvas") 'user interface
        player:    CreateObject("roVideoPlayer")
        load:      LoadFeed
        setup:     SetupFullscreenCanvas
        paint:     PaintFullscreenCanvas
        create_playlist_text: CreatePlaylistText
drawtext:  false
        eventloop: EventLoop
    }

    this.targetRect = this.canvas.GetCanvasRect()
    this.textRect = {x: 520, y: 480, w: 300, h:200} 

    this.load()
    'Setup image canvas:
    this.canvas.SetMessagePort(this.port)
    this.canvas.SetLayer(0, { Color: "#000000" })
    this.canvas.Show()

    this.player.SetMessagePort(this.port)
    this.player.SetLoop(true)
    this.player.SetPositionNotificationPeriod(1)
    this.player.SetDestinationRect(this.targetRect)
    
    this.player.Play()
    this.playingPrev = this.playing

    return this
End Sub

Sub EventLoop()
    while true
        msg = wait(0, m.port)
        if msg <> invalid
            if msg.isStatusMessage() and msg.GetMessage() = "startup progress"
                m.paused = false
                print "Raw progress: " + stri(msg.GetIndex())
                progress% = msg.GetIndex() / 10
                if m.progress <> progress%
                    m.progress = progress%
                    m.paint()
                end if

            'Playback progress (in seconds):
            else if msg.isPlaybackPosition()
                m.position = msg.GetIndex()
                print "Playback position: " + stri(m.position)

            else if msg.isRemoteKeyPressed()
                index = msg.GetIndex()
                print "Remote button pressed: " + index.tostr()
                if index = 4  '<LEFT>
                    m.playing = m.playing - 1
                    if (m.playing < 0)
                        m.playing = 2
                    endif
                    m.player.SetNext(m.playing)
                    m.player.Play()
                    m.playingPrev = m.playing    
                else if index = 5 '<RIGHT>
                    m.playing = m.playing + 1
                    if (m.playing > 2)
                        m.playing = 0
                    endif
                    m.player.SetNext(m.playing)                    
                    m.player.Play()
                    m.playingPrev = m.playing                    
                else if index = 2 '<Up>
                    if m.drawtext
                        m.playing = m.playing - 1
                        if (m.playing < 0)
                            m.playing = m.playlistSize-1
                        endif
                        m.paint()
                    endif                
                else if index = 3 '<Down>
                    if m.drawtext
                        m.playing = m.playing + 1
                        if (m.playing > m.playlistSize-1)
                            m.playing = 0
                        endif
                        m.paint()
                    endif                
                end if

            else if msg.isPaused()
                m.paused = true
                m.paint()

            else if msg.isResumed()
                m.paused = false
                m.paint()

            end if
        endif
    end while
End Sub

Sub SetupFullscreenCanvas()
    m.canvas.AllowUpdates(false)
    m.paint()
    m.canvas.AllowUpdates(true)
End Sub

Sub PaintFullscreenCanvas()
    splash = []
    list = []

    if m.progress < 100
        progress_bar = {TargetRect: {x: 350, y: 500, w: 598, h: 37}, url: "pkg:/images/progress_bar.png"}
        color = "#00a0a0a0"
        splash.Push({
            url: "pkg:/images/splash.png"
            TargetRect: m.targetRect
        })
        list.Push({
            Text: "Buffering..."
            TextAttrs: { font: "large", color: "#707070" }
            TargetRect: m.textRect
        })        
        if m.progress >= 0 AND m.progress < 20
            progress_bar.url = "pkg:/images/progress_bar_1.png"
            print progress_bar.url
        else if m.progress >= 20 AND m.progress < 40
            progress_bar.url = "pkg:/images/progress_bar_2.png"
            print progress_bar.url
        else if m.progress >= 40 AND m.progress < 75
            progress_bar.url = "pkg:/images/progress_bar_3.png"
            print progress_bar.url
        else
            progress_bar.url = "pkg:/images/progress_bar_4.png"
            print progress_bar.url            
        endif
        list.Push(progress_bar)
        
    end if

if m.drawtext
  textArr = m.create_playlist_text()
  yTxt = 40
color = "#00000000"
index = 0
    list.Push({
            color:"#AA000000"
            TargetRect: {x:80, y:120, w: 300, h: 350}
    })
for each str in textArr
   if index = m.playing
     textColor = "#00ff00"
   else
     textColor = "#dddddd"
   endif
    list.Push({
    Text: str
    TextAttrs: {color: textColor, Font:"Medium", HAlign:"Left", VAlign:"VCenter", Direction:"LeftToRight" }
    TargetRect: {x:100, y:yTxt, w: 300, h: 250}
    })
    yTxt = yTxt + 40
    index = index + 1
end for
else
color = "#00000000"
list.Push({
Text: ""
TextAttrs: {font: "medium"}
TargetRect: {x:100, y:600, w: 300, h: 100}
})
endif

    'Clear previous contents
    m.canvas.ClearLayer(0)
    m.canvas.ClearLayer(1)
    m.canvas.ClearLayer(2)    
    m.canvas.SetLayer(0, { Color: color, CompositionMode: "Source" })
    if (splash.Count() > 0)
        m.canvas.SetLayer(1, splash)
        m.canvas.SetLayer(2, list)
    else
        m.canvas.SetLayer(1, list)
    endif
    list.Clear()
    splash.Clear()
End Sub

Function LoadFeed() as void
    jsonAsString = ReadAsciiFile("pkg:/json/feed.json")
    m.feedData = ParseJSON(jsonAsString)
    m.playlistSize = m.feedData.Videos.Count()
    contentList = []
    for each video in m.feedData.Videos
        contentList.Push({
            Stream: { url: video.url }
            StreamFormat: "hls"
        })
    end for    
    m.player.SetContentList(contentList)    
End Function

Function CreatePlaylistText() as object
    textArr = []
    for each video in m.feedData.Videos
        textArr.Push(video.title)
    end for
    return textArr
End Function


and here's the package with the other files
https://www.dropbox.com/s/eap4syn1aiyrta1/manifest.zip?dl=0

Thanks!
0 Kudos
2 Replies
renojim
Level 8

Re: Custom Playback Screen loops after three streams

"Alexanderrk" wrote:
            else if msg.isRemoteKeyPressed()
                index = msg.GetIndex()
                print "Remote button pressed: " + index.tostr()
                if index = 4  '<LEFT>
                    m.playing = m.playing - 1
                    if (m.playing < 0)
                        m.playing = 2
                    endif
                    m.player.SetNext(m.playing)
                    m.player.Play()
                    m.playingPrev = m.playing    
                else if index = 5 '<RIGHT>
                    m.playing = m.playing + 1
                    if (m.playing > 2)
                        m.playing = 0
                    endif
                    m.player.SetNext(m.playing)                    
                    m.player.Play()
                    m.playingPrev = m.playing                    


Should be:
           else if msg.isRemoteKeyPressed()
               index = msg.GetIndex()
               print "Remote button pressed: " + index.tostr()
               if index = 4  '<LEFT>
                   m.playing = m.playing - 1
                   if (m.playing < 0)
                       m.playing = m.playlistSize - 1
                   endif
                   m.player.SetNext(m.playing)
                   m.player.Play()
                   m.playingPrev = m.playing    
               else if index = 5 '<RIGHT>
                   m.playing = m.playing + 1
                   if (m.playing >= m.playListSize)
                       m.playing = 0
                   endif
                   m.player.SetNext(m.playing)                    
                   m.player.Play()
                   m.playingPrev = m.playing                    

Your were constraining m.playing to 0-2.  It looks like Up and Down were trying to do what you want Left/Right to do, but the code isn't there to change to the next/previous stream.

-JT
0 Kudos
Alexanderrk
Level 7

Re: Custom Playback Screen loops after three streams

That's awesome, thank you so much!
0 Kudos