[SOLVED] How can I play a video in jme?

how can I play a video in jme?

edited:
I tried JavaFX and it was not straightforward and has a lot of problems in codecs…
I also used JavaFX webView to play video but not it working correctly because of the video codecs. I expected the web engine to work properly but it did not.
finally, I used SimpleMediaPlayer and it works :slight_smile:

1 Like

https://hub.jmonkeyengine.org/search?q=play%20video%20

Looks like you are in for an adventure.

3 Likes

Maybe this can help you:

2 Likes

You can use JavaFX, or VLC, or about 10 different other ways.

2 Likes

I have to install vlcPlayer for using that way?

1 Like

Yes.

1 Like

but it is not straight forward…
what about final user? Does it have to install a vlcPlayer? is not possible to embed vlc dll/so files in a jar file and load it according to os?

The user must have VLC installed, or your “game” must come with an installer that installs it. I don’t know about repackaging VLC. It may or may not be a violation of its license. I have no idea.

1 Like

do you know who did write the jme-jfx library?

@javasabr and me. And several contributors.

yes, VLCj , you can make a dir containing vlc & attach it to your game dir so users when downloading your game , if they donot have vlc , the one you attached it to your game dir would take over the functionality ,So ,you donot have to repackage it into an installer ,you can take the whole directory of the installed one on your pc , but this will increase your game size , so its better to use jfx video player,

                Media media = new Media(path);
                MediaPlayer player = new MediaPlayer(media);
                player.setAutoPlay(autoplay);
                mediaView.setMediaPlayer(player);

mediaView (just a widget with an id that takes in videos) can take over the stop & play actions if you press it(onClickListener) , i donot remember exactly if it creates an auto seekBar for you or donot , but anyway this must run on a jfx context !

1 Like

I tried to use it to play video but nothing plays.

public class GamestartUpVideoState implements AppState {

    /**
     * is this appsate initialized?
     */
    private boolean is_initialized = false;

    /**
     * set appstate enable/dissable state
     */
    private boolean is_enabled = false;

    /**
     * timer for the time of execution of the main loop update
     */
    private float timer = 0;

    /**
     * timer limit that limit the timer of the main loop update method in
     * seconds
     */
    private final float timer_limit = 1.0f;

    /**
     * it will keeps instance of this class for singletone
     */
    private static GamestartUpVideoState instance = null;

    /**
     * directory of data and its save file
     */
    private final String data_dir = tools.get_root_dirctory(this) + "DATA" + File.separator;

    /**
     * it will return instance of this class for singletone
     *
     * @return instance
     */
    public static GamestartUpVideoState get_instance() {
        if (instance == null) {
            instance = new GamestartUpVideoState();
        }
        return instance;
    }
    private MediaPlayer mp;
    private TextureMovie textureMovie;
    private final Geometry screen1;

    /**
     * private initializer because of the singletone design pattern
     */
    private GamestartUpVideoState() {
        PlatformImpl.startup(() -> {
        });

        final Media media = new Media(new File(data_dir + "move.mp4").toURI().toASCIIString());

        media.errorProperty().addListener(new ChangeListener<MediaException>() {

            @Override
            public void changed(final ObservableValue<? extends MediaException> observable, final MediaException oldValue, final MediaException newValue) {
                newValue.printStackTrace();
            }
        });
        this.mp = new MediaPlayer(media);
        this.textureMovie = new TextureMovie(globalVars.app, this.mp, LetterboxMode.VALID_LETTERBOX);
        this.textureMovie.setLetterboxColor(ColorRGBA.Black);

        screen1 = new Geometry("Screen1", new Quad(200, 200));

        final Material s1mat = new Material(globalVars.get_intance().assetManager, "com/jme3x/jfx/media/MovieShader.j3md");
        s1mat.setTexture("ColorMap", this.textureMovie.getTexture());
//        s1mat.setInt("SwizzleMode", this.textureMovie.useShaderSwizzle());
        screen1.setMaterial(s1mat);

    }

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        is_initialized = true;

