繁体   English   中英

如何从Flex中的as3类调用main.mxml应用程序中的函数

[英]How call the function that is in my main.mxml app from a as3 class in Flex

我编写了一个声音播放类,以停止较旧的声音并播放新声音。此后,我需要在该类中使用一个额外的方法来在声音播放完成时触发。我成功实现了这一点,但是我需要通知主应用程序( main.mxml)关于声音播放的完成情况。 我该怎么做? 提前致谢。

这是我的声音播放课程。

package com.m2b.data
{
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundMixer;
    import flash.net.URLRequest;

    import mx.controls.Alert;
public class SoundPlayback
{
    private var channel:SoundChannel = new SoundChannel();
    private var sm:SoundMixer = new SoundMixer();
    public  var snds:Sound; 

    public function SoundPlayback()
    {
        // constructor function
    }
    /** call if need to close all previous sound and play new one **/
    public function playSound():void{
        // the StopAll method is used to close/shutdown all sound 
        // in that domin that are describe in that cross doamin
        SoundMixer.stopAll();
        // play the new sound.
        channel = snds.play();
        channel.addEventListener(Event.SOUND_COMPLETE, soundcomplete);
    }
    /** call when the new sound is play without stop old sounds**/
    public function playAllSound():void{
        // play the new sound.
        channel = snds.play();
        }
        private function soundcomplete(e:Event):void{
            Alert.show('<<<< complete >>>>>>');
        }
    }
}

这是我们将声音obj作为参数传递给类,然后调用play sound方法播放声音的函数。

//tahir - play the sound (close all previous sound and play new one)
private var soundPlayer:SoundPlayback = new SoundPlayback();
private function welcomePackage():void{

 soundPlayer.snds = loaderQueue.getSound('CV-welcome'+randomNumber(1,3));
 soundPlayer.playSound();

}

谢谢。

最简单的方法是调度和侦听自定义事件。 您可以在这里阅读更多内容 但实际上,您是在主类上创建一个事件侦听器,并从声音播放器分派一个自定义事件。

希望这可以帮助。

您可以在main.mxml创建一个函数,如下所示:

public function soundDone():void{
    Alert.show('<<<< complete >>>>>>');
}

然后修改您的soundcomplete()函数

private function soundcomplete(e:Event):void{
    parentApplication.soundDone();
}

经过对我的问题进行一些研发之后,我找到了答案

也就是说,您可以从任何mxml组件调用主应用程序,但是对于AS3而言,它是无效的。

我只是公开了频道类,并在main.mxml中编写了监听器函数。

谢谢 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM