开发者

Flex: how to change the volume of an EMBEDDED sound?

Searching for how to change the volume of sounds, I always get this irritating snd=new Sound(URLRequest), followed by snd.setVolume(val). Oh, fine, but my sound is not an URLRequest, it's an embed.

I've made a lot of random attempts (1) to no avail. How am 开发者_运维问答I supposed to do that instead?

(1) including casting my class to Sound, creating a Sound with the embed class as argument, creating a SoundTransform and setting it to the channel, etc.


Instantiate your embedded class like this:

[Embed(source="MySound.mp3")]
public var soundClass:Class;


protected function application1_creationCompleteHandler(event:FlexEvent):void
{
    var smallSound:Sound = new soundClass() as Sound;
    var trans:SoundTransform = new SoundTransform(.01);
    smallSound.play(0,0,trans);
}

Update:

In case what you really wanted to know was how to change the volume if the sound was already playing:

[Embed(source="MySound.mp3")]
public var soundClass:Class;
public var smallSound : Sound;
public var vol : Number = 0.01;
public var trans : SoundTransform;

public var chan : SoundChannel = new SoundChannel();

protected function application1_creationCompleteHandler(event:FlexEvent):void {
    smallSound = new soundClass() as Sound;
    trans = new SoundTransform(vol);
    chan = smallSound.play(0,0,trans);
}

protected function volUp_clickHandler(event:MouseEvent):void {
    vol += .1;
    trans = new SoundTransform(vol);

    chan.soundTransform = trans;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