0

我正在尝试从网络链接播放音频文件,但现在似乎无法正常工作。这些是我尝试过的代码:

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

它给出了一个错误

4

2 回答 2

2

您无法使用 loadSound API 将远程音频文件加载到内存中,因为此函数仅用于加载本地文件。默认情况下,在项目文件夹 (system.ResourceDirectory) 中搜索该文件,但更改 baseDir 参数您也可以查看不同的本地文件夹。

因此,要播放您的远程音频文件,您应该首先通过 network.download API 在 system.DocumentsDirectory 中下载它。完成后,您可以使用 loadSound 加载它,指定正确的 baseDir。

有关 network.download API 的详细信息,请参见此处

于 2012-10-12T18:39:24.097 回答
0

使用下面的狙击手下载您的远程文件,然后将其保存到localFilename基本目录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
于 2014-10-18T08:47:06.360 回答