You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

当HSL流停止播放时,如何刷新Roku应用的视频场景?

Roku直播流冻结问题:检测播放状态并重建ContentNode方案

完全可以通过检测视频播放状态识别冻结情况,再销毁并重建ContentNode来恢复直播流播放。以下是具体实现方案:

实现思路

直播流正常播放时,Video组件的position属性会持续增长;若出现冻结,position会在一段时间内保持不变。我们可以通过定时监控position的变化,结合播放状态判断是否需要重建ContentNode。

修改后的代码实现

<?xml version = "1.0" encoding = "utf-8" ?> 
<component name = "VideoExample" extends = "Scene" >
  <script type = "text/brightscript" >
    <![CDATA[
    m.video = invalid
    m.lastPosition = 0
    m.checkInterval = 5000 ' 每5秒检查一次播放状态
    m.positionCheckTimer = invalid

    sub init()
      m.video = m.top.findNode("exampleVideo")
      ' 监听视频状态变化
      m.video.observeField("state", "onVideoStateChange")
      
      ' 初始化视频内容并启动播放
      setupVideoContent()
      m.video.setFocus(true)
      m.video.control = "play"
      
      ' 启动位置监控定时器
      startPositionCheck()
    end sub

    sub setupVideoContent()
      ' 创建新的ContentNode实例
      videocontent = createObject("RoSGNode", "ContentNode")
      videocontent.live = true
      videocontent.PlayStart = 999999999
      videocontent.streamformat = "hls"
      videocontent.url = "my_m3u8_url"
      
      m.video.content = videocontent
    end sub

    sub startPositionCheck()
      m.positionCheckTimer = createObject("roSGNode", "Timer")
      m.positionCheckTimer.interval = m.checkInterval
      m.positionCheckTimer.observeField("fire", "checkVideoPosition")
      m.positionCheckTimer.control = "start"
    end sub

    sub checkVideoPosition()
      if m.video.state = "playing" then
        currentPos = m.video.position
        ' 连续两次检查位置无变化,判定为冻结
        if currentPos = m.lastPosition then
          print "视频疑似冻结,执行重建操作"
          resetVideoPlayback()
        else
          m.lastPosition = currentPos
        end if
      end if
    end sub

    sub resetVideoPlayback()
      ' 停止定时器与视频播放
      if m.positionCheckTimer <> invalid then
        m.positionCheckTimer.control = "stop"
        m.positionCheckTimer = invalid
      end if
      m.video.control = "stop"
      
      ' 清空旧ContentNode触发销毁
      m.video.content = invalid
      
      ' 重建ContentNode并重启播放
      setupVideoContent()
      m.video.control = "play"
      
      ' 重启监控定时器
      startPositionCheck()
      m.lastPosition = 0
    end sub

    sub onVideoStateChange()
      print "视频状态变更:" + m.video.state
      ' 播放出错时直接触发重建
      if m.video.state = "error" then
        resetVideoPlayback()
      end if
    end sub

    ' 场景销毁时清理资源,避免内存泄漏
    sub onSceneUnload()
      if m.positionCheckTimer <> invalid then
        m.positionCheckTimer.control = "stop"
        m.positionCheckTimer = invalid
      end if
      if m.video <> invalid then
        m.video.control = "stop"
        m.video.content = invalid
      end if
    end sub

    ]]>
  </script>
  <children >
    <Video id = "exampleVideo"/>
  </children>
</component>

关键说明

  • 状态监听:通过observeField监听Video组件的state属性,处理播放错误等异常场景
  • 位置检测:使用定时器定时对比position值,判断是否出现播放冻结
  • 重建逻辑:冻结时停止播放、清空旧ContentNode、创建新节点并重启播放,同时重置监控定时器
  • 资源清理:在场景销毁时清理定时器和视频资源,避免内存泄漏

你可以根据实际直播流的情况,调整checkInterval(检查间隔)的数值,优化冻结检测的灵敏度。

内容的提问来源于stack exchange,提问作者nagualcode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 20:03:19