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: 
Photovor
Level 7

Screengraph and AsyncGetToFile

Hello, I've created a program using screengraph, and I'm running into trouble getting notifications from urlEvent when using AsyncGetToFile. I've created a task that looks like this:

<component name="DownloadAssets" extends="Task">
  <interface>
   <field id = "downloadRequestID" type = "string" />
   <field id = "downloadStatus" type = "string" />
  </interface>
  <script type="text/brightscript">
    <![CDATA[
      sub init()
        m.top.functionName = "go"
        m.pendingXfers = {}
        m.top.downloadStatus = "start"
      end sub

      sub go()
        Print "Downloading Assets"
        m.top.downloadStatus = "start"
        downloadURL = "http://www.something.com/video.mp4"
        xfer = CreateObject("roUrlTransfer")

        xfer.SetUrl (downloadURL)
        xfer.AsyncGetToFile("tmp:/video.mp4")
        requestId = xfer.GetIdentity().ToStr()
        m.top.downloadRequestID = requestId
        m.pendingXfers[requestId] = xfer

      end sub

      Function HandleUrlEvent(event as Object)
          requestId = event.GetSourceIdentity().ToStr()
          xfer = m.pendingXfers[requestId]
          if xfer <> invalid then
              m.top.downloadStatus = "done"
          end if
      End Function

    ]]>
  </script>
</component>


I'm able to trigger the task, and I have an observer on the downloadStatus field which works (from testing by setting manually at the end of the go sub) but I never get a notification that the download completes. I'm not sure where I should put my handler for the urlEvent, if it needs to be in the task, or in the screen's brs file.
0 Kudos
1 Reply
renojim
Level 8

Re: Screengraph and AsyncGetToFile

I can't say what may or may not be wrong with what you're doing, but see tim_beynart's excellent tutorial (especially Lesson 3) for an example of async requests in a task.

-JT
0 Kudos