NetStream.seek error Seeking videos - actionscript-3

i try to do seek function in NetStream class using Action Script language , The seek is not work correctly . I read about KeyFrames in NetStream , what is the relationship between KeyFrames and seek ? is there other problem of using seek function by NetStream ?
onClick() Function to Seek;
private function onClick(event:MouseEvent):void
{
if (event.currentTarget is Group)
{
var myGroup:Group = event.currentTarget as Group;
if ( myGroup.mouseX >= 100)
{
mouseClickedXPos = myGroup.mouseX;
ns.inBufferSeek = true;
var seekTime:Number = (mouseClickedXPos-100) * (totalTime/(controlBarControls.width-100));
ns.seek(seekTime);
}
}
}
there is event for netStatus for net stream
ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
private function onNetStatus(event:NetStatusEvent):void
{
if ( event.info == "NetStream.Play.StreamNotFound" )
legend.text = "Video file passed, not available!";
else if(event.info.code == "NetStream.Play.FileStructureInvalid")
legend.text = "Video file passed, FileStructureInvalid";
}
event.info.code is be NetStream.Seek.InvalidTime , the video will stop playing , sometime will seek for end of video , but i trace it , the ns.time() doesn't update to new value (seekTime)

I'm not entirely sure, but I think you got this line wrong:
var seekTime:Number = (mouseClickedXPos-100) * (totalTime/(controlBarControls.width-100));
Try this instead:
var seekTime:Number = ( ( mouseClickedXPos - 100 ) / ( controlBarControls.width - 100 ) ) * totalTime;
Basically, you were comparing two completely different quantities to form a ratio and multiply that by the mouse click position. Instead, divide that mouseX by the total width (giving the position of the click as a percentage of the total width) and multiply that by the time (giving you a percentage of the total time).
I'm not overly familiar with the way NetStream works (I've used it, just not extensively enough to know this off the top of my head), but if you pass a time greater than the totalTime, there has to be an error or it has to handle it by either not completing the command or by setting the value to 0 (which I personally think is what would happen. It's how I would do it, at any rate)

Related

Control timeline with pinchzoom in Actionscript 3.0

I'm using Actionscript 3.0 in Flash. Is there a way to control the timeline with pinching (so instead of zooming out/in you are moving the timeline back/forth)? I'm working on an story app where the player is in control of the story.
You could do something like the following:
import flash.events.TransformGestureEvent;
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stop();
//listen on whatever object you want to be able zoom on
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM , zoomGestureHandler);
function zoomGestureHandler(e:TransformGestureEvent):void{
//get the zoom amount (since the last event fired)
//we average the two dimensions (x/y). 1 would mean no change, .5 would be half the size as before, 2 would twice the size etc.
var scaleAmount:Number = (e.scaleX + e.scaleY) * 0.5;
//set the value (how many frames) to skip ahead/back
//we want the value to be at least 1, so we use Math.max - which returns whichever value is hight
//we need a whole number, so we use Math.round to round the value of scaleAmount
//I'm multiplying scaleAmount by 1.25 to make the output potentially go a bit higher, tweak that until you get a good feel.
var val:int = Math.max(1, Math.round(scaleAmount * 1.25));
//determine if the zoom is actually backwards (smaller than before)
if(scaleAmount < 1){
val *= -1; //times the value by -1 to make it a negative number
}
//now assign val to the actual target frame
val = this.currentFrame + val;
//check if the target frame is out of range (less than 0 or more than the total)
if(val < 1) val = this.totalFrames + val; //if less than one, add (the negative number) to the totalFrames value to loop backwards
if(val > this.totalFrames) val = val - this.totalFrames; //if more than total, loop back to the start by the difference
//OR
if(val < 1) val = 0; //hard stop at the first frame (don't loop)
if(val > this.totalFrames) val = this.totalFrames; //hard stop at the last frame (don't loop)
//now move the playhead to the desired frame
gotoAndStop(val);
}

function 'for - do - end" / moving object ( Corona Sdk )

I'm beginner for coding, I've questions :
First I'd like know why that code is not moving :
local speed = 5
function cube ()
for i = 1,20,2 do
local rect = display.newRect(50,50,50,50)
rect.x = screenleft-300 + (50*i)
rect.y = _y
rect.x = rect.x - speed
if (rect.x < -450 )then
rect.x = 1200
end
end
end
timer.performWithDelay(1, cube, -1)
Secondly : What's the difference between
Runtime:addEventListener( "enterFrame", cube )
and
timer.performWithDelay(1, cube, -1)
Because I get the same result with both of them
And to be done, Why when I use the function "for" to duplicate something like the square i've done upside, this one put the image behind eachother and not like the square beside eachother ( the image i'm trying to duplicate has more than 4 side )
Thanks for all your reply !
thks a lot dude , I know what you mean by here but my question is little bit weird maybe lol and maybe we can't do it
I try to explain again :
for i=1,10,1 do
local Circle = display.newCircle(50, 20, 20)
Circle.x = _x + (50*i)
Circle.y = _y
end
So here I've a Circle line like that 00000 (imagine 0 are circle ^^)
and I want to make that line moving from the left to the right screen, but when i try to make it move with :
Circle.x = Circle.x - speed
Corona don't recognize the " circle.x " so I can't, maybe because is insert into the"FOR"
SO my question is : "How to move this Circle line if that's possible with the "FOR" ?
I hope I've Been clearer
Anyway, Thanks for all
I'll answer your second question first:
Runtime:addEventListener( "enterFrame", cube )
The function addEventListener adds a listener to the object’s list of listeners. When the named event occurs (in this case "enterFrame"), the listener will be invoked and be supplied with a table representing the event. In your code, the listener will call cube() on every frame (normaly, games run at 60 frames per second).
timer.performWithDelay(delay, listener [, iterations])
performWithDelay does what it says it does: Call a specified function after a delay. The timer function returns an object that can be used with other timer.* functions. In your code timer.performWithDelay(1, cube, -1) the function is calling cube() every 1ms and it is going to do so forever. This is not a good thing to do. There's nothing catching the return of the timer function and it is going to be running forever.
Now, to answer your main question. I believe what you are trying to do is create a square an move it in the screen. If that's correct, here's how you should do it:
local square = display.newRect(100,100,50,50)
local speed = 2
-- called every frame
local function moveSquare()
square.x = square.x + speed
end
Runtime:addEventListener("enterFrame", moveSquare)
The reason your code doesn't do what you want it to do is because you misunderstood a some basic CoronaSDK things.
Hope this little code helps you to understand more about how CoronaSDK works. Don't forget to check the documentation of Corona in http://docs.coronalabs.com/
You're creating an object locally in a loop and trying to move it outside of the loop. This doesn't work due to the way lua uses local variables. See http://www.lua.org/pil/4.2.html for more info about this.
Also, you'll need to place the objects into a single display group in order to move them easily. If you're using Box2D physics at all, I recommend reading up on it more at http://docs.coronalabs.com/api/library/physics/index.html.
Your code:
for i=1,10,1 do
local Circle = display.newCircle(50, 20, 20)
Circle.x = _x + (50*i)
Circle.y = _y
end
Should Be Changed to:
local Circle = display.newGroup(); --Forward declaration of Variable. Place this before any calls for it.
local speed = 2;
for i=1,10,1 do
local object = display.newCircle(50,20,20);
object.x = _x + (50*i);
object.y = _y;
Circle:insert(object); --Insert this local object into the display group
end
function moveCircle()
Circle.x = Circle.x + speed;
end
Runtime:addEventListener( "enterFrame", moveCircle);
This will move the Circle line every frame, on the X-axis, by the speed variable's value.
If you're trying to move it with a for-loop, then we'd need to see more of you code in context.

