javaFX: Implementation of conference multimedia switching system (3)

Key class (MeetingShow):

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 */
package ren.kun.meetingshow;

/**
 *
 * @author 65340
 */
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

/**
 *
 * @author 65340
 * General idea: Convert each page in the pptx file into a picture through init(), and then use the JavaFX component (ImageView) to display the picture full screen;
 * By listening to keyboard key events, switch programs (ie: pictures and music)
 */
public class MeetingShow extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        launch(args);
    }
    FileInputStream fis;
    Image img;
    ImageView imgView = new ImageView();
    String baseDir = "";
    String baseSoundDir = "C:/meetingShowItems/";
    String currentFile = "";
    Media m = null;
    MediaPlayer player = null;
    int pptxIndex = 0;
    boolean isPlay = false;

    
    @Override
    public void init() {
        
        String baseUrl = "C:\meetingShowItems\";
        String fileName;
        for (int i = 0; i < 10; i + + ) {
            //Only support pptx files
            fileName = i + ".pptx";
            try {
                //The following function is responsible for converting each page in the pptx file into a picture (jpg)
                PPTUtil.PPTtoImage(baseUrl + i + "\", fileName);
            } catch (Exception ex) {
                Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void start(Stage stage) {
        
        //The following 4 statements are responsible for obtaining the screen size of the current computer, which is used for full screen display
        Toolkit toolkit = Toolkit. getDefaultToolkit();
        Dimension screenSize = toolkit. getScreenSize();
        int width = screenSize. width;
        int height = screenSize. height;

        try {
            baseDir = "C:\meetingShowItems\0\output\";
            fis = new FileInputStream("C:\meetingShowItems\0\output\ToImage-0.jpg");
            //Set the title of the current stage
            stage.setTitle("meetingShowSystem");
            //Set the full screen display content
            stage.setFullScreen(true);
            //Remove the "Press ESC key to cancel full screen" that pops up when displaying in full screen
            stage.setFullScreenExitHint("");
            //Set the key combination "ctrl + c"
            KeyCombination ctrl_c = new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_DOWN);
            //Set "ctrl + c" pressed simultaneously to exit full screen
            stage.setFullScreenExitKeyCombination(ctrl_c);

            img = new Image(fis);
            imgView. setImage(img);
            imgView.setFitHeight(height);
            imgView.setFitWidth(width);
            
            //Set the buttons (0-9, <-, ->), and the display content (jpg picture) of imgView will change accordingly.
            //Set to press the space bar to play or pause background music
            imgView.setOnKeyPressed((var e) -> {

                //Press <- to switch backwards to small programs in the current program (for a pptx file with multiple pages)
                if (e.getCode().toString().equals("LEFT")) {

                    try {
                        if (pptxIndex > 0) {
                            pptxIndex--;
                        }
                        currentFile = "ToImage-" + pptxIndex + ".jpg";
                        fis = new FileInputStream(baseDir + currentFile);
                        img = new Image(fis);
                        imgView. setImage(img);
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        System.out.println("Error browsing ppt to the left (back)!");
                        pptxIndex = 0;
                        if (player != null) {
                            player. stop();
                            player = null;
                        }
                        isPlay = false;
                    }
                }
                
                //Press -> to switch forward to the small program in the current program (for a pptx file with multiple pages)
                if (e.getCode().toString().equals("RIGHT")) {

                    try {
                        if ((pptxIndex + 1) < PPTUtil.countFileNum(baseDir, "jpg")) {
                            pptxIndex++;
                        }
                        currentFile = "ToImage-" + pptxIndex + ".jpg";
                        fis = new FileInputStream(baseDir + currentFile);
                        img = new Image(fis);
                        imgView. setImage(img);
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        System.out.println("Error browsing ppt to the right (forward)!");
                        pptxIndex = 0;
                        if (player != null) {
                            player. stop();
                            player = null;
                        }
                        isPlay = false;
                    }
                }
                //Press space to pause or play music
                if (e.getCode().toString().equals("SPACE")) {

                    if (player != null & amp; & amp; isPlay == true) {
                        player. pause();
                        isPlay = false;
                    } else if (player != null & amp; & amp; isPlay == false) {
                        player.play();
                        isPlay = true;
                    } else {
                        System.out.println("There is currently no playable audio!");
                        isPlay = false;
                    }
                }

                //Press the 0 key on the small keyboard to switch to program 0. If there is background music in the current program, the music will be played at the same time.
                if (e.getCode().toString().equals("NUMPAD0")) {

                    try {
                        baseDir = "C:\meetingShowItems\0\output\";
                        fis = new FileInputStream("C:\meetingShowItems\0\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView. setImage(img);
                        pptxIndex = 0;
                        if (player != null) {
                            player. stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 0 + "/" + "0.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 0 + "/" + "0.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 0 + "/" + "0.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 0 + "/" + "0.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("Program No. 0 has no audio to play");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player.stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("No. 0 program failed to play!");
                    }
                }
                
                //Press keypad 1 to switch to program 1, if there is background music in the current program, play music at the same time
                if (e.getCode().toString().equals("NUMPAD1")) {

                    try {
                        baseDir = "C:\meetingShowItems\1\output\";
                        fis = new FileInputStream("C:\meetingShowItems\1\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView.setImage(img);

                        pptxIndex = 0;
                        if (player != null) {
                            player.stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 1 + "/" + "1.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 1 + "/" + "1.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 1 + "/" + "1.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 1 + "/" + "1.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("Program 1 has no audio to play");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player. stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("No. 1 program failed to play!");
                    }
                }
                
                // Press key 2 on the small keyboard to switch to program 2, if there is background music in the current program, play music at the same time
                if (e.getCode().toString().equals("NUMPAD2")) {

                    try {
                        baseDir = "C:\meetingShowItems\2\output\";
                        fis = new FileInputStream("C:\meetingShowItems\2\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView. setImage(img);
                        pptxIndex = 0;

                        if (player != null) {
                            player. stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 2 + "/" + "2.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 2 + "/" + "2.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 2 + "/" + "2.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 2 + "/" + "2.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("Program No. 2 has no audio to play");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player. stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("Program No. 2 failed to play!");
                    }
                }
                
                //Press key 3 on the small keyboard to switch to program 3. If there is background music in the current program, the music will be played at the same time.
                if (e.getCode().toString().equals("NUMPAD3")) {
                    try {
                        baseDir = "C:\meetingShowItems\3\output\";
                        fis = new FileInputStream("C:\meetingShowItems\3\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView. setImage(img);

                        pptxIndex = 0;
                        if (player != null) {
                            player. stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 3 + "/" + "3.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 3 + "/" + "3.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 3 + "/" + "3.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 3 + "/" + "3.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("No audio for program No. 3 needs to be played");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player. stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("Program No. 3 failed to play!");
                    }
                }

                //Press key 4 on the small keyboard to switch to program 4. If there is background music in the current program, the music will be played at the same time.
                if (e.getCode().toString().equals("NUMPAD4")) {
                    try {
                        baseDir = "C:\meetingShowItems\4\output\";
                        fis = new FileInputStream("C:\meetingShowItems\4\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView.setImage(img);
                        pptxIndex = 0;

                        if (player != null) {
                            player.stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 4 + "/" + "4.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 4 + "/" + "4.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 4 + "/" + "4.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 4 + "/" + "4.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("Program No. 4 has no audio to play");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player.stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("Program No. 4 failed to play!");
                    }
                }
                
                //Press key 5 on the small keyboard to switch to program 5, if there is background music in the current program, play music at the same time
                if (e.getCode().toString().equals("NUMPAD5")) {
                    try {
                        baseDir = "C:\meetingShowItems\5\output\";
                        fis = new FileInputStream("C:\meetingShowItems\5\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView. setImage(img);
                        pptxIndex = 0;

                        if (player != null) {
                            player. stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 5 + "/" + "5.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 5 + "/" + "5.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 5 + "/" + "5.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 5 + "/" + "5.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("Program 5 has no audio to play");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player. stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("No. 5 program failed to play!");
                    }
                }
                
                //Press key 6 on the small keyboard to switch to program 6, if there is background music in the current program, play music at the same time
                if (e.getCode().toString().equals("NUMPAD6")) {
                    try {
                        baseDir = "C:\meetingShowItems\6\output\";
                        fis = new FileInputStream("C:\meetingShowItems\6\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView.setImage(img);
                        pptxIndex = 0;

                        if (player != null) {
                            player.stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 6 + "/" + "6.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 6 + "/" + "6.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 6 + "/" + "6.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 6 + "/" + "6.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("No audio for program No. 6 needs to be played");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player.stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("Program No. 6 failed to play!");
                    }
                }
                
                //Press key 7 on the small keyboard to switch to program 7. If there is background music in the current program, the music will be played at the same time.
                if (e.getCode().toString().equals("NUMPAD7")) {
                    try {
                        baseDir = "C:\meetingShowItems\7\output\";
                        fis = new FileInputStream("C:\meetingShowItems\7\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView.setImage(img);
                        pptxIndex = 0;

                        if (player != null) {
                            player.stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 7 + "/" + "7.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 7 + "/" + "7.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 7 + "/" + "7.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 7 + "/" + "7.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("No audio for program No. 7 needs to be played");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                   // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player.stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("No. 7 program failed to play!");
                    }
                }
                
                // Press key 8 on the small keyboard to switch to program 8, if there is background music in the current program, play music at the same time
                if (e.getCode().toString().equals("NUMPAD8")) {
                    try {
                        baseDir = "C:\meetingShowItems\8\output\";
                        fis = new FileInputStream("C:\meetingShowItems\8\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView.setImage(img);
                        pptxIndex = 0;

                        if (player != null) {
                            player.stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 8 + "/" + "8.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 8 + "/" + "8.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 8 + "/" + "8.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 8 + "/" + "8.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("No. 8 program has no audio to play");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player.stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("No. 8 program failed to play!");
                    }
                }
                
                //Press the keypad 9 to switch to program 9, if there is background music in the current program, play music at the same time
                if (e.getCode().toString().equals("NUMPAD9")) {
                    try {
                        baseDir = "C:\meetingShowItems\9\output\";
                        fis = new FileInputStream("C:\meetingShowItems\9\output\ToImage-0.jpg");
                        img = new Image(fis);
                        imgView.setImage(img);
                        pptxIndex = 0;

                        if (player != null) {
                            player.stop();
                            player = null;
                            isPlay = false;
                        }

                        if (new File(baseSoundDir + 9 + "/" + "9.mp3").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 9 + "/" + "9.mp3");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else if (new File(baseSoundDir + 9 + "/" + "9.wav").exists() & amp; & amp; player == null) {
                            m = new Media("file:///" + baseSoundDir + 9 + "/" + "9.wav");
                            player = new MediaPlayer(m);
                            player.play();
                            isPlay = true;
                        } else {
                            System.out.println("Number 9 has no audio to play");
                            isPlay = false;
                        }
                    } catch (FileNotFoundException ex) {
                    // Logger.getLogger(MeetingShow.class.getName()).log(Level.SEVERE, null, ex);
                        pptxIndex = 0;
                        if (player != null) {
                            player.stop();
                            player = null;
                        }
                        isPlay = false;
                        System.out.println("No. 9 program failed to play!");
                    }
                }
            });

            StackPane root = new StackPane();
            root.getChildren().add(imgView);
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();

            //In order to monitor keyboard key events, the following statement is critical
            imgView.requestFocus();
        } catch (FileNotFoundException e) {
            pptxIndex = 0;
            if (player != null) {
                player.stop();
                player = null;
            }
            isPlay = false;
            System.out.println("No monogram page!");
        }

    }

}