使用 javafx 获取要播放的 mp3 文件 [英] Getting a mp3 file to play using javafx
问题描述
我今天花了几个小时寻找如何在 eclipse 中获得某种形式的音频,并且在每一步都遇到了麻烦.目前我有一些应该可以工作的东西,但我收到一个错误:
I have spent hours today looking up how to get some form of audio in eclipse and have had trouble every step of the way. Currently I have something that should work but I get an error:
线程main"中的异常java.lang.IllegalArgumentException:期望的文件名作为参数在 com.sun.javafx.css.parser.Css2Bin.main(Css2Bin.java:44)
Exception in thread "main" java.lang.IllegalArgumentException: expected file name as argument at com.sun.javafx.css.parser.Css2Bin.main(Css2Bin.java:44)
我基本上是从使用它的人那里复制的.我想说的是 FX 库被添加到了它应该在的地方.我知道这并不花哨,但我只是在尝试基础知识.
I have basically copied this from someone who had it working. I would like to say that the FX lib is added where it should be. I know this isn't fancy but I was just trying the basics.
package b;
import java.io.File;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class test {
public static void main(String[] args){
String uriString = new File("C:\Users\Mike\workspace\b\src\hero.mp3").toURI().toString()
MediaPlayer player = new MediaPlayer( new Media(uriString));
player.play();
}}
我还尝试了许多不同的路径名,以防万一它不走运,我也只是尝试复制并粘贴我通过转到属性(例如:/b/src/hero.mp3)在 Eclipse 中获得的路径名.帮助我摆脱这个噩梦将不胜感激.
I have also tried many different path names in case it was wrong with no luck, I also just tried to copy and paste the path name that i got in eclipse by going to properties ex: /b/src/hero.mp3. Help would be appreciated to get me out of this nightmare.
推荐答案
位于工作区之外的文件应包含在 file://
前缀中.演示该功能的一个简单示例是
The files located outside the workspace should be included with file://
prefix. A simple example demonstrating the functionality is
public class Reproductor extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Media media = new Media("file:///Movies/test.mp3"); //replace /Movies/test.mp3 with your file
MediaPlayer player = new MediaPlayer(media);
player.play();
}
}
这篇关于使用 javafx 获取要播放的 mp3 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!