1 /*
   2  * Copyright (c) 2008, 2016, 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.Observable;
  36 import javafx.beans.property.DoubleProperty;
  37 import javafx.beans.property.SimpleDoubleProperty;
  38 import javafx.scene.Parent;
  39 import javafx.scene.media.Media;
  40 import javafx.scene.media.MediaPlayer;
  41 import javafx.scene.Scene;
  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  * @docUrl http://docs.oracle.com/javase/8/javafx/media-tutorial/overview.htm#JFXMD101 Using JavaFX Media
  50  * @see javafx.scene.media.MediaPlayer
  51  * @see javafx.scene.media.Media
  52  * @playground arthPos (name="Arth Position", min=-100, max=100)
  53  * @playground fierPos (name="Fier Position", min=-100, max=100)
  54  * @playground arthRate (name="Arth Rate", min=0.1, max=1)
  55  * @playground fierRate (name="Fier Rate", min=0.1, max=1)
  56  * @conditionalFeatures WEB, MEDIA
  57  *
  58  * @related /Media/Advanced Media
  59  * @related /Media/Overlay Media Player
  60  * @related /Media/Streaming Media Player
  61  */
  62 public class AlphaMediaPlayerApp extends Application {
  63 
  64     PlanetaryPlayerPane planetaryPlayerPane;
  65     private MediaPlayer arthPlayer;
  66     private MediaPlayer fierPlayer;
  67 
  68     private final DoubleProperty arthPos = new SimpleDoubleProperty(-90.0);
  69     public final DoubleProperty arthPosProperty() {
  70         return arthPos;
  71     }
  72 
  73     private final DoubleProperty fierPos = new SimpleDoubleProperty(50.0);
  74     public final DoubleProperty fierPosProperty() {
  75         return fierPos;
  76     }
  77 
  78     private final DoubleProperty arthRate = new SimpleDoubleProperty(1.0);
  79     public final DoubleProperty arthRateProperty() {
  80         return arthRate;
  81     }
  82 
  83     private final DoubleProperty fierRate = new SimpleDoubleProperty(1.0);
  84     public final DoubleProperty fierRateProperty() {
  85         return fierRate;
  86     }
  87 
  88     public Parent createContent() {
  89         String ARTH_URL =
  90             "http://download.oracle.com/otndocs/products/javafx/arth_512.flv";
  91         String FIER_URL =
  92             "http://download.oracle.com/otndocs/products/javafx/fier_512.flv";
  93         arthPlayer = new MediaPlayer(new Media(ARTH_URL));
  94         arthPlayer.setAutoPlay(true);
  95         fierPlayer = new MediaPlayer(new Media(FIER_URL));
  96         fierPlayer.setAutoPlay(true);
  97 
  98         arthPos.addListener((Observable observable) -> {
  99             planetaryPlayerPane.setTranslate1(arthPos.doubleValue());
 100         });
 101 
 102         fierPos.addListener((Observable observable) -> {
 103             planetaryPlayerPane.setTranslate2(fierPos.doubleValue());
 104         });
 105 
 106         arthRate.addListener((Observable observable) -> {
 107             arthPlayer.setRate(arthRate.doubleValue());
 108         });
 109 
 110         fierRate.addListener((Observable observable) -> {
 111             fierPlayer.setRate(fierRate.doubleValue());
 112         });
 113 
 114         String alphaMediaPlayerCss =
 115             getClass().getResource("AlphaMediaPlayer.css").toExternalForm();
 116 
 117         planetaryPlayerPane = new PlanetaryPlayerPane(arthPlayer, fierPlayer);
 118         planetaryPlayerPane.setMinSize(480, 320);
 119         planetaryPlayerPane.setPrefSize(480, 320);
 120         planetaryPlayerPane.setMaxSize(480, 320);
 121         planetaryPlayerPane.getStylesheets().add(alphaMediaPlayerCss);
 122         return planetaryPlayerPane;
 123     }
 124 
 125     public void play() {
 126         MediaPlayer.Status status = fierPlayer.getStatus();
 127         if (status == MediaPlayer.Status.UNKNOWN ||
 128                 status == MediaPlayer.Status.HALTED) {
 129             return;
 130         }
 131         if (status == MediaPlayer.Status.PAUSED ||
 132                 status == MediaPlayer.Status.STOPPED ||
 133                 status == MediaPlayer.Status.READY) {
 134             fierPlayer.play();
 135             arthPlayer.play();
 136         }
 137     }
 138 
 139     @Override
 140     public void stop() {
 141         fierPlayer.stop();
 142         arthPlayer.stop();
 143     }
 144 
 145     @Override
 146     public void start(Stage primaryStage) throws Exception {
 147         primaryStage.setScene(new Scene(createContent()));
 148         primaryStage.show();
 149         play();
 150     }
 151 
 152     /**
 153      * Java main for when running without JavaFX launcher
 154      * @param args command line arguments
 155      */
 156     public static void main(String[] args) {
 157         launch(args);
 158     }
 159 }