简体   繁体   English

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

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

i write a sound playback class that stop the older sound and play the new sound.After that i need an extra method in this class that trigger when the sound play is complete.I successfully achieve this, but i need to inform the main app (main.mxml) about the completion of that sound playing. 我编写了一个声音播放类,以停止较旧的声音并播放新声音。此后,我需要在该类中使用一个额外的方法来在声音播放完成时触发。我成功实现了这一点,但是我需要通知主应用程序( main.mxml)关于声音播放的完成情况。 How i do that ? 我该怎么做? Thanks in advance. 提前致谢。

here is my sound playback class. 这是我的声音播放课程。

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

and here us the function that pass the sound obj as param to class and then call play sound method for playing sound. 这是我们将声音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();

}

Thanks. 谢谢。

The easiest way to do this, is with dispatching and listening to custom-events. 最简单的方法是调度和侦听自定义事件。 You can read more here . 您可以在这里阅读更多内容 But essentially, you're creating an event listener on your main class, and dispatching a custom event from your sound player. 但实际上,您是在主类上创建一个事件侦听器,并从声音播放器分派一个自定义事件。

Hope this helps. 希望这可以帮助。

You can create a function in your main.mxml as: 您可以在main.mxml创建一个函数,如下所示:

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

and then modify your soundcomplete() function 然后修改您的soundcomplete()函数

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

after some R & D on my problem i found the answer 经过对我的问题进行一些研发之后,我找到了答案

That is you call the main application from any mxml component but in case of AS3 it is not valid. 也就是说,您可以从任何mxml组件调用主应用程序,但是对于AS3而言,它是无效的。

i just made the channel class public and write the listener function in my main.mxml. 我只是公开了频道类,并在main.mxml中编写了监听器函数。

Thanks . 谢谢 。

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

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