Make vlcj screen black java - 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!

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.

libgdx how to access power button of android device?

I just want to check that the power button is pressed or not in LibGDX and if it is pressed then exit/close the game or something else like change screen.
I used this code:
#Override
public void render(SpriteBatch sb) {
sb.begin();
if(Gdx.input.isKeyPressed(Input.Keys.POWER)){
Gdx.app.exit();
}
sb.end();
}
but this is not working. After I press the button screen turns off and when I turn it back on the game resumes from exactly where I left it.
I wanted to keep the screen on on power button press but I didn't find any solution on that.
Now I at least want to access the button and then do something on button press.
the same code works for volume up/down button.
Code:
#Override
public void render(SpriteBatch sb) {
sb.begin();
if(Gdx.input.isKeyPressed(Input.Keys.VOLUME_UP)){
Gdx.app.exit();
}
sb.end();
}
Please describe if I'm doing anything wrong and what shall I do.
You can do all saves in pause method that is available in Screen and ApplicationListener classes. It will be called when you exit the app or block device.
#Override
public void pause () {
// save here
}

Wrote the whole game in one class MyGdxGame. How do i mae the main menu? libgdx

I have a big problem.I wrote the whole game in same(one) class MyGdxGame extends from the ApplicationAdapter. How do I make the main menu? So that when opening the game, the main menu opens, in which 1 button, when pressed, opens the game itself (MyGdxGame)? Just tell me how to do it (what to create, what to change)
It's better to achieve high cohesion but when you code inside one class, cohesiveness become very low.
Use Screen and Game class to implement more than one screen as in your case like MainScreen, GameScreen, LevelScreen...
But if you don't want to implement that use flags for different screen in your MyGdxGame class.
public enum GameScreen{
MENU_SCREEN, GAME_SCREEN, LEVELSCREEN;
}
In render method of your MyGdxGame
public GameScreen currentScreen=GameScreen.MENU_SCREEN;
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(currentScreen==GameScreen.MENU_SCREEN){
//render objects for Menu Screen
}else if(currentScreen==GameScreen.GAME_SCREEN){
//render objects for Game Screen
}
}
Change value of currentScreen when you want to move on other screen.
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(currentScreen==GameScreen.MENU_SCREEN){
//detect button of MenuScreen by bounds or any other way
if(check in bound){
currentScreen==GameScreen.GAME_SCREEN;
}
}else if(currentScreen==GameScreen.GAME_SCREEN){
// for Game Screen
}
return false;
}
EDIT
Pelocho suggested to use abstract method inside enum.
public enum GameScreen{
MENU_SCREEN{
#Override
public void render(MyGdxGame gdxGame) {
}
}, GAME_SCREEN{
#Override
public void render(MyGdxGame gdxGame) {
}
}, LEVELSCREEN{
#Override
public void render(MyGdxGame gdxGame) {
}
};
public abstract void render(MyGdxGame gdxGame);
}
And inside render method of MyGdxGame
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
currentScreen.render(this);
}
You should do it properly instead of hacking a menu in. If you look at the Screen class it has the about the same methods as the ApplicationAdapter (show, render, resize, dispose, hide, pause, resume). If you would create a new class and have it implement Screen you can start to cut/paste from your main class to this screen class. If you have something in the Create method then either put it in the Show method or in the constructor. You then create another Screen for your main menu, options, credits, etc.
Have you main class MyGdxGame extend from Game instead of ApplicationAdapter delete all methods except the create method. In the create method you can load your assets and call your first screen like this setScreen(new MenuScreen()). For changing screens you have several options.
Pass the Game object allong with the screens so you can do game.setScreen(..)
Have some sort of screen manager with public static methods to change screens.
Gdx.app.getApplicationListener can be casted into game. This way you can change screen from anywhere in your LibGDX app. ((Game)Gdx.app.getApplicationListener).setScreen(myScreen)
If you are feeling scared you will destroy functionality, make a backup of you game in its current state. Abstracting code from big classes to new smaller classes is a important task for each programmer so it will be good practice. If you feel at any point that a certain piece of code should belong to another/new class feel free to do so. There are many ways to do things and keeping your code readable and understandable is your number 1 priority.

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

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.

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.

Categories

Resources