AS3 - 我正在尝试通过XML播放列表播放声音文件,但它似乎不起作用

时间:2014-06-15 08:58:30

标签: xml actionscript-3 audio

我正在尝试通过XML播放列表播放声音文件,但它似乎无法正常工作 我得到这个:错误#2044:未处理的IOErrorEvent:。 text =错误#2032:流错误。知道为什么吗?帮助非常感谢。

var soundURL:URLRequest;
var mySound:Sound = new Sound (); 
var mySoundChannel:SoundChannel = new SoundChannel();
var playing:Boolean = true;
var resumeTime:Number = 0;
var myCurrentSong:Number = 0;
var currentSong:int = 0;
var loadedXML:XML

var myXMLLoader:URLLoader = new URLLoader();
var myURL:URLRequest = new URLRequest("playList.xml");
myXMLLoader.load(myURL);

myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
loadedXML = XML(e.target.data);
soundURL = new URLRequest(loadedXML.songFile[currentSong].@songURL); 
mySound = new Sound(soundURL);
}

btnPlayPause.buttonMode = true;
btnStop.buttonMode = true;
btnNext.buttonMode = true;

btnPlayPause.addEventListener(MouseEvent.CLICK, playSound);
btnStop.addEventListener(MouseEvent.CLICK, stopSound);

function playSound(m:MouseEvent){
if (playing==true)
{
btnPlayPause.gotoAndStop("lbPause");
mySound.load(myURL);
mySoundChannel = mySound.play(resumeTime);
playing = false;
}
else
{
btnPlayPause.gotoAndStop("lbPlay");
resumeTime = mySoundChannel.position;
mySoundChannel.stop();
playing = true;
  }
}

function stopSound(f:MouseEvent):void
{
mySoundChannel.stop();
}

1 个答案:

答案 0 :(得分:0)

你很接近但是在设置跟踪网址时出现了一些小问题。

如果您的XML文件看起来像这样

< ?xml version="1.0" encoding="UTF-8"?>

<MP3s>

    <songFile>


        <songURL> Apakalypse_Exist.mp3 </songURL>   


    </songFile>

</MP3s>

然后代码应该工作。查看function processXML

的最后一行
var soundURL:URLRequest;
var mySound:Sound = new Sound (); 
var mySoundChannel:SoundChannel = new SoundChannel();
var playing:Boolean = true;
var resumeTime:Number = 0;
var myCurrentSong:Number = 0;
var currentSong:int = 0;
var loadedXML:XML;

var myXMLLoader:URLLoader = new URLLoader();
var myURL:URLRequest = new URLRequest("playList.xml");

////Setup... 
btnPlayPause.buttonMode = true;
btnStop.buttonMode = true;
btnNext.buttonMode = true;

btnPlayPause.addEventListener(MouseEvent.CLICK, playSound);
btnStop.addEventListener(MouseEvent.CLICK, stopSound);

myXMLLoader.load (myURL);
myXMLLoader.addEventListener(Event.COMPLETE, processXML);


//// Functions...
function processXML (e:Event):void 
{
    loadedXML = XML(e.target.data);
    //trace("loaded XML" + loadedXML); //for test only

    //soundURL = new URLRequest(loadedXML.songFile[currentSong].@songURL); 
    //mySound = new Sound(soundURL);

    mySound = new Sound(new URLRequest(loadedXML.songFile[currentSong].songURL));
}

function playSound (m:MouseEvent)
{
    if (playing==true)
    {
        btnPlayPause.gotoAndStop("lbPause");
        //mySound.load(myURL);
        mySoundChannel = mySound.play(resumeTime);
        playing = false;
    }
    else
    {
        btnPlayPause.gotoAndStop("lbPlay");
        resumeTime = mySoundChannel.position;
        mySoundChannel.stop();
        playing = true;
    }
}

function stopSound (f:MouseEvent):void
{
    mySoundChannel.stop();
}