以前的歌曲不会停止AS3

时间:2015-03-24 18:13:43

标签: actionscript-3 flash flash-cc

如果我正在更改组合框中的歌曲,此代码将不会停止上一首歌曲。新歌开始播放,但如上所述,旧歌不会停止。请帮我这个学校项目。

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import fl.data.DataProvider;
import flash.events.MouseEvent;

var loader:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest("xml/xmlSanger.xml");
var xml:XML;
var dp:DataProvider = new DataProvider(); 

dropdown2.addEventListener(Event.CHANGE, visSang); 

loader.addEventListener(Event.COMPLETE, lastetXML); 
loader.load(url);

function lastetXML(evt:Event) {
    xml = new XML(loader.data);
    fyllComboBoks(); 
}

function fyllComboBoks(){
    var antallSanger = xml.sang.length();
    for(var teller:int = 0; teller < antallSanger; teller++)
    {
        dp.addItem({label:xml.sang[teller].navn[0]});
    }

dropdown2.dataProvider = dp; 
dropdown2.selectedIndex = 0;

}

function visSang (evt:Event){
    soundClip.stop();
    var soundClip:Sound = new Sound();
    var id:uint = dropdown2.selectedIndex;
    trace (id);
    songname.text = xml.sang[id].navn[0];
    artistname.text = xml.sang[id].artistname[0];
    var filbane = xml.sang[id].fil[0];
    soundClip.load(new URLRequest(filbane));    
    soundClip.play();
}

1 个答案:

答案 0 :(得分:0)

您需要在visSang之外声明soundClip,以便在再次调用该函数时可以访问它。

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import fl.data.DataProvider;
import flash.events.MouseEvent;

var loader:URLLoader = new URLLoader();
var url:URLRequest = new URLRequest("xml/xmlSanger.xml");
var xml:XML;
var dp:DataProvider = new DataProvider(); 
var soundClip:Sound;

dropdown2.addEventListener(Event.CHANGE, visSang); 

loader.addEventListener(Event.COMPLETE, lastetXML); 
loader.load(url);

function lastetXML(evt:Event) {
    xml = new XML(loader.data);
    fyllComboBoks(); 
}

function fyllComboBoks(){
    var antallSanger = xml.sang.length();
    for(var teller:int = 0; teller < antallSanger; teller++)
    {
        dp.addItem({label:xml.sang[teller].navn[0]});
    }

dropdown2.dataProvider = dp; 
dropdown2.selectedIndex = 0;

}

function visSang (evt:Event){
    soundClip.stop();
    soundClip = new Sound();
    var id:uint = dropdown2.selectedIndex;
    trace (id);
    songname.text = xml.sang[id].navn[0];
    artistname.text = xml.sang[id].artistname[0];
    var filbane = xml.sang[id].fil[0];
    soundClip.load(new URLRequest(filbane));    
    soundClip.play();
}