Thursday, 15 March 2012

user interface - JavaFX - Set Slider value after dragging mouse button -



user interface - JavaFX - Set Slider value after dragging mouse button -

i'm writing music player , don't know how code slider dragging handler set value after user frees mouse button. when write simple mousedragged method dragging brings non estetic "rewinding" sound because mediaplayer changes value every time slider moves. while playing slider automatic changes value mediaplayer listener synchronize track duration. got far.

changelistener<duration> timelistener = new changelistener<duration>() { @override public void changed( observablevalue<? extends duration> observablevalue, duration duration, duration current) { durslider .setvalue(current .toseconds()); } }; durslider.setonmousedragged(new eventhandler<mouseevent>() { @override public void handle(mouseevent mouseevent) { mediaplayer.seek(duration.seconds(durslider.getvalue())); } });

the valuechanging property of slider indicates if slider in process of beingness changed. observable property, can attach listener straight it, , respond when value stops changing:

durslider.valuechangingproperty().addlistener(new changelistener<boolean>() { @override public void changed(observablevalue<? extends boolean> obs, boolean waschanging, boolean isnowchanging) { if (! isnowchanging) { mediaplayer.seek(duration.seconds(durslider.getvalue())); } } });

this won't alter position of player if user clicks on "track" on slider, or uses keyboard move it. that, can register listener value property. need careful here, because value going alter via time listener. in theory, time listener should set value of slider, , should cause effort set current time of player exact value has (which result in no-op). however, rounding errors result in lot of little adjustments, causing "static" observing. prepare this, move media player if alter more little minimum amount:

private static double min_change = 0.5 ; //seconds // ... durslider.valueproperty().addlistener(new changelistener<number>() { @override public void changed(observablevalue<? extends number> obs, number oldvalue, number newvalue) { if (! durslider.isvaluechanging()) { double currenttime = mediaplayer.getcurrenttime().toseconds(); double slidertime = newvalue.doublevalue(); if (math.abs(currenttime - slidertime) > 0.5) { mediaplayer.seek(newvalue.doublevalue()); } } } });

finally, don't want time listener move slider if user trying drag it:

changelistener<duration> timelistener = new changelistener<duration>() { @override public void changed( observablevalue<? extends duration> observablevalue, duration duration, duration current) { if (! durslider.isvaluechanging()) { durslider.setvalue(current.toseconds()); } } };

here's finish illustration (using lambdas brevity):

import javafx.application.application; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.slider; import javafx.scene.layout.borderpane; import javafx.scene.layout.hbox; import javafx.scene.media.media; import javafx.scene.media.mediaplayer; import javafx.scene.media.mediaview; import javafx.stage.stage; import javafx.util.duration; public class videoplayertest extends application { private static final string media_url = "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv"; private static final double min_change = 0.5 ; @override public void start(stage primarystage) { mediaplayer player = new mediaplayer(new media(media_url)); mediaview mediaview = new mediaview(player); slider slider = new slider(); player.totaldurationproperty().addlistener((obs, oldduration, newduration) -> slider.setmax(newduration.toseconds())); borderpane root = new borderpane(mediaview, null, null, slider, null); slider.valuechangingproperty().addlistener((obs, waschanging, ischanging) -> { if (! ischanging) { player.seek(duration.seconds(slider.getvalue())); } }); slider.valueproperty().addlistener((obs, oldvalue, newvalue) -> { if (! slider.isvaluechanging()) { double currenttime = player.getcurrenttime().toseconds(); if (math.abs(currenttime - newvalue.doublevalue()) > min_change) { player.seek(duration.seconds(newvalue.doublevalue())); } } }); player.currenttimeproperty().addlistener((obs, oldtime, newtime) -> { if (! slider.isvaluechanging()) { slider.setvalue(newtime.toseconds()); } }); scene scene = new scene(root, 540, 280); primarystage.setscene(scene); primarystage.show(); player.play(); } public static void main(string[] args) { launch(args); } }

user-interface javafx slider

No comments:

Post a Comment