问题描述
我无法使用MediaPlayer在JavaFX中播放mp4视频。 它会引发此异常:
MediaException: UNKNowN : [com.sun.media.jfxmediaimpl.platform.gstreamer.GSTMediaPlayer@1ae76b31] ERROR_MEDIA_INVALID: ERROR_MEDIA_INVALID
并且当我使用从oracle网站下载的flv文件时,没有播放,也没有抛出。
为什么以及如何解决该问题?
,其他信息是:
java版本“ 1.8.0_251”
Java(TM)SE运行时环境(build1.8.0_251-b08)
Java HotSpot(TM)64位服务器VM(内部版本25.251-b08,混合模式)
5.4.0-45-Generic#49-Ubuntu SMP Wed Aug 26 13:38:52 UTC 2020 x86_64 x86_64 x86_64 GNU / Linux
package advancedmedia;
import java.io.File;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.changelistener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.borderpane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* An advanced media player with controls for play/pause,seek,and volume.
*
* @see javafx.scene.media.MediaPlayer
* @see javafx.scene.media.Media
*/
public class AdvancedMedia extends Application {
private static final String MEDIA_URL = new File("/Path/to/video/file.flv").toURI().toASCIIString();
private MediaPlayer mediaPlayer;
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root));
mediaPlayer = new MediaPlayer(new Media(MEDIA_URL));
mediaPlayer.setAutoplay(true);
MediaControl mediaControl = new MediaControl(mediaPlayer);
mediaControl.setMinSize(480,280);
mediaControl.setPrefSize(480,280);
mediaControl.setMaxSize(480,280);
root.getChildren().add(mediaControl);
mediaPlayer.setonError(new Runnable() {
@Override
public void run() {
System.out.println(mediaPlayer.getError());
}
});
}
public void play() {
Status status = mediaPlayer.getStatus();
if (status == Status.UNKNowN
|| status == Status.HALTED) {
//System.out.println("Player is in a bad or unkNown state,can't play.");
return;
}
if (status == Status.PAUSED
|| status == Status.STOPPED
|| status == Status.READY) {
mediaPlayer.play();
}
}
@Override
public void stop() {
mediaPlayer.stop();
}
public class MediaControl extends borderpane {
private MediaPlayer mp;
private MediaView mediaView;
private final boolean repeat = false;
private boolean stopRequested = false;
private boolean atEndOfMedia = false;
private Duration duration;
private Slider timeSlider;
private Label playTime;
private Slider volumeSlider;
private HBox mediaBar;
private final Image PlayButtonImage = new Image(AdvancedMedia.class.getResourceAsstream("playbutton.png"));
private final Image PauseButtonImage = new Image(AdvancedMedia.class.getResourceAsstream("pausebutton.png"));
ImageView imageViewPlay = new ImageView(PlayButtonImage);
ImageView imageViewPause = new ImageView(PauseButtonImage);
private Pane mvPane;
private Stage newStage;
private boolean fullScreen = false;
@Override
protected void layoutChildren() {
if (mediaView != null && getBottom() != null) {
mediaView.setFitWidth(getWidth());
mediaView.setFitHeight(getHeight() - getBottom().prefheight(-1));
}
super.layoutChildren();
if (mediaView != null && getCenter() != null) {
mediaView.setTranslateX((((Pane) getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2);
mediaView.setTranslateY((((Pane) getCenter()).getHeight() - mediaView.prefheight(-1)) / 2);
}
}
@Override
protected double computeMinWidth(double height) {
return mediaBar.prefWidth(-1);
}
@Override
protected double computeMinHeight(double width) {
return 200;
}
@Override
protected double computePrefWidth(double height) {
return Math.max(mp.getMedia().getWidth(),mediaBar.prefWidth(height));
}
@Override
protected double computePrefheight(double width) {
return mp.getMedia().getHeight() + mediaBar.prefheight(width);
}
@Override
protected double computeMaxWidth(double height) {
return Double.MAX_VALUE;
}
@Override
protected double computeMaxHeight(double width) {
return Double.MAX_VALUE;
}
public MediaControl(final MediaPlayer mp) {
this.mp = mp;
setStyle("-fx-background-color: #bfc2c7;"); // Todo: Use css file
mediaView = new MediaView(mp);
mvPane = new Pane();
mvPane.getChildren().add(mediaView);
mvPane.setStyle("-fx-background-color: black;"); // Todo: Use css file
setCenter(mvPane);
mediaBar = new HBox(5.0);
mediaBar.setPadding(new Insets(5,10,5,10));
mediaBar.setAlignment(Pos.CENTER_LEFT);
borderpane.setAlignment(mediaBar,Pos.CENTER);
final Button playButton = ButtonBuilder.create()
.minWidth(Control.USE_PREF_SIZE)
.build();
playButton.setGraphic(imageViewPlay);
playButton.setonAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
updateValues();
Status status = mp.getStatus();
if (status == Status.UNKNowN
|| status == Status.HALTED) {
// don't do anything in these states
return;
}
if (status == Status.PAUSED
|| status == Status.READY
|| status == Status.STOPPED) {
// rewind the movie if we're sitting at the end
if (atEndOfMedia) {
mp.seek(mp.getStartTime());
atEndOfMedia = false;
playButton.setGraphic(imageViewPlay);
//playButton.setText(">");
updateValues();
}
mp.play();
playButton.setGraphic(imageViewPause);
//playButton.setText("||");
} else {
mp.pause();
}
}
});
mp.currentTimeproperty().addListener(new changelistener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> observable,Duration oldValue,Duration newValue) {
updateValues();
}
});
mp.setonPlaying(new Runnable() {
public void run() {
if (stopRequested) {
mp.pause();
stopRequested = false;
} else {
playButton.setGraphic(imageViewPause);
//playButton.setText("||");
}
}
});
mp.setonPaused(new Runnable() {
public void run() {
playButton.setGraphic(imageViewPlay);
//playButton.setText("||");
}
});
mp.setonReady(new Runnable() {
public void run() {
duration = mp.getMedia().getDuration();
updateValues();
}
});
mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1);
mp.setonEndOfMedia(new Runnable() {
public void run() {
if (!repeat) {
playButton.setGraphic(imageViewPlay);
//playButton.setText(">");
stopRequested = true;
atEndOfMedia = true;
}
}
});
mediaBar.getChildren().add(playButton);
// Time label
Label timeLabel = new Label("Time");
timeLabel.setMinWidth(Control.USE_PREF_SIZE);
mediaBar.getChildren().add(timeLabel);
// Time slider
timeSlider = SliderBuilder.create()
.minWidth(30)
.maxWidth(Double.MAX_VALUE)
.build();
HBox.setHgrow(timeSlider,Priority.ALWAYS);
timeSlider.valueproperty().addListener(new InvalidationListener() {
public void invalidated(Observable ov) {
if (timeSlider.isValueChanging()) {
// multiply duration by percentage calculated by slider position
if (duration != null) {
mp.seek(duration.multiply(timeSlider.getValue() / 100.0));
}
updateValues();
}
}
});
mediaBar.getChildren().add(timeSlider);
// Play label
playTime = LabelBuilder.create()
//.prefWidth(130)
.minWidth(Control.USE_PREF_SIZE)
.build();
mediaBar.getChildren().add(playTime);
//Fullscreen button
Button buttonFullScreen = ButtonBuilder.create()
.text("Full Screen")
.minWidth(Control.USE_PREF_SIZE)
.build();
buttonFullScreen.setonAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (!fullScreen) {
newStage = new Stage();
newStage.fullScreenproperty().addListener(new changelistener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> ov,Boolean t,Boolean t1) {
onFullScreen();
}
});
final borderpane borderpane = new borderpane() {
@Override
protected void layoutChildren() {
if (mediaView != null && getBottom() != null) {
mediaView.setFitWidth(getWidth());
mediaView.setFitHeight(getHeight() - getBottom().prefheight(-1));
}
super.layoutChildren();
if (mediaView != null) {
mediaView.setTranslateX((((Pane) getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2);
mediaView.setTranslateY((((Pane) getCenter()).getHeight() - mediaView.prefheight(-1)) / 2);
}
}
;
};
setCenter(null);
setBottom(null);
borderpane.setCenter(mvPane);
borderpane.setBottom(mediaBar);
Scene newScene = new Scene(borderpane);
newStage.setScene(newScene);
//Workaround for disposing stage when exit fullscreen
newStage.setX(-100000);
newStage.setY(-100000);
newStage.setFullScreen(true);
fullScreen = true;
newStage.show();
} else {
//toggle FullScreen
fullScreen = false;
newStage.setFullScreen(false);
}
}
});
mediaBar.getChildren().add(buttonFullScreen);
// Volume label
Label volumeLabel = new Label("Vol");
volumeLabel.setMinWidth(Control.USE_PREF_SIZE);
mediaBar.getChildren().add(volumeLabel);
// Volume slider
volumeSlider = SliderBuilder.create()
.prefWidth(70)
.minWidth(30)
.maxWidth(Region.USE_PREF_SIZE)
.build();
volumeSlider.valueproperty().addListener(new InvalidationListener() {
public void invalidated(Observable ov) {
}
});
volumeSlider.valueproperty().addListener(new changelistener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable,Number oldValue,Number newValue) {
if (volumeSlider.isValueChanging()) {
mp.setVolume(volumeSlider.getValue() / 100.0);
}
}
});
mediaBar.getChildren().add(volumeSlider);
setBottom(mediaBar);
}
protected void onFullScreen() {
if (!newStage.isFullScreen()) {
fullScreen = false;
setCenter(mvPane);
setBottom(mediaBar);
Platform.runLater(new Runnable() {
@Override
public void run() {
newStage.close();
}
});
}
}
protected void updateValues() {
if (playTime != null && timeSlider != null && volumeSlider != null && duration != null) {
Platform.runLater(new Runnable() {
public void run() {
Duration currentTime = mp.getCurrentTime();
playTime.setText(formatTime(currentTime,duration));
timeSlider.setdisable(duration.isUnkNown());
if (!timeSlider.isdisabled() && duration.greaterThan(Duration.ZERO) && !timeSlider.isValueChanging()) {
timeSlider.setValue(currentTime.divide(duration).toMillis() * 100.0);
}
if (!volumeSlider.isValueChanging()) {
volumeSlider.setValue((int) Math.round(mp.getVolume() * 100));
}
}
});
}
}
private String formatTime(Duration elapsed,Duration duration) {
int intElapsed = (int) Math.floor(elapsed.toSeconds());
int elapsedHours = intElapsed / (60 * 60);
if (elapsedHours > 0) {
intElapsed -= elapsedHours * 60 * 60;
}
int elapsedMinutes = intElapsed / 60;
int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 - elapsedMinutes * 60;
if (duration.greaterThan(Duration.ZERO)) {
int intDuration = (int) Math.floor(duration.toSeconds());
int durationHours = intDuration / (60 * 60);
if (durationHours > 0) {
intDuration -= durationHours * 60 * 60;
}
int durationMinutes = intDuration / 60;
int durationSeconds = intDuration - durationHours * 60 * 60 - durationMinutes * 60;
if (durationHours > 0) {
return String.format("%d:%02d:%02d/%d:%02d:%02d",elapsedHours,elapsedMinutes,elapsedSeconds,durationHours,durationMinutes,durationSeconds);
} else {
return String.format("%02d:%02d/%02d:%02d",durationSeconds);
}
} else {
if (elapsedHours > 0) {
return String.format("%d:%02d:%02d",elapsedSeconds);
} else {
return String.format("%02d:%02d",elapsedSeconds);
}
}
}
}
@Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
play();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts,e.g.,in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)