javabuttonjavafxmediaview

ToogleButton with MediaView. JavaFX/Java


I'm trying to make an imitation of TV screen using java, javaFX and scenebulider. Right now I'm able to play the video but I don't know how to make it off at first, then after a button press it's going to play. Here's my Controller code

public class Controller implements Initializable {

@FXML
private Slider suwak2;

@FXML
private Slider suwak5;

@FXML
private Slider suwak1;

@FXML
private Slider suwak4;

@FXML
private Slider suwak6;

@FXML
private Slider suwak3;

@FXML
private ToggleButton wlacznik;


@FXML
private MediaView mv;
private MediaPlayer mp;
private Media me;

@Override
public void initialize(URL location, ResourceBundle resources)
{
    String path=new File("src/application/lol.mp4").getAbsolutePath();
    Path clip = new Path(new MoveTo(50, 400), new ArcTo(50, 200, 0, 50, 0, false, true), new HLineTo(450), new ArcTo(50, 200, 0, 350, 400, false, true), new ClosePath());
    clip.setFill(Color.BLACK);
    clip.setStroke(null);
    mv.setClip(clip);
    me = new Media(new File(path).toURI().toString());
    mp = new MediaPlayer(me);
    mv.setMediaPlayer(mp);
    mp.setAutoPlay(true);
    mp.setCycleCount(MediaPlayer.INDEFINITE);
    suwak1.setValue(mp.getVolume() * 100); 
    suwak1.valueProperty().addListener(new InvalidationListener() {

        @Override
        public void invalidated(Observable observable) {
            mp.setVolume(suwak1.getValue()/100);
        }

    });
    }
 }

suwak1 is my volume slider, wlacznik is my toogle button to make it on and off.


Solution

  • Maybe you need a Button for Play/Stop

       Button stop = new Button("STOP");
       stop.setOnAction((e) -> mp.stop());
    
       Button play = new Button("Play");
       play.setOnAction((e) -> mp.play());