2

我对 AS 有点陌生,但仍在努力学习和完成看似艰难的事情。我会胜利的!

下面的代码显示了我到目前为止的进展。我有一个带有一系列缩略图的 UI 加载器。每次单击缩略图都会加载一个新的 SWF 文件...好吧,我正在尝试通过 XML 文件获取一系列外部 mp3 文件来执行相同的操作。到目前为止,我已经设法在每次点击时只播放一个音频文件,并在现有文件上播放。

我只想让每个缩略图播放一个新的,并在用户单击新缩略图时停止播放以前的音频文件。

如果可能,请提供帮助...帮助我查看我的方式的错误。有点不知所措。

var imagesXML:XML;
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.addEventlistener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("lessons/images/Images5.xml"));


/////////////////////////////////////////////////////////////
////////////    thumbnail loader    /////////////////////////

var mySound:Sound;
var myChannel:SoundChannel;


function xmlLoaded(evt:Event):void
{
imagesXML = new XML(xmlloader.data);
var thumbLoader:UILoader;
for(var i:uint = 0; i < imagesXML.image.length(); i++)
    {
        thumbLoader UILoader(getChildByName("thumb" + 1));
        thumbLoader.load(new URLRequest("lessons/images/thumbs/" + imagesXML.image[i].@file));
        thumbLoader.buttonmode = true;
        thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
        var fullPath:String = "lessons/images/file-1.swf";
        mainLoader.load(new URLRequest(fullpath));
    }
}



/////////////////////////////////////////////////////////////
////////////////   load Images  /////////////////////////////

function thumbClicked(evt:MouseEvent):void
{
    var thumbName:String = evt.currentTarget.name;
    var thumbIndex:uint = uint(thumbName.substr(5));
    var fullPath:String = "lessons/images/" + imagesXML.image[thumbIndex].@file;
    mainLoader.load(new URLRequest(fullPath));

//stop any sound playing
    if (myChannel != null)
    {
        myChannel.stop();
    }

// load mp3 file
    mySound = new Sound(); myChannel = new SoundChannel();
    mySound.load(new URLRequest ("lessons/audio/narration/ch1p3.mp3"));
    mySound.play();
}

编辑

我还需要根据单击的缩略图制作动态播放的声音。

这是我更新的代码:

var imagesXML:XML;
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.addEventlistener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("lessons/images/Images5.xml"));

var thumbSounds:Dictionary = new Dictionary();


/////////////////////////////////////////////////////////////
////////////    thumbnail loader    /////////////////////////

var mySound:Sound;
var myChannel:SoundChannel;


function xmlLoaded(evt:Event):void
{
imagesXML = new XML(xmlloader.data);
var thumbLoader:UILoader;
for(var i:uint = 0; i < imagesXML.image.length(); i++)
    {
        thumbLoader UILoader(getChildByName("thumb" + 1));
        thumbLoader.load(new URLRequest("lessons/images/thumbs/" + imagesXML.image[i].@file));
        thumbLoader.buttonmode = true;
        thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
        var fullPath:String = "lessons/images/file-1.swf";
        mainLoader.load(new URLRequest(fullpath));
        thumbSounds[thumbLoader] = "lessons/audio/narration/sound/ch1" + i + ".mp3";

 //load mp3 file
       mySound = new Sound(); 
       myChannel = new SoundChannel();
       mySound.load(new URLRequest ("lessons/audio/narration/ch1p3.mp3"));
       myChannel = mySound.play();
    }
}



/////////////////////////////////////////////////////////////
////////////////   load Images  /////////////////////////////

function thumbClicked(evt:MouseEvent):void
{
    var thumbName:String = evt.currentTarget.name;
    var thumbIndex:uint = uint(thumbName.substr(5));
    var fullPath:String = "lessons/images/" + imagesXML.image[thumbIndex].@file;
    mainLoader.load(new URLRequest(fullPath));
//stop any sound playing
    if (myChannel != null)
       {
          trace("HOLD sound");
          SoundMixer.stopAll();
       }
    mySound = new Sound();
    myChannel = new SoundChannel();
    mySound.load(new URLRequest (thumbSounds[evt.currentTarget]));
    myChannel = mySound.play();

    trace("now playing" + thumbSounds[evt.currentTarget]);
}
4

1 回答 1

4

您所缺少的只是使用调用SoundmyChannel时创建并返回的声道填充 var 。play()

所以,而不是这样做:

myChannel = new SoundChannel();
//this creates an empty sound channel that has no relevance to anything you're doing
//so later when you do myChannel.stop(), you're just stopping this empty channel

做这个:

myChannel = mySound.play();
//play returns the relevant sound channel to what you've asked to play

所以它应该是这样的:

mySound = new Sound();
mySound.load(new URLRequest ("lessons/audio/narration/ch1p3.mp3"));
myChannel = mySound.play();

阅读 play 函数的文档,您会发现以下内容:

生成一个新的 SoundChannel 对象来播放声音。此方法返回一个 SoundChannel 对象,您可以访问该对象来停止声音并监控音量。


美东时间

为了解决您的评论,您需要以某种方式将音频文件与每个 UILoader 项目相关联。您可以通过扩展 UILoader 类并添加属性来做到这一点,或者您可以使用字典,或者您可以解析标识符的拇指路径。

我将向您展示字典方法,因为它可能是最简单的:

//at the top of your code with your other vars
var thumbSounds:Dictionary = new Dictionary();

然后,当您在该循环中创建 UILoader 项目时,请执行以下操作:

thumbSounds[thumbLoader] = "lessons/audio/narration/ch1p" + i + ".mp3";
//I'm just guessing on your naming convention above, eg ch1p1.mp3, ch1p2.mp3 etc

现在,在播放声音的单击处理程序中,执行以下操作:

//in an event handler, the event's currentTarget is a reference to item that you added the listener to, in this case the thumb that was clicked.
mySound.load(new URLRequest (thumbSounds[evt.currentTarget]));
于 2015-11-25T01:24:35.220 回答