【问题标题】:How to to control Javafx media player app by user input [duplicate]如何通过用户输入控制 Javafx 媒体播放器应用程序 [重复]
【发布时间】:2017-02-12 08:32:29
【问题描述】:

我是 java 和 javafx 的新手,我创建了 javafx 媒体播放器,它运行良好,但我尝试使用用户输入来控制暂停和播放功能,例如,如果用户输入 1 应该执行暂停功能和视频暂停。 当我运行我的代码时,什么都没有发生,应用程序继续加载。这可能吗?提前感谢任何帮助。

public class Main extends Application {
Player player ;

@Override
public void start(Stage primaryStage) {

     player =new Player("file:///C:/song.mp4");
    Scene scene =new Scene(player,720,480,Color.BLACK);



    primaryStage.setScene(scene);

    primaryStage.show();
    user();
 }
public void user(){
    Scanner sc =new Scanner(System.in);
    System.out.println("enter 1 to pasue");
    int option =sc.nextInt();
    player.pause1(option);
}


public static void main(String[] args) {
    launch(args);

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    您的应用程序一直在加载,因为sc.nextInt() 正在等待用户输入,从而有效地阻止了执行。您可以为此使用另一个线程...

    @Override
    public void start(Stage primaryStage) {
        MediaPlayer player = new MediaPlayer(new Media("http://www.archive.org/download/moby_dick_librivox/mobydick_000_melville.mp3"));
        MediaView view = new MediaView(player);
        Pane root = new Pane(view);
        Scene scene = new Scene(root);
    
        primaryStage.setTitle("Mp3 player");
        primaryStage.setScene(scene);
        primaryStage.show();
        player.play();
    
        new Thread(() -> { 
            Scanner sc = new Scanner(System.in);
            System.out.println("enter 1 to pasue");
            int option = sc.nextInt();
            if (option == 1) {
                player.stop();
            }
        }).start();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
    

    它会起作用,但这也不是一个好的解决方案。控制台应用程序适用于可以在批处理模式下执行的任务。另一方面,您的应用程序是交互式应用程序,因此您应该考虑切换到事件驱动编程和 GUI,例如,通过使用按钮处理用户操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 2016-05-18
      相关资源
      最近更新 更多