        globalVars.app.getGuiNode().attachChild(screen1);
    }

    @Override
    public void update(float tpf) {
        timer += tpf;
        if (timer >= (timer_limit / globalVars.game_speed_factor)) {
            timer = 0;
            System.out.println(mp.getCurrentTime());
        }
    }

    @Override
    public boolean isInitialized() {
        return is_initialized;
    }

    @Override
    public void setEnabled(boolean active) {
        this.is_enabled = active;
        this.mp.play();
    }

    @Override
    public boolean isEnabled() {
        return is_enabled;
    }

    @Override
    public void stateAttached(AppStateManager stateManager) {

    }

    @Override
    public void stateDetached(AppStateManager stateManager) {
        instance = null;

        this.mp.stop();
        PlatformImpl.exit();
    }

    @Override
    public void render(RenderManager rm) {

    }

    @Override
    public void postRender() {

    }

    @Override
    public void cleanup() {

    }

}
1 Like

I never used javafx in jme, can you share a piece of code that plays a video using jfx and mediaView on a jfx context?

1 Like

Do you hear something like a music is playing in the background because in jfx media library if you donot attach your video to the MediaView widget you will end up playing it as a music only !

1 Like

Ingnore the int values , they are acting as switches here !

package DRG208.ConfigsKT;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;

import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.util.Duration;

import javax.swing.*;

import java.awt.event.ActionListener;

import static DRG208.Controller.pressant_1;

public  class PlayVideo{
    int playing=0;


    public PlayVideo(MediaView mediaView, String path, Button playPauseBtn, Slider mediaSlider, boolean autoplay) {
        try {
                Media media = new Media(path);
                MediaPlayer player = new MediaPlayer(media);
                player.setAutoPlay(autoplay);
                mediaView.setMediaPlayer(player);

                mediaView.setOnMouseClicked(event -> {
                    switch (playing) {
                        case 1:
                            player.pause();
                            playing = 0;
                            break;
                        case 0:
                            player.play();
                            playing = 1;
                            break;
                    }
                });
                mediaView.setOnKeyTyped(event -> {
                    switch (event.getCode()){
                        case  G:
                            player.seek(Duration.seconds(0.2));
                            JOptionPane.showConfirmDialog(null,"");
                            break;
                    }
                });


                player.play();
                playing = 1;


//                mediaSlider.setMax(player.getTotalDuration().toMillis());
//            Timer t=new Timer(500,action->{
//               mediaSlider.adjustValue(player.getVolume());
//            });
//                t.start();

            playPauseBtn.setVisible(true);
                playPauseBtn.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        switch (playing) {
                            case 1:
                                player.pause();
                                playing = 0;
                                break;
                            case 0:
                                player.play();
                                playing = 1;
                                break;
                        }
                    }
                });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

calling it , focus on the constructor part of the Controller.java , Controller.java is called within th fxml file that holds the front-end & id of widgets:

package DRG208;

import javafx.scene.chart.AreaChart;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.TextArea;
import  DRG208.ConfigsKT.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Controller {
    public AreaChart graph;
    public TextArea info;
    public Button btn;
    public MediaView mediaView;
    public Button btnPlayPause;
    public Button plasmahalflifebtn;
    public Slider mediaSlider;

    public void Plasma_Half_Life(){

       new PlayVideo(mediaView, "file:///E:/Androidprogramming/Videos/Android.mp4", btnPlayPause,null, false);
    }
   
}

EDIT : let me see your code when you finish it beacuse i didnot try this with jme

1 Like

but the problem is I don’t know how to use JavaFX in jme!

1 Like

You have to use java-11 and follow the instructions. But the thing is you don’t just want to use javafx to play a video. That’s a very expensive way to play a video. There are other solutions. The only reason you’d want to use JavaFX is if you’re going to use it as a GUI, not to just play videos.

2 Likes

what does this part do , because i cannot see a MediaView here may be this one ?

& whats your stackTrace ?

1 Like
1 Like

I think it will change the texture of screen1 to a frame of the playing video.

1 Like