how to stop / pause geolocation on android - actionscript-3

Im experimenting with flashbuilder 4.6 and am using this simple / bare-bones geolocation code I found online to do some testing as I try to learn more about it and how it might be used...
one thing I am curious to know is how to stop / interrupt the geolocation routine so that it STOPS polling for the location and waits for the user to 'start' the geolocation.
if I use clearInterval(interval); that can stop the loop I guess -- but geo2 continues to exist and use device resources, correct? what would the code look like to use a slideToggle to control it for example?
The geolocation code snippet Im experimenting with...
private function onMapReady(e:Event):void
{
if (Geolocation.isSupported)
{
var geo = new Geolocation();
geo.setRequestedUpdateInterval(100);
geo.addEventListener(GeolocationEvent.UPDATE, geolocationUpdateHandler);
}
else
{
trace("No geolocation support.");
}
}
private function geolocationUpdateHandler(event:GeolocationEvent):void
{
trace("lat:" + event.latitude.toString() + " - ");
trace("long:" + event.longitude.toString() + "° - ");
trace("Accuracy:" + event.horizontalAccuracy.toString() + " m");
}

If you want to stop reacting to polling updates, just remove the listener:
geo.removeEventListener(GeolocationEvent.UPDATE, geolocationUpdateHandler);
If you want to switch off polling completely, just nullify it:
geo = null;
(and make sure you also remove any listeners first)

Related

as3 stopping sound removes eventListener?

I am having an issue with stopping a sound and then starting it back up. After I stop the sound and start it again the eventListener seems to be gone.
Now the "easy" fix seems to be just "add" another one when you start the sound again.
This can not be done easily because the sound channel "Praying" has dynamic listener
added to it with a different function called at the end of each. So I would have to know what listener was added to it and what function should be called when done.
Again, I simply want to "pause" the currently prayed prayer with mouse click and start it up in the same spot with another click. But the issue is that it is removing the eventListener with the instructions for what to do after the sound is done playing.
Any thoughts on a work around? Or maybe this is an easy fix?
/// EXAMPLE 1
Praying = OFE.play();
Praying.addEventListener(Event.SOUND_COMPLETE, prayDecade );
/// EXAMPLE 2
Praying = JES.play();
Praying.addEventListener(Event.SOUND_COMPLETE, doSomethingElse);
public function togglePraying(e:Event = null)
{
if(nowPraying)
{
Praying.stop();
nowPraying = ! Praying;
trace("Praying: " + currentSound);
}
else
{
Praying = currentSound.play();
nowPraying = ! Praying;
trace("Praying: " + Praying);
}
}
This is normal, when you call OFE.play(), you get a SoundChannel reference and if you call it another time, you get a NEW REFERENCE. You need to register the event again, but don't forget to remove the listener.
if(nowPraying)
{
Praying.removeEventListener(Event.SOUND_COMPLETE, doSomethingElse);
Praying.stop();
nowPraying = ! Praying;
trace("Praying: " + currentSound);
}

getMicrophone() in as3 only works when using setLoopBack()

