Actionscript 3 中可回收声音对象?

发布于 2024-08-13 14:01:02 字数 134 浏览 7 评论 0原文

在 Actionscript3 中使用 ONE Sound() 对象,如何播放一个 MP3,然后当用户选择不同的 MP3 时,使用 SAME Sound() 对象播放第二声音?

编辑:请参阅我的答案以了解我是如何做到的。

Using ONE Sound() object in Actionscript3, how can I play a one MP3 and then, when the user chooses a different one, play a second sound, using the SAME Sound() object?

EDIT: See my answer for how I did it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2024-08-20 14:01:02

您不能使用相同的 Sound 对象来播放 多个文件

一旦在 Sound 对象上调用 load(),您以后就无法将不同的声音文件加载到该 Sound 对象中。要加载不同的声音文件,请创建一个新的 Sound 对象。

You cannot use same Sound object to play multiple files.

Once load() is called on a Sound object, you can't later load a different sound file into that Sound object. To load a different sound file, create a new Sound object.

恋竹姑娘 2024-08-20 14:01:02

好的,我实际上是使用以下代码完成的。我的错误位于 FLA 文件中的其他位置,但这是有效的。我创建了一个未初始化的全局变量,并在函数内部本地创建了 Sound() 对象。虽然我在技术上使用多个声音对象,但我的引用都指向一个对象。此外,我可以相互调用这些方法,以便更轻松地编码。这对我有用:

    /* -------------

Sound player
functions

 ------------ */

var snd:Sound;                      //the sound object
var sndC:SoundChannel;              //the soudchannel used as "controller"
var sndT:SoundTransform;            //soundTransform used for volume 
var vol:Number = 1;                 //the volume of the song
var pan:Number = 0;                 //panning of the sound
var pos:Number = 0;                 //position of the song 
var currentSound:String;                //currently playing song?


function playSound(s:String){                                   //this function resets the sound and plays it
    stopSound(sndC);                                            //stop the sound from playing
    snd = new Sound();                                          //reset the sound
    snd.load(new URLRequest(s));                                //load the desired sound    
    sndC = new SoundChannel();                                  //(re-)apply the sound channel
    applyVolume(vol,pan,sndT,sndC);                             //apply the volume
    sndC = snd.play(pos);                                       //play it
    sndC.addEventListener(Event.SOUND_COMPLETE, startSound);    //remind it to restart playing when it's done
}                                                               //end function

function applyVolume(n:Number, p:Number, st:SoundTransform, sc:SoundChannel){   //takes an argument for the volume, pan, soundTYransform and soundChannel
    sndT = new SoundTransform(n,p);                                             //applies the soundTransfrom settings
    sndC.soundTransform = sndT;                                                 //and attaches it to the soundChannel
}                                                                               //end function

function stopSound(sndC:SoundChannel){          //this function stops a sound from playing
    if(sndC != null){                           //if the sound was used before (ie: playing)
        if(currentLabel == "video-frame"){      //if we are in the video frame
          pos = sndC.position;                  //store the position of the song to play from at a later time
        }else{                                  //otherwise
          pos = 0;                              //set the position at 0
        }                                       //end if
        sndC.stop();                            //stop it
    }                                           //end if
}                                               //end function

function startSound(snd:Sound){                 //restarts a  sound when it's playing
    if(snd != null){                            //if the sound exists   
        sndC = snd.play(pos);                   //play it
    }                                           //end if
}                                               //end function

Ok, I actually did it using the following code. My bug was somewhere else in the FLA file, but this works. I made an uninitialized global variable and created the Sound() object LOCALLY inside of a function. While I'm technically using multiple sound objects, my references are all pointing to ONE object. Additionally, I can call these methods over one another for easier coding. This works for me:

    /* -------------

Sound player
functions

 ------------ */

var snd:Sound;                      //the sound object
var sndC:SoundChannel;              //the soudchannel used as "controller"
var sndT:SoundTransform;            //soundTransform used for volume 
var vol:Number = 1;                 //the volume of the song
var pan:Number = 0;                 //panning of the sound
var pos:Number = 0;                 //position of the song 
var currentSound:String;                //currently playing song?


function playSound(s:String){                                   //this function resets the sound and plays it
    stopSound(sndC);                                            //stop the sound from playing
    snd = new Sound();                                          //reset the sound
    snd.load(new URLRequest(s));                                //load the desired sound    
    sndC = new SoundChannel();                                  //(re-)apply the sound channel
    applyVolume(vol,pan,sndT,sndC);                             //apply the volume
    sndC = snd.play(pos);                                       //play it
    sndC.addEventListener(Event.SOUND_COMPLETE, startSound);    //remind it to restart playing when it's done
}                                                               //end function

function applyVolume(n:Number, p:Number, st:SoundTransform, sc:SoundChannel){   //takes an argument for the volume, pan, soundTYransform and soundChannel
    sndT = new SoundTransform(n,p);                                             //applies the soundTransfrom settings
    sndC.soundTransform = sndT;                                                 //and attaches it to the soundChannel
}                                                                               //end function

function stopSound(sndC:SoundChannel){          //this function stops a sound from playing
    if(sndC != null){                           //if the sound was used before (ie: playing)
        if(currentLabel == "video-frame"){      //if we are in the video frame
          pos = sndC.position;                  //store the position of the song to play from at a later time
        }else{                                  //otherwise
          pos = 0;                              //set the position at 0
        }                                       //end if
        sndC.stop();                            //stop it
    }                                           //end if
}                                               //end function

function startSound(snd:Sound){                 //restarts a  sound when it's playing
    if(snd != null){                            //if the sound exists   
        sndC = snd.play(pos);                   //play it
    }                                           //end if
}                                               //end function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文