簡體   English   中英

AudioSystem.getAudioInputStream(fileIn); 無權訪問該文件並拋出 java.io.FileNotFoundException

[英]AudioSystem.getAudioInputStream(fileIn); have no access to the file and throws java.io.FileNotFoundException

AudioSystem.getAudioInputStream()無法打開 wav 文件拋出 null 指針並表示無法訪問。 路徑寫對了。

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();
        }
    }
}

應在清單文件中添加以下行。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM