Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

java - Getting a mp3 file to play using javafx

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:

Exception in thread "main" java.lang.IllegalArgumentException: expected file name as argument at com.sun.javafx.css.parser.Css2Bin.main(Css2Bin.java:44)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...