I want to monitor the microphone audio input with flash ( as3 ).
This is just a tiny part of my code, but actually the problem is in there.
var mic:Microphone = Microphone.getMicrophone();
mic.setLoopBack(true);
addEventListener( Event.ENTER_FRAME, loop );
function loop( event:Event ):void {
trace( mic.activityLevel );
}
If i use the code like it is, i can trace the activityLevel and actually can see some values.. ( i think it is the volume ? )
Well, the only problem is, that the audio is also outputed to the speakers, what i DON'T want... ( mic.setLoopBack(true); )
But when i try mic.setLoopBack(false);, flash doesn't ask for microphone premissions anymore and the traced activityLevel stays "-1".....
So what can i do to disable the audio loopback OR to just monitor the audio data from the mic. ?
( when i say "audio data", i mean all the data that is necessary for later BPM detection... i think it's an byte array of the audio, isn't it ? )
As a temp solution to mute the mic you can try :
var st:SoundTransform = new SoundTransform(0);
mic.soundTransform = st;
You should still see the activity level.
Instead of using setLoopBack(), you just need to listen for SampleDataEvent's from the Microphone. Note the section titled "Detecting Microphone Activity" in this Adobe article, and in particular this note that talks about ways you can listen for microphone activity:
Note: A Microphone object only dispatches Activity events when your application is monitoring the microphone. Thus, if you do not call setLoopBack( true ), add a listener for sample data events, or attach the microphone to a NetStream object, then no activity events are dispatched.
var mic:Microhpone = Microphone.getMicrophone;
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
function onSampleData(event:SampleDataEvent):void
{
trace("activity from: " + mic.name + " level: " + mic.activityLevel);
}
This should be a more optimal solution, as the SampleDataEvent's are only dispatched when the microphone detects sound, as opposed to your current approach that works on every frame.

Set rotation flash tween to shortest distance as3?

I am working on an adobe air interactive table project and I'm trying to tween some rotations. For the most part it works fine, but occasionally it spins aaall the way around version just spinning a little to the other direction. Does anyone of a good way to prevent this in the flash tweens?
A snippet of my code:
var rotatePos:Number;
if (event.rotation > 180) { rotatePos = event.rotation - 360; } else { rotatePos = event.rotation; }
var rotateDifference:Number = Math.abs(Math.abs(rotatePos) - Math.abs(Number(rotationCurrent[tempCircleNumber])));
if ( rotateDifference > 4 && rotateDifference < 60) {
rotateTheFiducial();
} else if ( rotateDifference > 100 ) {
trace("too far, ignore : " + rotateDifference);
}
function rotateTheFiducial():void
{
try
{
var cardTweenRotation:Tween = new Tween(MovieClip(fiducialArray[tempCircleNumber]), "rotation", Regular.easeOut, Number(rotationCurrent[tempCircleNumber]), rotatePos, .2, true);
rotationCurrent[tempCircleNumber] = rotatePos;
}
catch (e:Error)
{
trace(fiducialId + " : Rotate Error : " + e);
}
}
Regular.easeOut is supposed to perform the above behavior.
Sometimes, while having multiple tweens with easing, flash even misses out the final point.
So if you do have to use easeout, add a tween complete event wherein you set the final point manually.
Else use None.easeNone
You really should avoid using Flash built-in Tween engine. Its slow, bloated, and is lacking a lot of really useful things.
Try using TweenLite/TweenMax or better, eaze that both have built-in functions for short rotations like you are facing.
They are waaaay faster than using the default Tween engine ! (as you can see here)
Not only you will be able to sort that kind of short-rotation problems, but your app will gets faster with one of them :-)

HTML5 video - measuring playback

Is it possible to measure viewing time for the 'video' tag? I see that it has 'onplaying' and 'onpause' events so I could probably fake a timer.
Just wondering if there is something more that Im missing.
HTML5 media elements fire a 'timeupdate' event. See http://dev.w3.org/html5/spec/media-elements.html#event-mediacontroller-timeupdate. When that event fires, you can check .currentTime for the current time and .duration for the length of the video. It also fires a 'durationchange' event.
Full HTML5 Media DOM Interface Specs: http://dev.w3.org/html5/spec/media-elements.html
videoElement.currentTime
Try it on jsFiddle!
Source: https://developer.mozilla.org/en-US/docs/DOM/HTMLMediaElement
I tried to accomplish the same thing in a project I'm working on.
I did it using a jquery stopwatch plugin.
This is the JS you'll need:
var w = new Stopwatch();
document.querySelector("#video").addEventListener("playing", Start, false);
document.querySelector("#video").addEventListener("pause", Stop, false);
document.querySelector("#video").addEventListener("waiting", Stop, false);
function Start() {
if ($("#video").get(0).paused == true)
Stop();
else
w.start();
}
function Stop() {
w.stop();
}
And this is how you get the elapsed view time (in milliseconds):
var e = w.getElapsed();
time = (((e.hours * 3600) + (e.minutes * 60) + e.seconds) * 1000 + e.milliseconds); // Total view time
Keep in mind - this is not the most accurate way to do this, yet it is the best I've managed to accomplish with my coding skills.
Good luck!

