1 /*
   2  * Copyright (c) 2008, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package ensemble.samples.media.alphamediaplayer;
  33 
  34 import javafx.application.Application;
  35 import javafx.beans.InvalidationListener;
  36 import javafx.beans.Observable;
  37 import javafx.beans.property.SimpleDoubleProperty;
  38 import javafx.scene.Parent;
  39 import javafx.scene.Scene;
  40 import javafx.scene.media.Media;
  41 import javafx.scene.media.MediaPlayer;
  42 import javafx.stage.Stage;
  43 
  44 /**
  45  * An alpha media player with 2 different media views and alpha channels.
  46  *
  47  * @sampleName Alpha Media Player
  48  * @preview preview.png
  49  * @see javafx.scene.media.MediaPlayer
  50  * @see javafx.scene.media.Media
  51  * @related /Media/Advanced Media
  52  * @related /Media/Overlay Media Player
  53  * @related /Media/Streaming Media Player
  54  * @playground arthPos (name="Arth Position", min=-100, max=100)
  55  * @playground fierPos (name="Fier Position", min=-100, max=100)
  56  * @playground arthRate (name="Arth Rate", min=0.1, max=1)
  57  * @playground fierRate (name="Fier Rate", min=0.1, max=1)
  58  * @conditionalFeatures WEB
  59  */
  60 public class AlphaMediaPlayerApp extends Application {
  61 
  62     private String alphaMediaPlayerCss = AlphaMediaPlayerApp.class.getResource("AlphaMediaPlayer.css").toExternalForm();
  63     private static final String ARTH_URL = "http://download.oracle.com/otndocs/products/javafx/arth_512.flv";
  64     private static final String FIER_URL = "http://download.oracle.com/otndocs/products/javafx/fier_512.flv";
  65     PlanetaryPlayerPane planetaryPlayerPane;
  66     private MediaPlayer arthPlayer;
  67     private MediaPlayer fierPlayer;
  68     SimpleDoubleProperty arthPos = new SimpleDoubleProperty(-90.0);
  69     SimpleDoubleProperty fierPos = new SimpleDoubleProperty(50.0);
  70     SimpleDoubleProperty arthRate = new SimpleDoubleProperty(1.0);
  71     SimpleDoubleProperty fierRate = new SimpleDoubleProperty(1.0);
  72 
  73     public Parent createContent() {
  74         arthPlayer = new MediaPlayer(new Media(ARTH_URL));
  75         arthPlayer.setAutoPlay(true);
  76         fierPlayer = new MediaPlayer(new Media(FIER_URL));
  77         fierPlayer.setAutoPlay(true);
  78 
  79         arthPos.addListener((Observable observable) -> {
  80             planetaryPlayerPane.setTranslate1(arthPos.doubleValue());
  81         });
  82 
  83         fierPos.addListener((Observable observable) -> {
  84             planetaryPlayerPane.setTranslate2(fierPos.doubleValue());
  85         });
  86 
  87         arthRate.addListener((Observable observable) -> {
  88             arthPlayer.setRate(arthRate.doubleValue());
  89         });
  90 
  91         fierRate.addListener((Observable observable) -> {
  92             fierPlayer.setRate(fierRate.doubleValue());
  93         });
  94 
  95         planetaryPlayerPane = new PlanetaryPlayerPane(arthPlayer, fierPlayer);
  96 
  97         planetaryPlayerPane.setMinSize(480, 320);
  98         planetaryPlayerPane.setPrefSize(480, 320);
  99         planetaryPlayerPane.setMaxSize(480, 320);
 100         planetaryPlayerPane.getStylesheets().add(alphaMediaPlayerCss);
 101         return planetaryPlayerPane;
 102     }
 103 
 104     public void play() {
 105         MediaPlayer.Status status = fierPlayer.getStatus();
 106         if (status == MediaPlayer.Status.UNKNOWN || status == MediaPlayer.Status.HALTED) {
 107             return;
 108         }
 109         if (status == MediaPlayer.Status.PAUSED || status == MediaPlayer.Status.STOPPED || status == MediaPlayer.Status.READY) {
 110             fierPlayer.play();
 111             arthPlayer.play();
 112         }
 113     }
 114 
 115     @Override
 116     public void stop() {
 117         fierPlayer.stop();
 118         arthPlayer.stop();
 119     }
 120 
 121     @Override
 122     public void start(Stage primaryStage) throws Exception {
 123         primaryStage.setScene(new Scene(createContent()));
 124         primaryStage.show();
 125         play();
 126     }
 127 
 128     /**
 129      * Java main for when running without JavaFX launcher
 130      * @param args command line arguments
 131      */
 132     public static void main(String[] args) {
 133         launch(args);
 134     }
 135 }