我在桌面应用程序中使用JavaFX来创建媒体播放器以播放本地mp3文件和网址流。但我无法流式传输mp4a AAC格式的URL。
这是一个示例代码:
public class WebRadioDemo extends Application {
Media media;
MediaPlayer mediaPlayer;
@Override
public void start(Stage primaryStage) {
try {
media = new Media (new URL("http://icecastsource2.amri.ca/cjab-mp3").toURI().toString());
} catch (MalformedURLException | URISyntaxException ex) {
Logger.getLogger(WebRadioDemo.class.getName()).log(Level.SEVERE, null, ex);
}
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setAlignment(Pos.TOP_LEFT);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
Button btnMusica = new Button();
btnMusica.setText("Tocar Música");
btnMusica.setAlignment(Pos.CENTER_RIGHT);
btnMusica.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
mediaPlayer = new MediaPlayer(media);
System.out.println("Tocar musica: "+media.getSource());
System.out.println("aaaa: "+mediaPlayer.getMedia());
mediaPlayer.play();
mediaPlayer.setOnError(new Runnable() {
@Override
public void run() {
System.err.println(mediaPlayer.getError());
}
});
}
});
StackPane root = new StackPane();
// root.getChildren().add(btn);
root.getChildren().add(btnMusica);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
当我首先调用aac流时,播放器冻结,没有任何响应,但是当我在本地文件播放或mp3流之后调用aac流时,它给了我一个例外:
MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player!
这是aac流:它在媒体经典,itunes等中发挥正常作用:
http://70.38.9.243:8402/stream
这是mp3流:http://icecastsource2.amri.ca/cjab-mp3
播放非mp3格式的流媒体的最佳方式是什么?