Flash authoring environment strange Test Movie behavior (AS3)

I can't for the life of me figure out why this is happening. Let me describe what I'm experiencing.
I add everything dynamically via actionscript.
In the Flash authoring environment, when I test the movie, sometimes all of the movieclips on stage disappear and all I see is the color of the Stage.
The strange thing is that I can still rollover/rollout (I added trace statements to my rollover/rollout handlers).
I'm also tracing out the 'visible' and the 'alpha' property and visible=true and alpha=1.0 !!!
On thing that I am seeing is sometimes the rollover/rollout methods get called multiple times in quick succession. I.e. the method invocation order is rollover, rollout, rollover or rollout, rollover, rollout.
The actions that I have in my rollover and rollout methods are really simple. All they do is turn on/off other movieclips...imagine a map...when you rollover an icon, a path shows up on the map and when your rolloff, the path goes away.
However, if I adjust the window of the test movie window, everything appears again!
The crazy thing is that when I publish it, this behavior doesn't happen in the browser or as an app!
What's going on? Could it be a memory thing with the authoring environment?
Posting some code here:
private function rollOverUserListener ( e:MouseEvent ) {
trace(">>>>>>>> rollOverUserListener() e.currentTarget.name : " + e.currentTarget.name);
trace("e.currentTarget.alpha: " + e.currentTarget.alpha);
trace("e.currentTarget.visible: " + e.currentTarget.visible);
e.currentTarget.rollOverAction(); //just scales the icon a little
//fade up/down the appropriate path
worldMap.resetPaths(); //turns off all the paths
for (var i=0; i<users.length; i++){
if ( e.currentTarget == users[i] ) { //highlight the right path
worldMap.highlightPath(i);
}
}
}
private function rollOutUserListener ( e:MouseEvent ) {
trace("<<<<<<<< rollOutUserListener() e.currentTarget.name : " + e.currentTarget.name);
e.currentTarget.rollOutAction(); //scales down the icon to normal
worldMap.resetPaths();
}
I don't think it's efficient to try and solve this problem via posting the code you did.
But, my guess is that the difference in behavior that you are seeing is due to the flash player version.
CS5 or whatever version you have of flash, comes with the latest player at that time. But the flash player is constantly being upgraded, so when you are in your browser -- you most likely have the latest flash player. That could account for the differences you are seeing.
However, the code above doesn't help to much without seeing the highlightPaths and resetPaths functions. I see that you have a trace, but right after that -- there's code executed that could potentially easily change the state of anything you traced before rendering the frame.
Stick some traces after that code to see if you get what you expect.
Are you using any libraries that might have features only supported by a newer flash player ?
private function rollOverUserListener ( e:MouseEvent ) {
trace(">>>>>>>> rollOverUserListener() e.currentTarget.name : " + e.currentTarget.name);
trace("e.currentTarget.alpha: " + e.currentTarget.alpha);
trace("e.currentTarget.visible: " + e.currentTarget.visible);
e.currentTarget.rollOverAction(); //just scales the icon a little
//fade up/down the appropriate path
worldMap.resetPaths(); //turns off all the paths
for (var i=0; i<users.length; i++){
if ( e.currentTarget == users[i] ) { //highlight the right path
worldMap.highlightPath(i);
}
}
}
private function rollOutUserListener ( e:MouseEvent ) {
trace("<<<<<<<< rollOutUserListener() e.currentTarget.name : " + e.currentTarget.name);
e.currentTarget.rollOutAction(); //scales down the icon to normal
worldMap.resetPaths();
}