VLCJ cannot display video after pause() followed by play() - java

I'm using VLCJ for playing mp4 video in my swing application. It can play properly but when I pause() the video followed by a play() it cannot display video but it is able to resume audio. If I call stop() instead of pause() everything works fine but it starts playing from the beginning. How can I pause the video properly so that I can resume the video?
I was trying with the following class:
public class JVLCPlayerPanel extends javax.swing.JPanel
{
private File vlcPath = new File("C:\\Program Files\\VideoLAN\\VLC");
private EmbeddedMediaPlayer player;
public JVLCPlayerPanel() {
initComponents();
NativeLibrary.addSearchPath("libvlc", vlcPath.getAbsolutePath());
EmbeddedMediaPlayerComponent videoCanvas = new EmbeddedMediaPlayerComponent();
this.setLayout(new BorderLayout());
this.add(videoCanvas, BorderLayout.CENTER);
this.player = videoCanvas.getMediaPlayer();
this.player.setPause(true);
}
public void play(String media)
{
player.prepareMedia(media);
player.parseMedia();
player.play();
}
public void pause()
{
player.pause();
}
public void resume()
{
player.play();
}
}

try using this code use pause() method of MediaPlayer
and use same pause() method to play it works for me
pauseButton.addActionListener((ActionEvent e) -> {
mediaPlayerComponent.getMediaPlayer().pause();
});

According to your post and comments, you are trying to switch a single media player component between multiple panels.
This simply does not work.
With vlcj/LibVLC the component that hosts the video surface must be "displayable" and must remain so at all times. You can not "re-parent" the video surface to another component, you can not even hide the component (you can set it to 1x1, or the best solution is to use a CardLayout, to hide it).
The reason it works when you first stop then play is that in this case vlcj will "re-associate" the video surface with the native media player. This can not be done while the video is playing. There's nothing you can do about it.
Probably keeping one media player per panel, and using a CardLayout, is the best solution.

Related

Overlapping music in a Java game with LibGDX implementation

I am having a problem in with this part of the code, I am creating a very simple game in Java with the implementation of LibGDX.
I have three different game states that are handled in three different classes:
StateCreation
MenuState
PlayState
They all work perfectly except for one thing. The music that I have inserted and that is created in the StateCreation should start over, when the player gets to the gameover and the state changes the mode to start over or go back to the menu. This does not happen and the music overlaps
public class GameBarbo extends ApplicationAdapter {
//impostazioni base dell'applicazione
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static final String TITLE = "Cavaliere";
private GameStateManager gsm = new GameStateManager();
private SpriteBatch batch;
private Music music;
#Override
public void create () {
batch = new SpriteBatch();
music = Gdx.audio.newMusic(Gdx.files.internal("sound.mp3"));
music.setVolume(0.1f);
music.play();
Gdx.gl.glClearColor(1, 0, 0, 1);
gsm.push(new MenuState(gsm));
}
#Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render(batch);
}
#Override
public void dispose() {
super.dispose();
music.dispose();
}
}
You code looks very much like what I found in this guide (though they don't include super.dispose().)
Is it possible that when your game state changes, the dispose() method does not execute? I haven't used libgdx in ages, but it could be the case that dispose() only executes when you exit the program.
I see from this wiki that the Music class has a stop() method. Perhaps that needs to be called directly when the game ends. But IDK if stopping the file and executing a reload for another iteration is asking for a memory leak or not.
Hopefully someone with more experience will answer, but maybe these thoughts will help you figure something out in the meantime.

KeyListener not working properly with MediaPlayer in Java

I have a question about Java related with the classes KeyListener (that is used for the listening of keys) and MediaPlayer (that is used for playing an introductory video). My KeyListener is working good independently (it is able to read the key that is being pressed), but when the video is being played it can't read any key, so I can conclude that KeyListener is not working properly with MediaPlayer.
In my application, when the "Escape" key is pressed during the introductory video it should make an interruption to a thread that has been executed by calling the function thread.interrupt(), so that it aborts the Thread.sleep() function and stops the video.
Here is the code of my application:
/**
* Main class of the application.
*/
public class Main{
// Define the variable for the window of the game.
public static JFrame window;
// Define the variable for the introductory video.
public static MediaPlayer video;
// Define the variable for the key listener.
public static KeyListener keyListener;
// Define the variable for the Runnable interface.
public static Runnable runnable;
// Define the variable for a thread.
public static Thread thread;
/**
* Main function of the application.
*/
public static void main(String[] args){
// Prevent the JavaFX toolkit from closing.
Platform.setImplicitExit(false);
// Create the window of the game.
window = new JFrame();
// Set the title.
window.setTitle("Chip");
// Set the resolution as 1920 x 1280.
window.setSize(1926,1343);
// Set the location as in the middle of the screen.
window.setLocationRelativeTo(null);
// Set the operation when the window closes.
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Disable the maximization and resizable mode.
window.setResizable(false);
// Enable the listening of keys.
enableKeyListener();
// Show the window.
window.setVisible(true);
// Create the Runnable interface.
runnable = new Runnable(){
/**
* Main function of the Runnable interface.
*/
public void run(){
try{
// Show the introductory video.
showVideo();
// Pause the execution of the application for 30 seconds (duration of the introductory video).
Thread.sleep(30000);
}catch (InterruptedException interruptedException){
// Stop the video if an interruption has been occurred.
video.stop();
}finally{
// Set the background image.
String filename = "./media/image/background.jpg";
window.setContentPane(new JLabel(new ImageIcon(filename)));
// Show the window.
window.setVisible(true);
}
}
};
// Create a new thread.
thread = new Thread(runnable);
// Start the execution of the thread.
thread.start();
}
/**
* Enables the listening of keys.
*/
public static void enableKeyListener(){
// Create the key listener.
keyListener = new KeyListener(){
// Set the behavior whenever a key is pressed.
public void keyPressed(KeyEvent keyEvent){
// Check if the "Escape" key is pressed.
if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE){
// Check if the introductory video it is being played.
if (video.getStatus().equals(Status.PLAYING)){
// Make an interruption in the thread that is being executed.
thread.interrupt();
}
}
}
// Set the behavior whenever a key is released.
public void keyReleased(KeyEvent keyEvent){}
// Set the behavior whenever a key is typed.
public void keyTyped(KeyEvent keyEvent){}
};
// Add the key listener to the window of the game.
window.addKeyListener(keyListener);
}
/**
* Shows the introductory video.
*/
public static void showVideo(){
// Create the video panel and the JavaFX panel.
JPanel panelVideo = new JPanel();
JFXPanel panelJavaFX = new JFXPanel();
// Set the size of the video panel as the resolution of the introductory video (1920 x 1080).
panelVideo.setSize(1920,1080);
// Set the location of the video panel as in the middle of the window of the game.
int coordinateX = (window.getWidth() - panelVideo.getWidth() - window.getInsets().left - window.getInsets().right) / 2;
int coordinateY = (window.getHeight() - panelVideo.getHeight() - window.getInsets().top - window.getInsets().bottom) / 2;
panelVideo.setLocation(coordinateX,coordinateY);
// Define the video file.
String filename = "./media/video/introduction.mp4";
video = new MediaPlayer(new Media(new File(filename).toURI().toString()));
// Add the video to the JavaFX panel.
panelJavaFX.setScene(new Scene(new Group(new MediaView(video))));
// Add the JavaFX panel to the video panel.
panelVideo.add(panelJavaFX);
// Add the video panel to the window of the game.
window.getContentPane().setLayout(null);
window.add(panelVideo);
// Play the video.
video.play();
}
}
The correct answer to this question is:
I added the KeyListener to the variable panelJavaFX (JFXPanel) and now it can read when the "Escape" key is pressed while the video is being played.

Game crash if interrupted while Splash Screen is on - LIBGDX

