在外部文件中更改号码时播放声音

时间:2014-09-18 09:47:40

标签: actionscript-3 flash adobe mp3 audio

播放mp3声音如果服务器上的外部文件中的数字从0更改为1,则可能闪存会在2分钟内读取外部文件更改

1 个答案:

答案 0 :(得分:0)

我认为你在这里真正要求的是与播放mp3无关,而是如何轮询服务器上托管的文件。

首先,您需要某种计时器。当该计时器关闭时,使用URLLoader加载文件。然后检查加载程序的内容以查看该值是否已更改为1:

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;

var currentValue:int = 0;
var fileLoader:URLLoader = new URLLoader();
var fileRequest:URLRequest = new URLRequest("url_of_your_file");
fileLoader.addEventListener(Event.COMPLETE, onLoadComplete);
// you prbably also want to handle errors (if the file doesn't exist)

var pollTimer:Timer = new Timer(120000); // 120,000 milliseconds = 2 minutes
pollTimer.addEventListener(TimerEvent.TIMER, pollFile);

pollFile(null); // initial call to pollFile
pollTimer.start(); // subsequent calls to pollFile

function pollFile(e:TimerEvent):void 
{
    fileLoader.load(fileRequest);
}

function onLoadComplete(e:Event):void 
{
    if(!currentValue && int(fileLoader.data) == 1)
    {
        // the value of the file has changed from 0 to 1 
        // execute your mp3 playing code here
    }
    currentValue = int(fileLoader.data);
}

编辑:阅读您的评论,您的请求可能会被缓存,因此每次都会返回相同的值。请求非缓存数据有一些技巧:

// when you create your request, add a query string (noCache) that will change every time.
var fileRequest:URLRequest = new URLRequest("url_of_your_file?noCache=" + new Date().time);

function pollFile(e:TimerEvent):void 
{
    // update the value of noCache every time you load the request
    fileRequest.url = fileRequest.url.split("?")[0] + "?noCache=" + new Date().time;
    fileLoader.load(fileRequest);
}