Play/Pause Multiple MediaViews With One Controller JavaFX FXML

At the moment I have a single scene with multiple mediaviews, each with their own play and pause buttons, in fxml. I was wondering if there is a way to play/pause which ever mediaview had its button clicked without making a play/pause controller for each mediaview. Below is an example of the tedious way that I do not wish do it 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; }

-------------Problems Reply------------

Consider creating a custom component for your media view with 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 the 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 your 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 to work a little to make sure the URLs are being communicated correctly; also if you want to use SceneBuilder you need to package ControlledMediaView and its FXML into a jar file and import it to SceneBuilder.

Category:java Views:3 Time:2018-05-14

Related post

  • Managing multiple apps with one Google Analytics account? 2010-04-23

    I've just setup a Google Analytics for Mobile Apps account and I've implemented the SDK in my iPhone app with no trouble at all. However, I haven't figured out how to manage multiple apps with one account. It seems fairly easy to setup multiple subdo

  • Can a browser maintain multiple sessions with one server? 2010-06-11

    Is there a way to maintain multiple sessions with one server within the browser? Here is what I am trying to accomplish: User1 has exclusive access to ContentA and User2 has exclusive access to ContentB. I want to be able to allow User3 to login mult

  • How can I update multiple rows with one query (perl-mysql)? 2010-06-29

    I need to update multiple rows with one query. For the insert we usually do something like INSERT INTO `table` (c1,c2,c3) VALUES (1,2,3), (4,5,6), .. Yet how can we do something similar for the update where each row has different values than the othe

  • Is it possible to set multiple variables with one ternary operator? 2010-07-15

    I was looking to see if it's possible to set multiple variables with one ternary operator. I google'd a bit, but didn't come up with anything. I started testing a few ideas, and found something close -- but also getting some strange behavior; any ide

  • Multiple notifications with one status bar icon in android 2010-09-07

    i am using custom notification...how can i set no of notification is showing?and list out those notification? this is my code... public void onReceive(Context context, Intent intent) { Toast.makeText(context, "coming", Toast.LENGTH_LONG).show(); Bund

  • Serving multiple site with one drupal (not using multi site) 2010-10-19

    I am looking for expert advice on how to best serve multiple sites with one Drupal instance (using Pressflow 6.x). Let's consider the company needing this is called "ABC Group of Companies" and it has 3 sister concerns. So, altogether there will be f

  • how to delete multiple records with one query 2011-01-09

    i would like to delete multiple records with one query using mysql and php instead of using any loops. $query = "DELETE FROM countries c WHERE c.id = 8"; what is the syntax for that? --------------Solutions------------- $query = "DELETE FROM countrie

  • socket.io - listen on multiple namespaces with one packet? 2011-07-17

    We are migrating from APE to socket.io. So far, so good, but... is there way to join multiple namespaces with one packet? I've tried something like that: websocket received data packet 1::/public,/foo,/bar,/baz but that doesn't work. What is best way

  • psycopg2: insert multiple rows with one query 2011-11-15

    I need to insert multiple rows with one query (number of rows is not constant), so I need to execute query like this one: INSERT INTO t (a, b) VALUES (1, 2), (3, 4), (5, 6); The only way I know is args = [(1,2), (3,4), (5,6)] args_str = ','.join(curs

  • Netty - cannot set multiple cookies with one CookieEncoder 2011-11-29

    I have the following simple test code at my server http handler: String cookieString = request.getHeader(COOKIE); if (cookieString != null) { CookieDecoder cookieDecoder = new CookieDecoder(); Set<Cookie> cookies = cookieDecoder.decode(cookieSt

  • SQL - how to return multiple rows with one SQL query? 2012-03-08

    I have a managed bean which makes SQL queries to Oracle database. This is just very simple example how I make SQL queries. This is the table structure: GLOBALSETTINGS --------------------------------- SessionTTL VARCHAR2(40 BYTE) MAXACTIVEUSERS NUMBE

  • validating multiple forms with one button in jquery validation 2013-06-17

    I am trying to validate but not submit multiple forms with one button using the jquery validation plugin. Here is a simplified version of my code <script> $(document).ready(function() { var validator1 = $("#form1").validate({ rules: { field1: {

  • Using information from multiple databases with one user 2014-03-05

    I am trying to access information from multiple databases with one user account. Even though I am fairly sure the user has rights to both databases, MySQL is rejecting my SELECT query on the second database. My SQL query looks like this: SELECT a.id

  • Multiple phones with one Live ID. 2014-04-06

    We have just purchased 15 htc mozarts. Is there any issues around having the same windows live ID on all of the phones? Don't want to have to create multiple accounts. Do I really need to assign a live ID to each phone anyway? As long as I can update

  • Spring - handle multiple forms submit with one controller 2009-12-03

    Is it possible to have one controller to handle multiple forms in spring? For example, i have 3 steps registration that map with "/register" url. Is it possible to have only one RegisterController that can handle all registration's steps form submit?

  • EF and repository pattern - ending up with multiple DbContexts in one controller - any issues (performance, data integrity)? 2012-02-04

    Most of my knowledge of ASP.NET MVC 3 comes from reading through the book Pro ASP.NET MVC 3 Framework by Adam Freeman and Steven Senderson. For my test application I have tried to stick to their examples very closely. I am using the repository patter

  • Multiple Controllers with one Name in ASP.NET MVC 2 2010-04-13

    I receive the following error when trying to run my ASP.NET MVC application: The request for 'Account' has found the following matching controllers: uqs.Controllers.Admin.AccountController MvcApplication1.Controllers.AccountController I searched the

  • Question about editing by multiple Models and one controller 2011-09-02

    i have three Models(word,Adjectiv,Adverb) and one Controller(Words) that manage these Models.I have only one form,that user can add an adverb,an Adjectiv or a word. My edit function: def edit @word=Word.find(params[:id]) @adjektiv=Adjektiv.find(param

  • Changing various properties of various elements with one controller in jQuery: the right way? 2012-02-24

    Seems like I'm stuck with jQuery selectors. I would like to have one controller to change for instance css color value of colorsample element and html in value element. The page goes like this: <span class="controller blue">██</span> <

  • Objective - C How to manage multiple views with View Controller iphone 2013-04-16

    I am new in developing iOS apps. I am trying to develop a multiple views app. My doubt is how to manage a multiple views app with View Controller, I mean, I do not want to use Navigation Controller nor Tab Controller. My idea is to show a first View

  • Java - Handle multiple events with one function? 2009-02-01

    First of all, I am a complete Java NOOB. I want to handle multiple button presses with one function, and do certain things depending on which button was clicked. I am using Netbeans, and I added an event with a binding function. That function is sent

  • Print multiple variables with one command in jdb 2009-09-28

    How can I print multiple local primitives with one command in jdb (verison 1.4)? I've tried: print v1, v2, v3 but that only prints out the value of v1. Any suggestions? Edit: Effectively I need a watch in jdb. --------------Solutions------------- Doe

  • Update multiple rows with one query? 2010-03-27

    I found something that works with updating one field at here: http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/ UPDATE person SET name = CASE id WHEN 1 THEN 'Jim' WHEN 2 THEN 'Mike' WHEN 3 THEN

  • How to JSP display multiple page with one layout (Example index.jsp?page=about display about.jsp) 2010-07-16

    I new in JSP, i have a problem with JSP in php i use $page=$_GET["page"] for display multiple page for one layout it mean i have index , it display layout and when i click on menu go to about us the index url = index.jsp?page=about in PHP when i decl

  • mysql INSERT into multiple tables with one statement 2010-09-08

    Is it possible to insert data into multiple tables in one statement? For example, I have two tables (live_products and products) they are the same, except live_products is highly transactional and I keep the record count low (delete once done), where

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.165 (s). 11 q(s)