I have a splash screen and a menu screen class that loads all my texture atlases and skins for the menu and processes lots of stuff. If I put the constructor for the menu screen in the SplashScreen constructor or in the create() method of my main game class (MyGame class) then it would pass a lot of time with no splash screen render image on the phone while the whole stuff loads so I have delayed the loading of the menu class to the second frame render (gets called in the SplashScreen render method in the second frame); I simply check if I passed the first frame then I call a method on the main game class that loads the menus.
It all works fine , as expected , but only if the SplashScreen load process is not interrupted. If I get a call or I simple press the HOME button of the mobile and then I re-enter the game by clicking the icon on the home screen I get an AndroidGraphics error : "waiting for pause synchronization took too long: assuming dead lock and killing"; this is just after I can see the splash screen on the screen for about a second;
I tried to dispose the MyGame in the hide() and pause() methods of both the main game class and the splash screen class so it's terminated if I press the HOME button but they never get called.
How can I fix this? It would be annoying to get a call while loading the game and after you finish the call and try to re-enter the game it crashes.
public class MyGame extends Game{
...
public MainMenu menu;
...
#Override
public void create(){
this.screen_type == SCREEN_TYPE.SPLASH;
splashScreen = new SplashScreen();
setScreen(splashScreen);
}
...
#Override
public void pause(){
//never gets called if I press the HOME button in middle of splash screen
if(this.screen_type == SCREEN_TYPE.SPLASH)
{
this.dispose();
}
}
...
public void LoadMenuTimeConsumingConstructor(){
//load all menus and process data
main_menu = new MainMenu();
loaded_menu = true;
}
}
public class SplashScreen implements InputProcessor, Screen{
public MyGame main_game;
...
public SplashScreen(MyGame game){
this.main_game = game;
}
#Override
public void pause(){
//never gets called if I press the HOME button in middle of splash screen
if(main_game.screen_type == SCREEN_TYPE.SPLASH)
{
main_game.dispose();
}
}
#Override
public void hide(){
//never gets called if I press the HOME button in middle of splash screen
if(main_game.screen_type == SCREEN_TYPE.SPLASH)
{
main_game.dispose();
}
}
#Override
public void render(delta float){
...
//wait 1.5 sec
if(TimeUtils.millis() - startTime > 1500){
{
if(main_game.loaded_menu = true){
main_game.setScreen(main_game.main_menu);
}
}
...
if(is_this_second_frame){ // we start loading menus in the second frame so we already have the splash onscreen
main_game.LoadMenuTimeConsumingConstructor();
}
...
}
}
I do not understand your question very well, but if you are not using AssetManger Class, I think this read can help, I hope so.
If this response is not valid or not useful to you, tell me, and delete
wiki LibGDX
https://github.com/libgdx/libgdx/wiki/Managing-your-assets
video on YouTUBE
https://www.youtube.com/watch?v=JXThbQir2gU
other example in GitHub
https://github.com/Matsemann/libgdx-loading-screen
NEW look this:
Proper way to dispose screens in Libgdx
What's the right place to dispose a libgdx Screen
It can help you decide if you need to call dipose, and how, I hope it will help.

Make vlcj screen black java

im making a quiz for a project and when someone wants to answer the question on a video piece, i need to pause the video and make the screen black so other people can't watch more of the video.
Pausing works perfectly, but is there any way how i can make the screen black? I've tried changing it's brightness but that isn't working.
This is what my run/pause/resume functions look like :
/**
* Run the media player
*/
public void run(){
ourMediaPlayer.getMediaPlayer().playMedia(mediaPath);
}
/**
* Pause the media player
*/
public void pause() {
ourMediaPlayer.getMediaPlayer().pause();
}
public void resume() {
ourMediaPlayer.getMediaPlayer().play();
}
Thanks!

How to play flv, mp4, avi format video in JAVA?

I can play .mpg format video with the Java media framework.
But I can not play all format of video.
When I want to play .AVI format video, the video is play but the sound is not come.
How to play other format such as FLV, mp4, avi?
public class MediaPanel extends JPanel
{
public Player mediaPlayer;
public Component video;
public Component controls;
public MediaPanel(URL mediaURl)
{
setLayout(new BorderLayout());
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
try
{
//Player
// this. mediaPlayer.stop();
//this.mediaPlayer.close();
mediaPlayer=Manager.createRealizedPlayer(mediaURl);
//Component
video=mediaPlayer.getVisualComponent();
//Component
controls=mediaPlayer.getControlPanelComponent();
if(video!=null)
add(video,BorderLayout.CENTER);
if(controls!=null)
add(controls,BorderLayout.SOUTH);
mediaPlayer.start();
}
catch(NoPlayerException noPlayerException)
{
System.err.println("No media");
}
catch(CannotRealizeException cannotRealizeException)
{
System.err.println("Could not");
}
catch(IOException iOException)
{
System.err.println("Error");
}
}}
You need to add a Service Provider Interface for the encoding in question. E.G. The JMF provides an SPI for MP3 that can be added to the run-time class-path of an app. to allow it to read MP3s using Java Sound.

Categories

Resources