简单的Java视频播放器

一、简单的Java视频播放器

代码如下(示例):

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.util.Duration;
public class Wzs_17 extends Application{
    
    
    String eURL="file:///E:/新海诚的世界.mp4";//----->视频的位置,不要有空格
    @Override
    public void start(Stage stage){
    
    
        Media media=new Media(eURL);
        MediaPlayer mPlayer=new MediaPlayer(media);
        MediaView mView=new MediaView(mPlayer);
        mView.setFitWidth(900);
        mView.setFitHeight(520);
        Button pBut=new Button(">");
        pBut.setOnAction(e ->{
    
    
            if(pBut.getText().equals(">")){
    
    
                mPlayer.play();
                pBut.setText("||");
            }else {
    
    
                mPlayer.pause();
                pBut.setText(">");
            }
        });
        Button rBut=new Button("<<");
        rBut.setOnAction(e->mPlayer.seek(Duration.ZERO));
        Slider sVol=new Slider();
        sVol.setMinWidth(30);
        sVol.setPrefWidth(150);
        sVol.setValue(50);
        mPlayer.volumeProperty().bind(sVol.valueProperty().divide(100));
        HBox hB=new HBox(10);
        hB.setAlignment(Pos.CENTER);
        Label vol=new Label("音响");
        hB.getChildren().addAll(pBut,rBut,vol,sVol);
        BorderPane bPane=new BorderPane();
        bPane.setCenter(mView);
        bPane.setBottom(hB);
        Scene scene=new Scene(bPane);
        stage.setTitle("视频播放器");
        stage.setScene(scene);
        stage.show();
    }
}


播放器效果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46466198/article/details/113729727