媒体播放器类很好。但是,我无法播放存储在另一个类中的 mp3 文件(单击鼠标时)。有人可以检查我的代码吗?
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();
}
当我运行程序时(在下面的鼠标单击上),我收到以下两个错误: 错误号 1:线程“AWT-EventQueue-0”中的异常 java.lang.UnsupportedOperationException:尚未实现错误号 2:线程“线程 3 中的异常” " java.lang.IllegalStateException: Toolkit not initialized 我需要有关如何实现异常的帮助。
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);
}