用代码java 实现视频断点播放
时间: 2023-12-23 21:04:17 浏览: 132
当视频被暂停时,保存当前的播放进度。当用户需要恢复播放时,可以根据保存的播放进度来继续播放视频。
以下是用Java实现视频断点播放的代码示例:
```java
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;
import javax.media.protocol.*;
import javax.media.format.*;
import javax.media.protocol.DataSource;
import java.net.*;
public class VideoPlayer extends JFrame implements ControllerListener {
private MediaLocator mediaLocator = null;
private Player player = null;
private Component visualComponent = null;
private Component controlComponent = null;
private Container contentPane = null;
private int pauseLocation;
public VideoPlayer(String mediaURL) {
super("Video Player");
try {
mediaLocator = new MediaLocator(mediaURL);
player = Manager.createPlayer(mediaLocator);
player.addControllerListener(this);
player.realize();
while (player.getState() != player.Realized) {
Thread.sleep(10);
}
visualComponent = player.getVisualComponent();
if (visualComponent != null) {
contentPane = getContentPane();
contentPane.add(visualComponent, BorderLayout.CENTER);
}
controlComponent = player.getControlPanelComponent();
if (controlComponent != null) {
contentPane.add(controlComponent, BorderLayout.SOUTH);
}
setPreferredSize(new Dimension(640, 480));
pack();
setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void controllerUpdate(ControllerEvent event) {
if (event instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(pauseLocation));
player.stop();
}
}
public void pause() {
player.stop();
pauseLocation = (int)player.getMediaTime().getSeconds();
}
public void play() {
player.setMediaTime(new Time(pauseLocation));
player.start();
}
public static void main(String[] args) {
VideoPlayer player = new VideoPlayer(args[0]);
JFrame frame = new JFrame("Video Player");
JPanel panel = new JPanel();
JButton playButton = new JButton("Play");
JButton pauseButton = new JButton("Pause");
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
player.play();
}
});
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
player.pause();
}
});
panel.add(playButton);
panel.add(pauseButton);
frame.getContentPane().add(player);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
}
```
阅读全文