mardi 17 février 2015

how to play mp3 using javafx?

I am trying to play an mp3 file however I am getting an error: Cannot instantiate the type Media. I have no idea how to fix this error. I need the code to play the mp3 file, I also need to get the length of the song in millisecond. Here is my code:



import java.awt.FileDialog;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javafx.embed.swing.JFXPanel;
import javafx.scene.media.MediaPlayer;

import javax.print.attribute.standard.Media;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;

public class MusicProgress{
static URL url = null;

public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame b = new JFrame();
final JFXPanel fxPanel = new JFXPanel();
FileDialog fd = new FileDialog(b, "Pick a file: ", FileDialog.LOAD);
fd.setVisible(true);
//final File file = new File(fd.getDirectory()+ fd.getFile());
try{
final Media medias = new Media(fd.getDirectory()+ fd.getFile());
MediaPlayer mediaPlayer = new MediaPlayer(medias);
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

1 commentaire:

  1. import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.media.Media;
    import javafx.scene.media.MediaPlayer;
    import javafx.scene.media.MediaView;
    import javafx.stage.Stage;

    RépondreSupprimer