AgnosticDev
AgnosticDev

Reputation: 1853

AS3 Android Air App, Music does not turn off when user leaves app

Hello I have a AS3 Android Application that is playing background music, but when the user leaves the application, the music still is playing in the background. Is there a method to call that senses the user leaving the application so that I can turn off my music?
Thank you for your time, Scientific

Upvotes: 3

Views: 3940

Answers (2)

AgnosticDev
AgnosticDev

Reputation: 1853

I found this on an Adobe Air forum: http://forums.adobe.com/message/4075374#4075374 The following code will deactivate the application if it loses focus and reactivate the application if it regains focus.

     NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE , handleDeactivate, false, 0, true);

     function handleDeactivate(event:Event):void {
         //the app is now losing focus
         musicChannel.stop();
     }

     NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate, false, 0, true);

     function handleActivate(event:Event):void {
         musicChannel= mySong.play();
     }

Hope this helps everyone. Let me know if it works for you.

Upvotes: 7

francis
francis

Reputation: 6359

In your App.mxml you can add handlers for app open and close

<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                        xmlns:s="library://ns.adobe.com/flex/spark"
                        activate="open(event)" deactivate="close(event)">

    <fx:Script>
    <![CDATA[
private function open(e:Event):void{
trace("open");
}
private function close(e:Event):void{
trace("close");
}
    ]]>

</fx:Script>

Upvotes: 1

Related Questions