AudioSystem.getAudioInputStream()无法打开wav文件,抛出空指针,表示无法访问。路径写得很正确。
public class GuiAndStreamClass extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
try {
StackPane root = new StackPane();
Button play = new Button("Play");
play.setPrefSize(50, 20);
EventHandler<ActionEvent> buttonHandler;
play.setOnAction(buttonHandler = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
streamMethod();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
});
root.getChildren().add(play);
root.setPrefSize(500, 500);
Scene scene = new Scene(root, 500, 500);
primaryStage = new Stage();
primaryStage.setScene(scene);
primaryStage.setTitle("First JavaFX Application");
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void streamMethod() throws LineUnavailableException {
File fileIn = new File("C:/Users/benutzer1/Desktop/WAV");
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(fileIn);
Clip audioClip = AudioSystem.getClip();
audioClip.open(audioInputStream);
audioClip.open();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}发布于 2020-08-13 18:34:56
应该在清单文件中添加以下行。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />https://stackoverflow.com/questions/63392990
复制相似问题