Forum Discussion

Rookai's avatar
Rookai
Binge Watcher
12 months ago

Accessing local (tmp:/) audio files to be played by roAudioplayer

Hi,

I have a local mp3 audio file stored in tmp:/ ("tmp:/audio.mp3), and this file is generated from response of an API call " AsyncPostFromFileToFile " . I would like to play it using roAudioPlayer component. When I pass it to roAudioPlayer, it does not play anything; where it plays from an external URL perfectly. This is my script:

' Create the roAudioPlayer object
        wavFilePath = "tmp:/audio.mp3"
        audioPlayer = CreateObject("roAudioPlayer")
        port = CreateObject("roMessagePort")
        audioPlayer.SetMessagePort(port)

        ' Create the associative array for the audio content
        song = CreateObject("roAssociativeArray")
        song.url = wavFilePath
        file = CreateObject("roFileSystem")
        print file.GetVolumeList() 
        print file.GetDirectoryListing("tmp:/") 'tmp:/audio.mp3 exists
        ' Add content to the audio player and start playback
        audioPlayer.addcontent(song)
                ' urlPath = "file://" + wavFilePath
                ' print "urlPath: "; urlPath
        'urlPath = "http://www.sdktestinglab.com/Tutorial/sounds/audionode.mp3"                
        'audioPlayer.setcontentlist([{ url: urlPath }]) ' works when tried individually
        'audioPlayer.addcontent(song)
        audioPlayer.setloop(false)
        audioPlayer.play()

        print "Audio playback triggered for file: " + wavFilePath

        ' Event loop to handle audio player events
        while true
            msg = wait(0, port)
            if type(msg) = "roAudioPlayerEvent"
                if msg.isStatusMessage()
                    print "roAudioPlayerEvent: "; msg.getmessage(); msg.getindex()
                    if msg.getmessage() = "end of playlist"
                        exit while
                    end if
                end if
            end if
        end while

 

This is the consul log for this section (please note I require msg.getindex() for each message to monitor progress; for URL-based mp3 file, it goes 0 to 1000):

roAudioPlayerEvent: prebuffer started 0
roAudioPlayerEvent: startup progress 0
roAudioPlayerEvent: startup progress 66
roAudioPlayerEvent: startup progress 132
roAudioPlayerEvent: startup progress 198
roAudioPlayerEvent: startup progress 264
roAudioPlayerEvent: startup progress 330
roAudioPlayerEvent: prebuffer done 0
roAudioPlayerEvent: startup progress 330
roAudioPlayerEvent: prebuffer done 0
roAudioPlayerEvent: startup progress 330
roAudioPlayerEvent: Content contains no playable tracks. 0
roAudioPlayerEvent: end of playlist 0

 

Answers like following suggest the local path can be fed to roURLTransfer by using prefix "file://", but I had no luck trying it:

https://community.roku.com/t5/Roku-Developer-Program/Simple-Quesiton-on-URLs/m-p/301815/highlight/true#M15085

What am I doing wrong? What should I change to make this work? Any help will be appreciated.

  • renojim's avatar
    renojim
    Community Streaming Expert

    I don't see anything that's obviously wrong in your code and I just tried playing an MP3 from tmp:/ and didn't have a problem.  I suspect your MP3 isn't being properly saved to tmp:/.  AsyncPostFromFileToFile?  Is that what you're using that to save the MP3?  I can't say I've ever used it, but check the file that gets saved to tmp:/ with something like roFileSystem stat and/or read it into an roByteArray to check its contents.

    • Rookai's avatar
      Rookai
      Binge Watcher

      Thanks a lot for your response. I followed your instructions and it seems the file is being created, but only the first 4 characters of the response is being stored in the file. If I understand correctly,

      msg.GetResponseCode()

      returns 200 as soon as the response starts coming in, and it does not wait for the API to return the complete response if the process is not instantaneous. What would be the proper way to make it wait until the response return is complete? I tried a few tweaks but each of them generates new complexities.

       

      • renojim's avatar
        renojim
        Community Streaming Expert

        I don't know what to suggest on the Roku side, but I usually use something like curl to test interfacing to a server.  Something like:

        curl --data-raw @command.txt -o apiresponse.out http://apiurl.com/api

        That should mimic what AsyncPostFromFileToFile does.

        I tried it myself both using curl and on my Roku device connecting to my own server and both worked as expected.

        I assume you're using msg = wait(0,port) to wait for the response.  It shouldn't be returning until the transfer is complete.