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 开发者_Python百科inform the main app (main.mxml) about the completion of that sound playing. 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.
//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:
public function soundDone():void{
Alert.show('<<<< complete >>>>>>');
}
and then modify your soundcomplete()
function
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.
i just made the channel class public and write the listener function in my main.mxml.
Thanks .
精彩评论