简体   繁体   English

无法在日食中关闭小程序

[英]Cannot close applet in eclipse

I have an applet that runs VLCJ (http://code.google.com/p/vlcj/) - basically embedding a VLC player in an applet. 我有一个运行VLCJ的小程序(http://code.google.com/p/vlcj/)-基本上是将VLC播放器嵌入到小程序中。 When running in eclipse, it runs well but I cannot close the debugging applet-window or terminate it somehow. 在eclipse中运行时,它运行良好,但是我无法关闭调试小程序窗口或以某种方式终止它。 I wonder, why is this? 我不知道为什么呢? Is there anything in the code that prevents it from stopping debugging? 代码中是否有阻止它停止调试的内容? I have to restart eclipse in order to make it quit. 我必须重新启动Eclipse才能使其退出。 Im quite sure you dont need to add destroy() to enable closing of the debugging window. 我非常确定您不需要添加destroy()即可关闭调试窗口。

Thanks 谢谢

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Frame;
import javax.swing.JApplet;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;


public class Main extends JApplet {

    /**
     * @param args
     */
    /* entry point */
    public void init() {
        String file = "110825-155446.wmv";    // only 2-3 seconds clip for minimum storage      
        runVideo(file);
    }

    /* runs the video file */
    public void runVideo(String file) {

        setSize(400,300);
        setLayout(new BorderLayout()); 
        Canvas vs = new Canvas();
        add(vs,BorderLayout.CENTER);
        setVisible(true);

        MediaPlayerFactory factory = new MediaPlayerFactory();

        EmbeddedMediaPlayer mediaPlayer = factory.newEmbeddedMediaPlayer();
        mediaPlayer.setVideoSurface(factory.newVideoSurface(vs));

        mediaPlayer.playMedia(file);
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }


}

The reason is at this code snippet part: 原因是在此代码段部分:

try {
    Thread.currentThread().join();
} catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

It blocks the application from closing since it doesn't want to return to the system. 由于它不想返回系统,因此阻止了应用程序的关闭。 Thread.join() makes the current thread waits for another thread to complete, basically it waits forever. Thread.join()使当前线程等待另一个线程完成,基本上它将永远等待。

To improve it, you can do like this (as in http://code.google.com/p/vlcj/wiki/MinimalMp3Player ): 要改善它,您可以这样做(如http://code.google.com/p/vlcj/wiki/MinimalMp3Player所示 ):

mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
  public void finished(MediaPlayer mediaPlayer) {
    System.exit(0);
  }
  public void error(MediaPlayer mediaPlayer) {
    System.exit(1);
  }
});
mediaPlayer.playMedia(args[0]);
Thread.currentThread().join();

However, we cannot use System.exit() method in a Java servlet code (or even applet code) as it can shutdown the JVM used by the code which may be needed by other Java application/servlet/applet codes. 但是,我们不能在Java servlet代码(甚至applet代码)中使用System.exit()方法,因为它可以关闭其他Java应用程序/ servlet / applet代码可能需要的代码所使用的JVM。 See Alternatives to System.exit(1) , Calling System.exit() in Servlet's destroy() method 请参阅System.exit(1)的替代 方法,在Servlet的destroy()方法中调用System.exit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM