1

Media player class is good. However, I can't play a mp3 file stored in another class (when mouse clicked). Could someone check my code?

 package mediaplayer;

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

     public class MediaPlayer extends Application {
         private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";
         private static String arg1;

         @Override public void start(Stage stage) {
             stage.setTitle("Media Player");
             Group root = new Group();
             Scene scene = new Scene(root,600,265);
             // create media player
             Media media = new Media((arg1 != null) ? arg1 : MEDIA_URL);
             javafx.scene.media.MediaPlayer mediaPlayer = new javafx.scene.media.MediaPlayer(media);
             mediaPlayer.setAutoPlay(true);
             MediaControl mediaControl = new MediaControl(mediaPlayer);
             scene.setRoot(mediaControl);
              scene.getStylesheets().add(MediaPlayer.class.getResource("mediaplayer.css").toExternalForm());
             // show stage
             stage.setScene(scene);
             stage.show();
         }

         public static void main(String[] args) {
             if (args.length > 0) {
                 arg1 = args[0];
             }
             Application.launch(args);
         }
     }

This is the class I am trying to use to play my audio file:

     package mediaplayer;

     import java.awt.Cursor;

     /**
      *
      * @author Yves
      */
     public class LacherPrise extends javax.swing.JFrame {

         /**
          * Creates new form LacherPrise
          */
         public LacherPrise() {

             this.setVisible(true);
             // définition de la taille de la fenêtre de l’éditeur
             setBounds(200, 100, 800, 600);

             initComponents();
         }

When I run the program (on the mouseclicked below), I get these two errors: Error no1: Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not yet implemented Error no2:Exception in thread "Thread-3" java.lang.IllegalStateException: Toolkit not initialized I would need help on how to implement Exception.

private void audio010MouseClicked(java.awt.event.MouseEvent evt) {                                      
    //getting URL to a sound file stored locally
    String MEDIA_URL = "file:///C:/Users/Yves/Documents/NetBeansProjects/ExamenFinSessionJavaFX/src/RessourcesLacherPrise/Aff010.mp3";
    Media media = new Media(MEDIA_URL.toString());
    MediaPlayer MediaPlayer = new MediaPlayer();
    MediaPlayer.play(MEDIA_URL);
}                  
  • 1
    what happens when you try to play the mp3? – Sam I am says Reinstate Monica Dec 14 '12 at 22:27
  • When I run the program (on the mouseclicked below), I get these two errors: Error no1: Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not yet implemented Error no2:Exception in thread "Thread-3" java.lang.IllegalStateException: Toolkit not initialized I would need help on how to implement Exception. – user1905218 Dec 15 '12 at 19:58

1 Answers1

0

The main problem i believe is that the JavaFX toolkit has not been initialized. Take a look at the following question:

JavaFX 2.1: Toolkit not initialized

Simply putting this at the start of your code could solve the problem:
new JFXPanel();

Community
  • 1
  • 1
Alexander
  • 1,332
  • 10
  • 17