gpt4 book ai didi

java - 音频文件未在Java中播放

转载 作者:行者123 更新时间:2023-12-03 02:33:15 26 4
gpt4 key购买 nike

我正在尝试使用MediaPlayer播放音频文件,但无法正常工作并显示

Exception in thread "main" java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'audio.mp3'

谁能帮我解决这个问题?

这是我的代码:
package playaudio;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class PlayAudio {

public static void main(String[] args) {

String fileName = "audio.mp3";
Media playFile = new Media(fileName);
MediaPlayer mediaPlayer = new MediaPlayer(playFile);
mediaPlayer.play();
}

}

编辑:当我尝试将文件名转换为URI时,它显示以下异常。
Exception in thread "Thread-0" java.lang.IllegalStateException: Toolkit not initialized

码:
package playaudio;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class PlayAudio {

public static void main(String[] args) {

String fileName = "audio.mp3";
Media playFile = new Media(Paths.get(fileName).toUri().toString());
MediaPlayer mediaPlayer = new MediaPlayer(playFile);
mediaPlayer.play();
}

}

最佳答案

Media的构造函数实际上需要一个URI,这意味着您必须将文件名转换为URI,请尝试以下操作:
Media media = new Media(Paths.get(fileName).toUri().toString());

关于java - 音频文件未在Java中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com