How to make slider to move as video plays?
Initialization :
BorderPane border = new BorderPane();
HBox bar = new HBox(10);
//Button play = new Button("Play");
//Button pause = new Button("Pause");
Media:
Media media = new Media(Paths.get("C://video.mp4").toUri().toString());
MediaPlayer player = new MediaPlayer(media);
MediaView mediaView = new MediaView(player);
Positioning :
bar.getChildren().addAll(play,pause,timeSlider);
bar.setAlignment(Pos.CENTER);
border.setCenter(mediaView);
border.setBottom(bar);
Any suggestions how to make my slider move as video plays?
Edit:
Found answer :
player.currentTimeProperty().addListener((obs, oldTime, newTime) -> {
if (!timeSlider.isValueChanging()) {
timeSlider.setValue(newTime.toSeconds());
}
});
I'm working on a similar problem right now. This should work for you:
// Listen to the slider. When it changes, seek with the player.
// MediaPlayer.seek does nothing when the player is stopped, so the play/pause button's handler
// always sets the start time to the slider's current value, and then resets it to 0 after starting the player.
InvalidationListener sliderChangeListener = o-> {
Duration seekTo = Duration.seconds(timeSlider.getValue());
player.seek(seekTo);
});
timeSlider.valueProperty().addListener(sliderChangeListener);
// Link the player's time to the slider
player.currentTimeProperty().addListener(l-> {
// Temporarily remove the listener on the slider, so it doesn't respond to the change in playback time
// I thought timeSlider.isValueChanging() would be useful for this, but it seems to get stuck at true
// if the user slides the slider instead of just clicking a position on it.
timeSlider.valueProperty().removeListener(sliderChangeListener);
// Keep timeText's text up to date with the slider position.
Duration currentTime = player.getCurrentTime();
int value = (int) currentTime.toSeconds();
timeSlider.setValue(value);
// Re-add the slider listener
timeSlider.valueProperty().addListener(sliderChangeListener);
});
Related
Essentially, what I'm trying to do is something like to a text-based RPG using JavaFX. Right now, to display some text, I've got this:
final IntegerProperty i = new SimpleIntegerProperty(0);
Timeline timeline = new Timeline();
KeyFrame keyFrame = new KeyFrame(
Duration.millis(70),
event -> {
if (i.get() > info.getText().length()) {
timeline.stop();
} else {
text.setText(info.getText().substring(0, i.get()));
i.set(i.get() + 1);
}
});
timeline.getKeyFrames().add(keyFrame);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
timeline.setOnFinished(a -> {
hb_start.getChildren().clear();
hb_start.getChildren().addAll(start_left,start_right);
hb_start.setAlignment(Pos.CENTER);
});
Because the length of the animation depends on the size of the text, the cyclecount is set to indefinite. Unless there's some other way I'm missing to make the animation play once and then stop, I'd like it so that when you press enter (or some other key that I decide on later) for it to call timeline.stop(); but I can't figure out how to add any sort of listener. Trying to implement keyListenerseems to come with all sorts of stuff that I don't need, and it also doesn't work with a TextField, and instead wants a JTextField, which might be fine, except that I don't have a clue how to do anything with Swing.
Currently, the text is being displayed in aTextFlow from the text of Text. I'm assuming the listener would be added to the TextFlow, or even the scene itself, honestly, I'm at a loss for what to do. It sounds simple, but I can't seem to figure it out.
KeyListener is a AWT class, not a JavaFX class. Unless you're embedding a Swing component in your JavaFX application or a JavaFX node in a Swing application, you should use JavaFX's equivalent EventHandler<KeyEvent> instead. Furthermore there is no need to include a TextField (or Swing's JTextField) in your application just for the sake of receiving key events. You could add the listener directly to the Scene:
final KeyCode stopKey = KeyCode.ENTER;
EventHandler<KeyEvent> handler = event -> {
if (event.getCode() == stopKey) {
timeline.stop();
}
};
scene.setOnKeyPressed(handler);
Note that events can be consumed by nodes before they reach the scene, e.g. by a TextField that has the focus. In this case you could make sure you get the event by registering a the listener as a event filter instead:
// scene.setOnKeyPressed(handler);
scene.addEventFilter(KeyEvent.KEY_PRESSED, handler);
I am a beginner in Paper.js . Please could someone tell me how to display tool tip on a paper.js object ? The tool tip should be visible when the mouse moves over the object and should disappear when the mouse moves away from the object .
Thank you.
When drawing your object you want to create a popup for, first add it to a group, then use the onMouseEnter and onMouseLeave handlers on that object:
// Create a group
var group = new Group();
// Create a dot (circle the mouse will hover)
var point = new Point(50, 50);
var dot = new Path.Circle(point, 5);
dot.fillColor = 'blue';
// Add dot to group
group.addChild(dot);
// Create onMouseEnter event for dot
dot.onMouseEnter = function(event) {
// Layout the tooltip above the dot
var tooltipRect = new Rectangle(this.position + new Point(-20, -40), new Size(40, 28));
// Create tooltip from rectangle
var tooltip = new Path.Rectangle(tooltipRect);
tooltip.fillColor = 'white';
tooltip.strokeColor = 'black';
// Name the tooltip so we can retrieve it later
tooltip.name = 'tooltip';
// Add the tooltip to the parent (group)
this.parent.addChild(tooltip);
}
// Create onMouseLeave event for dot
dot.onMouseLeave = function(event) {
// We retrieve the tooltip from its name in the parent node (group) then remove it
this.parent.children['tooltip'].remove();
}
I'm working on Adobe Flash CS5 and on actionscript 3 - My question is how can I make a button that only becomes available to click on once the four other buttons have been clicked and the scenes have been accessed?
Let's assume that your 5 buttons are always available on the timeline and they are at the top document level (if not, we need to make some adjustments). Let's also assume that, from your question, you're not ready for the best practice of a document Class file, so this will be written as timeline script.
//assumes the 4 buttons are on the stage, named button1, button2, etc.
var prerequisiteButtons:Array = [button1, button2, button3, button4];
var prerequisitesClicked:Array /*of Boolean*/ = [];
coyButton.enabled = false;//your fifth button, who plays hard to get
//loop through and listen for clicks on your prerequisite buttons
for each (var button:DisplayObject in buttons) {
button.addEventListener(MouseEvent.CLICK, checkAllClicked);
}
//check to see if all the buttons have been clicked
function checkAllClicked(e:Event):void {
//find the button in the prerequisite buttons array
var buttonIndex:int = prerequisiteButtons.indexOf(e.currentTarget);
//set the matching index to true in the array that keeps track of what has been clicked
prerequisitesClicked[buttonIndex] = true;
//count how many of them have been clicked
var prerequisiteDoneCount:int = 0;
for (var i:int = 0; i< prerequisitesClicked.length; i++) {
if (prerequisitesClicked[i]) prerequisiteDoneCount++;
}
//if all the buttons have been clicked, enable the button (may also want to add a listener here)
if (prerequisiteDoneCount==prerequisiteButtons.length) coyButton.enabled = true;
}
In Flex (Flash Builder 4) I am opening a new window via PopUpManager.addPopUp. I have timer code that runs in my component and I need to stop my timer when that window opens and start the timer again when the window closes.
I figure it's easy enough to stop the timer in the function that opens the window, but how can I start the timer again when the window closes?
Is there a way to tell if there is a pop-up window in front of my component, or if a specific pop-up window is still open via PopUpManager?
Maybe events are a better approach?
Thanks!
Events! is the way to go.
Fire events during launch/close. Add your logic in the event Handlers!
You can use following code to check whether opened popup window is getting closed or not.
if it is closed you can stop the timer.
//set the flag to find your popup window is exist or not.
private var isPopupExist:Boolean = false;
private function closePopUpWindow():void
{
var systemManager:SystemManager = FlexGlobals.topLevelApplication.systemManager;
//Returns a list of all children.
var childList:IChildList = systemManager.rawChildren;
for(var i:int=childList.numChildren-1;i>=0;i--)
{
var childObject:* = childList.getChildAt(i);
//If child object is Uicomponent.
if (childObject is UIComponent)
{
var uiComponent:UIComponent = childObject as UIComponent;
//If uicomponent is popup and class name is equal to **your popup component name** here i am using "ChatComp".
if (uiComponent.isPopUp && uiComponent.className == "ChatComp")
{
isPopupExist = true;
}
}
}
}
in your Timer,
private function checkPopUpExistance():void
{
call closePopUpWindow() function for every 1 sec or any seconds(your wish) to check whether popup is exist or not.
if(isPopupExist)
{
here you stop the timer.
}
}
Now you can start the Timer, when you opened the Popup window.
The popupmanager is a singleton class, so you can easily know how many popups have been created with his ChildList
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/managers/PopUpManagerChildList.html
I have swf file(900x600) and main part of that file is transparense.
So I want by clicking in swf file know either user clicks on transaprent part of image or not...
I can get mouse coordinates by
event.localX
event.localY
So how to know in clicked part swf is transparent or not?
First of all, be sure, that you have some transparent sprite on background of your swf - otherwise you won't receive event.
Second, do not use pure local coordinates, they can contain local coordinates of another inner object, while you need them from root. For example, I've used stage's coordinates
If you receive mouse event, add mouse event listener to the root of that swf and write following:
var bmd:BitmapData = new BitmapData(1, 1, true, 0);
var pt:Point = new Point();
var m:Matrix = new Matrix();
m.translate(-e.stageX, -e.stageY);
bmd.draw(this, m);
var transparent:Boolean = !bmd.hitTest(pt, 0x00, pt);
trace('color: '+bmd.getPixel32(0,0).toString(16));
trace('is tranparent? ' + transparent);
bmd.dispose();
You can add an event listener on the stage for a mouse click, and check if e.target == stage.
Here is the code:
import flash.events.MouseEvent;
stage.addEventListener(MouseEvent.CLICK, hClick);
function hClick(e : MouseEvent) : void
{
trace(e.target == stage); // true if we click on the transparent zone, false if we hit a symbol on the stage.
}
Not the best or cleanest code but it should work.
This is not tested code I just whipped it up.
private function handleMouseDown(event:MouseEvent):void {
var pt:Point = new Point(event.localX, event.localY);
pt = event.target.globalToLocal(pt);
var tmp:int = int( (new uint( event.target.getPixel32(pt.x,pt.y) ).toString(16)).substr(0,2) );
if( tmp != 0 ){
trace( 'is transparent' )
}
}