saravanan
saravanan

Reputation: 1092

how to play web url audio files corona sdk?

I am trying to play audio file from web link but it seems not working now. These are the codes I tried:

local birdSound =  audio.loadSound("www.sound.com/birds.mp3")
audio.play(birdSound)

It gives an error

Upvotes: 0

Views: 1024

Answers (2)

Xmobile Vietnam
Xmobile Vietnam

Reputation: 37

Use the sniper below to download your remote file and then save it to localFilename at base dir basedir:

local xmnetwork  = {
    last_error = nil,
    downloadHandlerInProgress = nil,
    downloadHandlerFinished = nil,
    downloadBeginHandler = nil,
    network = require("network")
}

function xmnetwork.download(url, errorHandler,beganHandler, inProgressHandler, endedHandler, localFilename, basedir)
    if( xmnetwork.network == nil) then
        xmnetwork.network = require("network")
    end

    xmnetwork.downloadHandlerInProgress = inProgressHandler
    xmnetwork.downloadHandlerFinished = endedHandler
    xmnetwork.downloadBeginHandler = beganHandler
    xmnetwork.errorHandler = errorHandler
    local function downloadListener( event )
        print("download event:" .. tostring(event))
        if ( event.isError ) then
            print( "Network error!" )
            if( xmnetwork.errorHandler) then
                xmnetwork.errorHandler(event)
            end
        elseif ( event.phase == "began" ) then
            if ( event.bytesEstimated <= 0 ) then
                print( "Download starting, size unknown" )
            else
                print( "Download starting, estimated size (in MB): " .. ( event.bytesEstimated /1024/1024))
            end
            if( xmnetwork.downloadBeginHandler) then
                xmnetwork.downloadBeginHandler(event)
            end
        elseif ( event.phase == "progress" ) then
            if(xmnetwork.downloadHandlerInProgress ) then
                xmnetwork.downloadHandlerInProgress (event)
            end
            if ( event.bytesEstimated <= 0 ) then
                print( "Download progress: " .. event.bytesTransferred )
            else
                print( "Download progress: " .. (event.bytesTransferred / event.bytesEstimated) * 100 .. "'%'")
--                log("xmnetwork.download", "downloading :" .. (event.bytesTransferred / event.bytesEstimated) * 100 .. "'%'")
            end
        elseif ( event.phase == "ended" ) then
            print( "Download complete, total bytes transferred: " .. event.bytesTransferred )
            if(xmnetwork.downloadHandlerFinished) then
                xmnetwork.downloadHandlerFinished(event)
            end
        end
    end

    local params = {}

    -- Tell network.request() that we want the "began" and "progress" events:
    params.progress = "download"

    -- Tell network.request() that we want the output to go to a file:
    params.response = {
        filename = localFilename,
        baseDirectory = basedir
    }

    xmnetwork.network.request( url, "GET", downloadListener,  params )
end

Upvotes: 0

Fuzzy Mind
Fuzzy Mind

Reputation: 101

You can't load in memory a remote audio file with the loadSound API because this function is just intended to load local files. By default the file is searched for in the project folder (system.ResourceDirectory), but changing the baseDir parameter you can also look inside a different local folder.

So to play your remote audio file you should first dowload it in the system.DocumentsDirectory through the network.download API. When it is done you can load it with the loadSound, specifying the correct baseDir.

For details about the network.download API look here

Upvotes: 2

Related Questions