这是我的代码, 任何人都可以告诉我如何停止播放媒体文件, 在这个EmbeddedMediaPlayerComponent中是用来播放媒体文件的类.... 我的问题如何停止媒体文件...
public class MediaPlayer extends JPanel {
//Declares our media player component
private EmbeddedMediaPlayerComponent[] mediaplayer;
private String[] mediapath = {""};
private final String vlcpath = "C:\\Program Files (x86)\\VideoLAN\\VLC";
private JPanel video_pnl, control_pnl;
private JButton[] play_btn, stop_btn;
private int but = 0;
public MediaPlayer(String mediapath[]) {
this.mediapath = mediapath;
play_btn = new JButton[1];
stop_btn = new JButton[1];
mediaplayer = new EmbeddedMediaPlayerComponent[1];
int increment = 0;
while (increment < mediapath.length) {
video_pnl = new JPanel();
video_pnl.setLayout(new BorderLayout());
control_pnl = new JPanel();
control_pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
for (int i = 0; i < 1; i++) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcpath);
mediaplayer[i] = new EmbeddedMediaPlayerComponent();
play_btn[i] = new JButton("play");
stop_btn[i] = new JButton("stop");
video_pnl.add(mediaplayer[i], BorderLayout.CENTER);
control_pnl.add(play_btn[i]);
control_pnl.add(stop_btn[i]);
video_pnl.add(control_pnl, BorderLayout.SOUTH);
Handler handler = new Handler();
play_btn[i].addActionListener(handler);
}
add(video_pnl);
increment++;
}
}
private class Handler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == play_btn){
play();
}
}
}
public void play() {
for (int i = 0; i < mediapath.length; i++) {
mediaplayer[i].getMediaPlayer().playMedia(mediapath[i]);
}
}
public static void main(String[] args) {
//Declare and initialize local variables
String[] mediaPath = {"C:\\\\Users\\\\goldAnthony\\\\Desktop\\\\Videos\\\\Whistle.mp4", "C:\\\\Users\\\\goldAnthony\\\\Desktop\\\\Videos\\\\Beyonce_Hello.mp4",
"C:\\Users\\goldAnthony\\Desktop\\Videos\\HansRosling_2012S_480p.mp4","C:\\Users\\goldAnthony\\Desktop\\Videos\\oow2010_2.mp4",
"C:\\Users\\goldAnthony\\Desktop\\Videos\\The_Economic_Environment.mp4"};
//creates instances of the VlcPlayer object, pass the mediaPath and invokes the method "run"
MediaPlayer mediaplayer = new MediaPlayer(mediaPath);
JFrame ourframe = new JFrame();
ourframe.setContentPane(mediaplayer);
ourframe.setLayout(new GridLayout(5, 1));
ourframe.setSize(300, 560);
ourframe.setVisible(true);
mediaplayer.play();
ourframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:0)
停止播放
stopButton = new JButton();
stopButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("icons/control_stop_blue.png")));
stopButton.setToolTipText("Stop");
.......
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mediaPlayer.stop();
}
});
从PlayerControlsPanel.java样本测试类中提取。