我想.wmv
使用媒体类在 javaFX 中播放,这是我尝试过的以下方式
import java.io.File;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
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.scene.paint.Color;
import javafx.stage.Stage;
public class MoviePlayer extends Application{
public static void main (String [] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Group root = new Group();
try{
//File f=new File("C:/Wildlife.wmv");
//URI u=new URI("C:\\Wildlife.wmv");
Media media = new Media("C:\\Wildlife.wmv");
MediaPlayer player = new MediaPlayer(media);
MediaView view = new MediaView(player);
root.getChildren().add(view);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
player.play();
}
catch(Exception e)
{
e.printStackTrace();
}
}}
但它抛出以下异常
java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Wildlife.wmv
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at MoviePlayer.start(MoviePlayer.java:27)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Unknown Source)
任何人都可以告诉我如何解决它 更新 视频不播放
以下是错误处理后的代码和截图
import java.io.File;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaErrorEvent;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class MoviePlayer extends Application{
public static void main (String [] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Group root = new Group();
String source="file:///C:/share/test.mp4";
Media media;
MediaPlayer mediaPlayer;
MediaView mediaView;
try {
media = new Media(source);
if (media.getError() == null) {
media.setOnError(new Runnable() {
public void run() {
// Handle asynchronous error in Media object.
System.out.println("Handle asynchronous error in Media object");
}
});
try {
mediaPlayer = new MediaPlayer(media);
if (mediaPlayer.getError() == null) {
mediaPlayer.setOnError(new Runnable() {
public void run() {
// Handle asynchronous error in MediaPlayer object.
System.out.println("Handle asynchronous error in MediaPlayer object.");
}
});
mediaView = new MediaView(mediaPlayer);
root.getChildren().add(mediaView);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
mediaPlayer.play();
mediaView.setOnError(new EventHandler() {
public void handle(MediaErrorEvent t) {
// Handle asynchronous error in MediaView.
System.out.println("Handle asynchronous error in MediaView");
}
@Override
public void handle(Event arg0) {
// TODO Auto-generated method stub
}
});
} else {
// Handle synchronous error creating MediaPlayer.
System.out.println("Handle synchronous error creating MediaPlayer");
}
} catch (Exception mediaPlayerException) {
mediaPlayerException.printStackTrace();
// Handle exception in MediaPlayer constructor.
}
} else {
// Handle synchronous error creating Media.
}
} catch (Exception mediaException) {
mediaException.printStackTrace();
// Handle exception in Media constructor.
}
////////////////////////////////////
/* try{
//File f=new File("C:/Wildlife.wmv");
//URI u=new URI("C:\\Wildlife.wmv");
Media media = new Media("file:///C:/share/test.mp4");
MediaPlayer player = new MediaPlayer(media);
MediaView view = new MediaView(player);
root.getChildren().add(view);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
player.play();
}
catch(Exception e)
{
e.printStackTrace();
}*/
}}
这是处理异常后得到的输出
Handle asynchronous error in MediaPlayer object.
Handle asynchronous error in Media object
Handle asynchronous error in MediaPlayer object.