5 Minute Countdown With Buttons

I have to make a 5 minute countdown clock as an assignment. I was given the basis of a as3 and told to add 'Start' - 'Reset' - 'Stop' buttons. So far I have a respectable countdown going, but no way to control it. I'm new to as2 and flash so I hope this isn't one of those "If you know that, you should know your answer" situations. Anything i try to find on the web as far as tuts don't really help me so if anyone could just tell me if I'm on the right path or eve if it's possible to ass buttons to this code to do whats stated above. Thanks in adv :)
var secs = "0" + 0;
var mins = 5;
timerDisplay.text = mins + ":" + secs;
var timerInterval = setInterval(countDown,1000);
//DISPLAYS DYNAMIC TEXT
function countDown()
{
secs--;
if (secs < 0)
{
secs = 59;
mins--;
}
if (secs < 10)
{
var secs2 = "0" + secs;
}
else
{
var secs2 = secs;
}
if (mins == 0 && secs == 0)
{
clearInterval(timerInterval);//STOPS TIME # ZERO
}
timerDisplay.text = mins + ":" + secs2;
}
As this is an assignment, I won't give you any code, just some ideas to get you on your way to writing your own.
You may want to add two (possibly three) buttons to the stage. One button could be the reset button and another could be a button that trades off between being start and stop (depending on whether the timer is currently running).
The only code necessary for a reset button, is something that sets your min variable and your secs variable back to their original values (possibly plus 1 second because of your interval code). If you need help getting started with buttons clicks throwing functions check out the mc.onPress method.
The only code necessary for a stop button, is something that stops your interval counter from continuing to count. I believe you already have something doing that when "clear" your timer at zero.
The only code necessary for a start button, is something that restarts your interval counter. You do something of the sort when you first start your timerInterval.
This won't work if someone decides to click on the start button after you have finished the countdown or if someone decides to click the start button multiple times.
In the first case, the countdown will continue into negative numbers, so you may want to write an if statement that doesn't allow that to happen (inside of the start button function).
And in the second case, the countdown will get faster and faster each time the button is pressed. Creating a boolean that keeps track of whether the program is stopped could possibly help with that problem.
To clarify a statement I made above about the interval code: Your code will forever decrement every 1000 milliseconds. The text box is only ever being refreshed when you decrement, so if you try to reset with exactly 5 minutes and zero seconds, you will see the numbers jump to 4:59 and then keep decrementing. If you reset to 5 minutes and 1 second, the numbers will appear to jump to 5:00 and then decrement from there.
I hope this helps!
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Test extends MovieClip {
private var _timer:Timer = null;
private var _repeatCount:int = 0;
private var _totalMinutes:int = 5;
public function Test() : void {
addEventListener(Event.ADDED_TO_STAGE, _Init);
}
private function _Init(e:Event) : void {
_repeatCount = _totalMinutes * 60;
_timer = new Timer(1000, _repeatCount);
_timer.addEventListener(TimerEvent.TIMER, _OnTimerFired);
_timer.start();
}
private function _OnTimerFired(e:TimerEvent) : void {
var minRem:int = (_repeatCount - _timer.currentCount) / 60 ;
var secRem:int = (_repeatCount - _timer.currentCount) % 60;
trace(minRem + ":" + secRem);
}
}
}

How to display the highest value from a random created list of numbers in AS3

