Search code examples
rokubrightscript

Check if HLS Stream is Up and Show Error Message If Not?


Is there an easy way to check if an HLS (M3U8) stream is available and show an error message if not? I'm using the following code to play an M3U8 stream, but if the stream is not available, the "Retrieving" message just displays for an indefinite amount of time.

I'd like to show an error right away if the stream isn't up.

sub onButtonSelected()
  'Ok'
  if m.ButtonGroup.buttonSelected = 0
    m.Video.visible = "true"
    m.Video.control = "play"
    m.Video.setFocus(true)
  'Add error message logic here if stream doesn't play

  'Exit button pressed'
  else
    m.Exiter.control = "RUN"
  end if
end sub

Solution

  • You should observe state of the video player.

    sub onButtonSelected()
      'Ok'
      if m.ButtonGroup.buttonSelected = 0
        m.Video.visible = "true"
        ' This observer should not be set here because it will add new observer every time you
        ' select the button. For demonstration purposes only.
        m.video.observeField("state", "onVideoStateChanged")
        m.Video.control = "play"
        m.Video.setFocus(true)
    
      'Exit button pressed'
      else
        m.Exiter.control = "RUN"
      end if
    end sub
    
    
    sub onVideoStateChanged(event as Object)
        if event.getData() = "error"
            ' Show error dialog here
        end if
    end sub
    

    You can also see the error message/code by checking errorMsg/errorCode fields of the Video node.