2012-12-14 100 views
1

媒体播放器类很不错。但是,我无法播放存储在另一个类中的mp3文件(当点击鼠标时)。有人可以检查我的代码吗?使用另一个级别的JavaFX mediaplayer播放音频文件

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

这是我试图用它来播放我的音乐文件中的类:

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

当我运行程序(在下面的mouseClicked),我得到这两个错误: 错误NO1 :线程“AWT-EventQueue-0”中的异常java.lang.UnsupportedOperationException:尚未实现 错误no2:线程“Thread-3”中的异常java.lang.IllegalStateException:工具包未初始化 我需要关于如何实现例外。

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

当您尝试播放MP3会发生什么? –

+0

当我运行程序(在下面的鼠标点击)时,我得到这两个错误:错误no1:线程“AWT-EventQueue-0”中的异常java.lang.UnsupportedOperationException:尚未实现错误no2:线程中的异常“ 3“java.lang.IllegalStateException:Toolkit未初始化我需要关于如何实现Exception的帮助。 – user1905218

回答

相关问题