2015-05-15 144 views
0

目前,我有一個場景,多個媒體視圖,每個媒體視圖都有自己的播放和暫停按鈕,在fxml中。我想知道是否有一種方法可以播放/暫停mediaview的按鈕,而不必爲每個媒體視圖製作播放/暫停控制器。下面是我不希望這樣做的乏味方式的一個例子。使用一個控制器播放/暫停多個MediaViews JavaFX FXML

的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"/> 

控制器

@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; 

} 
+0

我無法從你的描述中看出你目前是如何實現這一點的;你可能需要顯示一些代碼。 –

+0

參考你的問題,我可以推斷你不想同時播放「MediaPlayer」。爲什麼你需要多個'MediaViews'? – ItachiUchiha

回答

1

考慮與按鈕媒體視圖中創建一個custom component

的基本思路看起來像

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(); 
    } 

} 

隨着ControlledMediaView.fxml(在同一個包):

<?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> 

然後你的主FXML可以做

<?xml version="1.0" encoding="UTF-8"?> 

<?import application.ControlledMediaView?> 

<!-- ... --> 

<ControlledMediaView mediaURL="@../vid/vid1.mp4"/> 
<ControlledMediaView mediaURL="@../vid/vid2.mp4"/> 

<!-- ... --> 

您可能需要稍微工作以確保URL正確傳達;如果您想使用SceneBuilder,則需要將ControlledMediaView及其FXML包裝到jar文件中並將其導入到SceneBuilder中。