java - Play/Pause Multiple MediaViews With One Controller JavaFX FXML -
at moment have single scene multiple mediaviews, each own play , pause buttons, in fxml. wondering if there way play/pause ever mediaview had button clicked without making play/pause controller each mediaview. below example of tedious way not wish with.
the fxml
<stackpane alignment="center"> <mediaview fx:id="mediaview" styleclass="mediaview"> <mediaplayer> <mediaplayer fx:id="mediaplayer" autoplay="false"> <media> <media source="@../vid/vid1.mp4"/> </media> </mediaplayer> </mediaplayer> </mediaview> <button fx:id="btn_play1" onaction="#handleplay1" alignment="center" styleclass="btn_play"/> <button fx:id="btn_pause1" onaction="#handlepause1" alignment="center" styleclass="btn_pause" visible="false"/> </stackpane> <stackpane alignment="center"> <mediaview fx:id="mediaview2" styleclass="mediaview"> <mediaplayer> <mediaplayer fx:id="mediaplayer2" autoplay="false"> <media> <media source="@../vid/vid2.mp4"/> </media> </mediaplayer> </mediaplayer> </mediaview> <button fx:id="btn_play2" onaction="#handleplay2" alignment="center" styleclass="btn_play"/> <button fx:id="btn_pause2" onaction="#handlepause2" alignment="center" styleclass="btn_pause" visible="false"/>
the controller
@fxml private void handlepause1(actionevent event) throws ioexception { mediaplayer.pause(); btn_pause1.setvisible(false); btn_play1.setvisible(true); } @fxml private void handleplay1(actionevent event) throws ioexception { mediaplayer.play(); btn_play1.setvisible(false); btn_pause1.setvisible(true); mediaactive = true; } @fxml private void handlepause2(actionevent event) throws ioexception { mediaplayer1.pause(); btn_pause2.setvisible(false); btn_play2.setvisible(true); } @fxml private void handleplay2(actionevent event) throws ioexception { mediaplayer1.play(); btn_play2.setvisible(false); btn_pause2.setvisible(true); mediaactive = true; }
consider creating custom component media view buttons.
the basic idea looks like
package application; import java.io.ioexception; import javafx.beans.namedarg; import javafx.fxml.fxml; import javafx.fxml.fxmlloader; import javafx.beans.property.readonlyobjectproperty ; import javafx.scene.control.button; import javafx.scene.layout.stackpane; import javafx.scene.media.mediaplayer; public class controlledmediaview extends stackpane { @fxml private mediaplayer mediaplayer ; @fxml private button playbutton ; @fxml private button pausebutton ; public controlledmediaview(@namedarg("mediaurl") string mediaurl) throws ioexception { fxmlloader loader = new fxmlloader(getclass().getresource("controlledmediaview.fxml")); loader.setroot(this); loader.setcontroller(this); loader.getnamespace().put("mediaurl", mediaurl); loader.load(); } public void initialize() { playbutton.visibleproperty().bind(mediaplayer.statusproperty().isnotequalto(mediaplayer.status.playing)); pausebutton.visibleproperty().bind(mediaplayer.statusproperty().isequalto(mediaplayer.status.playing)); } @fxml private void pause() { mediaplayer.pause(); } @fxml private void play() { mediaplayer.play(); } public readonlyobjectproperty<mediaplayer.status> statusproperty() { return mediaplayer.statusproperty(); } public final mediaplayer.status getstatus() { return statusproperty().get(); } }
with controlledmediaview.fxml (in same package):
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.layout.stackpane?> <?import javafx.scene.media.media?> <?import javafx.scene.media.mediaplayer?> <?import javafx.scene.media.mediaview?> <fx:root xmlns:fx="http://javafx.com/fxml/1" type="stackpane"> <mediaview fx:id="mediaview" styleclass="mediaview"> <mediaplayer> <mediaplayer fx:id="mediaplayer" autoplay="false"> <media> <media source="${mediaurl}" /> </media> </mediaplayer> </mediaplayer> </mediaview> <button fx:id="playbutton" onaction="#play" alignment="center" styleclass="btn_play" /> <button fx:idf="pausebutton" onaction="#pause" alignment="center" styleclass="btn_pause" /> </fx:root>
then main fxml can do
<?xml version="1.0" encoding="utf-8"?> <?import application.controlledmediaview?> <!-- ... --> <controlledmediaview mediaurl="@../vid/vid1.mp4"/> <controlledmediaview mediaurl="@../vid/vid2.mp4"/> <!-- ... -->
you may need work little make sure urls being communicated correctly; if want use scenebuilder need package controlledmediaview
, fxml jar file , import scenebuilder.
Comments
Post a Comment