URI语法和转换为字符串的困难

3

我和我的朋友一直在开发一个Java项目,使用Media、MediaPlayer和MediaView类创建一个简单的媒体播放器。然而,从一开始我们就遇到了成功打开我们用作测试文件的视频的问题。经过许多愤怒的运行时异常后,我们终于找到了问题的根源:传递给每个对象的字符串(Media需要以URI格式表示文件路径的字符串)。经过一些修改,我们发现以下URI可在我的计算机上打开该文件:

Media m = new Media("file:///C:/Users/mewww/Google%20Drive/Java/SmartPlay/EXO-MonsterMV.mp4");
MediaPlayer mp = new MediaPlayer(m);
MediaView mv = new MediaView(mp);

然而,我们后来尝试实现一个Open方法,允许用户选择他们想要播放的文件(作为File对象)。当我们这样做时,我们使用以下代码打开文件:

File currentFile = new File(null);

FileChooser fc = new FileChooser();
fc.setTitle("Open");
currentFile = fc.showOpenDialog(null);

Media m = new Media(currentFile.toURI().toString());
MediaPlayer mp = new MediaPlayer(m);
MediaView mv = new MediaView(mp);

这个问题又导致我们出现了运行时异常,所以我们使用println将问题输出到控制台以找出问题所在。使用的字符串与应该使用的字符串相比,现在少了两个斜杠:

"file:/C:/Users/mewww/Google%20Drive/Java/SmartPlay/EXO-MonsterMV.mp4"

然而,即使修改了字符串,一旦选择文件,我们仍然会收到相同的运行时错误:
Exception in Application start method
java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)

我们随后将整个Open方法注释掉,回到了我们的原始代码,但仍然收到相同的错误。

我们的完整代码在此处可用:

SmartPlay类

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import java.io.File;
import javafx.stage.FileChooser;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.application.Platform;

public class SmartPlay extends Application {
    File currentFile;
    Scene scene;

  @Override
  public void start(Stage primary) {
    primary.setTitle("SmartPlay");
    selectCurrentFileToOpen();
  //Player(currentFile.toURI().toString().substring(0,5)+"//"+currentFile.toURI().toString().substring(5));
    Player player = new Player("file:///C:/Users/mewww/Google%20Drive/Java/SmartPlay/EXOMonsterMV.mp4");

    scene = new Scene(player, 720, 480, Color.BLACK);
    player.setTop(makeMenus());

    primary.setScene(scene);
    primary.show();
  }

  private MenuBar makeMenus() {
      MenuBar mb = new MenuBar();
      Menu fileMenu = new Menu("File");
      MenuItem openItem = new MenuItem("Open...");
      openItem.setOnAction(e -> {
          selectCurrentFileToOpen();
          scene.setRoot(new Player(currentFile.toURI()));
      });
      MenuItem quitItem = new MenuItem("Quit");
      quitItem.setOnAction(e -> Platform.exit());
      fileMenu.getItems().addAll(openItem, quitItem);
      return mb;
  }

  public boolean selectCurrentFileToOpen() {
      FileChooser fc = new FileChooser();
      fc.setTitle("Open");
      currentFile = fc.showOpenDialog(null);
      return true;
  }

  public void stop() {
  }

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

玩家类

import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import java.net.URI;

public class Player extends BorderPane {
    Media m;
    MediaPlayer mp;
    MediaView mv;
    Pane p;
    MediaBar bar;

    public Player(String file) {
        m = new Media(file);
        mp = new MediaPlayer(m);
        mv = new MediaView(mp);

        p = new Pane();
        p.getChildren().addAll(mv);
        setCenter(p);

        bar = new MediaBar(mp);

        setBottom(bar);

        setStyle("-fx-background-color:#cccccc");

        mp.play();
    }
}

MediaBar 类

import javafx.scene.layout.HBox;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.media.MediaPlayer;
import javafx.scene.layout.Priority;
import javafx.scene.control.Slider;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.util.Duration;

public class MediaBar extends HBox {
    Slider time = new Slider();
    Slider vol = new Slider();

    Button playButton = new Button("Pause");
    Button halfSpeed = new Button("0.5x");
    Button normalSpeed = new Button("1.0x");
    Button doubleSpeed = new Button("2.0x");

    Label volume = new Label("Volume: ");
    Label nowTime;

    MediaPlayer player;

    public MediaBar(MediaPlayer play) {
        player = play;

        setAlignment(Pos.CENTER);
        setPadding(new Insets(5,10,5,10));

        vol.setPrefWidth(70);
        vol.setMinWidth(30);
        vol.setValue(100);

        nowTime = new Label(formatTime(player.getCurrentTime()) + "/" + formatTime(player.getTotalDuration()));
        HBox.setHgrow(time, Priority.ALWAYS);

        playButton.setPrefWidth(30);

        getChildren().addAll(playButton,time,nowTime,volume,vol);        
    }

    public static String formatTime(Duration duration) {  //StackOverflow: Jon Skeet
        long seconds = (long) duration.toSeconds();
        long absSeconds = Math.abs(seconds);
        String positive = String.format(
            "%d:%02d:%02d",
            //absSeconds / 3600,
            (absSeconds % 3600) / 60,
            absSeconds % 60);
        return seconds < 0 ? "-" + positive : positive;
    }
}

2
你实际得到的是 "file:/C:/Users/mewww/Google%20Drive/Java/SmartPlay/EXO-MonsterMV.mp4" 还是你认为看到的?如果是前者,为什么要加引号?请提供剩余的堆栈跟踪信息。 - user207421
这就是在控制台日志中打印出来的内容,我只是用引号将其标识为字符串。 - Jack Foster
我尝试使用API指南查找确切所需的语法,但URI资源指南的链接已经失效。RFC:2396特别提到了这一点。 - Jack Foster
额外的斜杠不会有任何影响。我仍然想看到真正的异常。new Player(...) 真的会抛出 RuntimeException 吗? - user207421
所以,在阅读了mkahihu的帖子之后,我注意到在cmd中运行它实际上可以获得完整的堆栈跟踪(我一直在使用Dr. Java,它没有提供如此具体的错误消息)。从那里我注意到它提到了%02d(正如mkahihu也指出的那样),这是我在MediaBar类(formatTime())中使用的过时格式化方法。所以我将formatTime()更改为使用toSeconds(),现在它可以工作了。不幸的是,我无法提供原始的堆栈跟踪,因为一旦我让它工作起来,我就清除了我的先前版本。 - Jack Foster
显示剩余4条评论
1个回答

1

我在命令行中运行了您的代码,成功获取到了更具体的调试错误。看起来似乎是您在MediaBar中进行的时间格式化导致了错误。我不知道您具体想要做什么,但您格式化时间的方式是不正确的。如果您将其注释掉以及其他用于添加时间格式的内容,URI路径就会正确,您的视频也应该可以正常播放。我知道您缺少一个'%02d'来进行格式化,但对于您要格式化的内容,我不太确定,所以无法帮助您。


我认为,对于你想要做的事情,使用位于java.time和Duration类中的toMinutes()和toSeconds()会更容易。这将有助于转换并仅格式化您想要的内容。 - mkahihu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接