是否有任何想法可以通过每次点击本课程来播放新的音频文件?

Have any idea's of getting a new audio file to play with every click of this lesson?

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

下面的代码显示了我目前的进度。我有一个 UI 加载程序,其中包含一系列缩略图。每次单击缩略图都会加载一个新的 SWF 文件...好吧,我正在尝试通过 XML 文件获取一系列外部 mp3 文件以执行相同的操作。到目前为止,我已经成功地让一个音频文件在每次点击时播放,并且它在现有文件上播放。

我只是想得到一个新的来播放每个缩略图并停止播放以前的音频文件 if/when 用户单击一个新的缩略图。

如果可能的话请帮忙...帮我看看我的方法中的错误。有点不知所措。

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]);
}

您所缺少的只是用当您在 Sound 上调用 play() 时创建并返回的声道填充 myChannel 变量。

所以,而不是做:

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();

阅读播放功能的documentation,您会发现以下内容:

Generates a new SoundChannel object to play back the sound. This method returns a SoundChannel object, which you access to stop the sound and to monitor volume.


美国东部时间

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

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

//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]));