I m building kind of a game in Flash (AS3) where sound input (microphone) trigger a equalizer (the micLevel.height controls the height of a mask (showing the equalizer). The activityLevel of the microphone gives me a number ( 0 - 100 ) wich is displayed in textfield prosent. The contestant shout in the microphone and try to reach 100 ( I use the mc.gain to make it hard to reach 100 ). So far so good! I m pretty new to AS3 so I feel kind of lost.
I need to display the highest number they manage to reach and I want to have a time limit on this. The highest sound level in lets say 5 seconds.
Here is the code so far:
var mic:Microphone = Microphone.getMicrophone();
Security.showSettings("privacy");
mic.setLoopBack(true);
if(mic != null)
{
mic.setUseEchoSuppression(true);
stage.addEventListener(Event.ENTER_FRAME, showLevel);
}
function showLevel(e:Event)
{
micLevel.height = mic.activityLevel * 6;
//mic.gain = 1;
//trace(mic.activityLevel);
prosent.text = "Activity: " + String(mic.activityLevel) + "%";
}
I just need some code that grab the highest number from the text field "prosent" (with a time limit) and display it in a new text field.
Im sorry if I m unclear, but if anyone could help me out I would be very happy!
Br Harald
Just create a variable that will be updated whenever the micLevel.height value is higher than it, e.g.
var highest:Number = 0;
function showLevel(e:Event):void
{
if(micLevel.height > highest)
{
// The mic level was higher than the previous highest level.
highest = micLevel.height;
// Change your other text field to show the value of 'highest'.
// ..
}
prosent.text = "Activity: " + String(mic.activityLevel) + "%";
}
Start a Timer to reset your highestLevel var. This will show you then the highest level every 5 seconds. You could add a timer start-stop button too if you wanted to keep one highest always showing.
Then in your showLevel function, use Math.max() to get the highest number (between current activity and recent highest)
var highestLevel:Number = 0;
var timer:Timer = new Timer(5000); // fires every 5 seconds
function initLevels(e:TimerEvent){
timer.stop(); // you could stop your timer here automatically and then use a button to start again
highestLevel = 0; // when timer fires restart your highestLevels var
}
function showLevel(e:Event) {
highestLevel = Math.max(mic.activityLevel * 6, highestLevel);
prosent.text = "Activity: " + String(highestLevel) + "%";
}
timer.start();
timer.addEventListener(TimerEvent.TIMER, initLevels);
addEventListener(Event.ENTER_FRAME, showLevel);

Check If animation is running in cocos2d-x

I am currently learning cocos2D-x and am doing some sprite animation.
My Objective is that when a button is clicked the object moves to left with some animation.
Now if you click multiple times rapidly the animation takes place immediately and it looks like the bear is hoping instead of walking.
The solution to it looks simple that I should check if animation is already running and if running the new animation should not take place.
The following is a part of my code.
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("AnimBear.plist");
CCSpriteBatchNode* spriteBatchNode = CCSpriteBatchNode::create("AnimBear.png", 8);
this->addChild(spriteBatchNode,10);
CCArray *tempArray = new CCArray();
char buffer[15];
for (int i = 1; i <= 8 ; i++)
{
sprintf(buffer,"bear%i.png", i);
tempArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(buffer));
}
CCAnimation *bearWalkingAnimation = CCAnimation::create(tempArray,0.1f);
startAnimation = CCSprite::createWithSpriteFrameName("bear1.png");
startAnimation->setPosition(ccp (350 , CCDirector::sharedDirector()->getWinSize().height/2 -100));
startAnimation->setScale(0.5f);
startAnimation->setTag(5);
//Animation for bear walking
bearAnimate = CCAnimate::create(bearWalkingAnimation);
Here bearAnimate is a global variable and i wish to know if its currently playing the animation.
How do I do it.?Thank you.
Assume the Sprite that runs the action is
CCSprite* bear;
I think you can use something like
bear->numberOfRunningActions()
numberOfRunningActions( ) returns an unsigned integer, so to check if there are no actions, you would have to check if it returns 0
if ( bear -> numberOfRunningActions( ) == 0 ) {
CCLOG( "No actions running." );
} else {
CCLOG( "Actions running." );
}
The bearAnimate (CCAnimate) has a method to check that.
if (bearAnimate.isDone())
doWhatYouWant();
The method is inherited from CCAction. Good luck.