功能简介:
- 录播上传服务提供服务端上传视频生成录播支持,目前仅支持java语言
集成过程:
- 新建java工程
- 复制commons-codec-1.9.jar到工程lib目录下
- 复制commons-logging-1.2.jar到工程lib目录下
- 复制hamcrest-core-1.1.jar到工程lib目录下
- 复制httpclient-4.4.1.jar到工程lib目录下
- 复制httpcore-4.4.1.jar到工程lib目录下
- 复制jdom-1.1.jar到工程lib目录下
- 复制json-20170516.jar到工程lib目录下
- 复制vhalluploadkit_pass-1.0-release.jar到工程lib目录下
调用流程:
一、获取上传实例:VhallUploadKit
util = VhallUploadKit.getInstance();
二、使用在微吼PAAS平台注册应用时分配的APPID和SECRETKEY初始化
util.initData(APP_ID, SECRET_KEY);
三、初始化成功后,就可以上传视频生成录播
util.uploadAndBuildVideo(file, videoName,Callback, PutObjectProgressListener);
API简介:
- 初始化 使用在微吼PAAS平台注册应用时分配的APPID和SECRETKEY初始化,重要!
void initData(String APPID, String SecretKey)
- 是否可用 初始化成功后,VhallUploadKit才会处于可用状态,才能上传录播
boolean isEnable()
- 上传视频并自动生成录播
/**
*
* @param file
* 需要上传的文件
* @param videoName
* 录播名称
* @param callback
* 服务器回调
* @param listener
* 上传过程监听,返回当前上传状态及上传进度
* @return 文件对应OSS路径
*/
String uploadAndBuildVideo(File file, String videoName,Callback callback, ProgressListener listener)
- 停止上传 录播上传服务支持断点续传,上传过程中,本地会生成一个ucp文件,保存当前上传进度,如果上传操作被异常或手动中断,下次上传会自动定位到最后一次的上传位置继续上传。
/**
* 中断上传
*
* @param fileKey
* 上传返回的文件ID
* @return 是否成功
*/
boolean stopUpload(String fileKey)
-取消上传 取消本次上传操作,取消操作会删除本地及服务器上传纪录和服务器上的文件碎片,重新从0开始上传
/**
* 取消上传
*
* @param fileKey
* 上传返回的文件ID
* @return 是否成功
*/
boolean abortUpload(String fileKey)
代码演示
public class SampleWithWindow extends JFrame {
private static final long serialVersionUID = 560684569647135515L;
// 编辑部分
// TODO APPID
public static final String APP_ID = "d0d7b081";// 微吼APPKEY
public static final String SECRET_KEY = "98294813a6d553b2a2dadd8a5375e21f";// 微吼SECRETKEY
public static final String callbackurl = "http://t.e.vhall.com/api/callback";
public static final String videoName = "";
// demo
static Callback callback;
static JLabel fileLabel;
static JLabel tipsLabel;
static JProgressBar bar;
static VhallUploadKit util;
static File file;
static String fileKey = "";
public SampleWithWindow() {
util = VhallUploadKit.getInstance();
callback = new Callback();
callback.setCallbackUrl(callbackurl);
callback.setCallbackBody("{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}");
callback.setCalbackBodyType(CalbackBodyType.JSON);
}
public static void main(String[] args) {
SampleWithWindow window = new SampleWithWindow();
window.setTitle("vhall upload kit");
window.setSize(600, 300);
window.setResizable(false);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(initUI());
window.setVisible(true);
util.initData(APP_ID, SECRET_KEY);
if (util.isEnable()) {
tipsLabel.setText("初始化成功!");
} else {
tipsLabel.setText("初始化失败!");
}
}
private static JPanel initUI() {
final JPanel contentPanel = new JPanel();
contentPanel.setLayout(new GridLayout(10, 1));
fileLabel = new JLabel();
fileLabel.setText("请选择文件!");
fileLabel.setHorizontalAlignment(SwingConstants.CENTER);
bar = new JProgressBar();
bar.setMaximum(100);
bar.setMinimum(0);
bar.setValue(0);
bar.setStringPainted(true);
JButton selectBtn = new JButton("选择文件");
selectBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectFile(contentPanel);
}
});
JButton uploadBtn = new JButton("上传文件");
uploadBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startUpload();
}
});
JButton stopBtn = new JButton("停止上传");
stopBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopUpload();
}
});
JButton cancelBtn = new JButton("取消上传");
cancelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
abortUpload();
}
});
tipsLabel = new JLabel();
tipsLabel.setText("初始化,请稍等...");
tipsLabel.setHorizontalAlignment(SwingConstants.CENTER);
contentPanel.add(fileLabel);
contentPanel.add(new JLabel());
contentPanel.add(bar);
contentPanel.add(selectBtn);
contentPanel.add(uploadBtn);
contentPanel.add(stopBtn);
contentPanel.add(cancelBtn);
contentPanel.add(new JLabel());
contentPanel.add(tipsLabel);
return contentPanel;
}
private static void startUpload() {
if (file == null) {
tipsLabel.setText("请先选择文件...");
return;
}
String key = util.uploadAndBuildVideo(file, videoName,null, new PutObjectProgressListener(file.length()));
if (!TextUtils.isEmpty(key))
fileKey = key;
}
private static void stopUpload() {
if (file == null) {
tipsLabel.setText("请先选择文件...");
return;
}
if (TextUtils.isEmpty(fileKey)) {
tipsLabel.setText("请先上传...");
return;
}
if (util.stopUpload(fileKey))
tipsLabel.setText("上传已停止...");
}
private static void abortUpload() {
if (file == null) {
tipsLabel.setText("请先选择文件...");
return;
}
if (TextUtils.isEmpty(fileKey)) {
tipsLabel.setText("请先上传...");
return;
}
if (util.abortUpload(fileKey)) {
tipsLabel.setText("上传已取消...");
fileKey = "";
}
}
/**
* 获取上传进度回调
*/
static class PutObjectProgressListener implements ProgressListener {
private long bytesWritten = 0;
private long totalBytes = -1;
private boolean succeed = false;
private long fileLength = 0;
public PutObjectProgressListener(long fileLength) {
super();
this.fileLength = fileLength;
}
@Override
public void progressChanged(ProgressEvent progressEvent) {
long bytes = progressEvent.getBytes();
ProgressEventType eventType = progressEvent.getEventType();
switch (eventType) {
case TRANSFER_STARTED_EVENT:
tipsLabel.setText("开始上传...");
break;
case REQUEST_CONTENT_LENGTH_EVENT:
this.totalBytes = bytes;
this.bytesWritten = fileLength - totalBytes;
break;
case REQUEST_BYTE_TRANSFER_EVENT:
this.bytesWritten += bytes;
if (this.totalBytes != -1) {
int percent = (int) (this.bytesWritten * 100.0 / this.fileLength);
bar.setValue(percent);
System.out.println(bytes + " bytes have been written at this time, upload progress: " + percent
+ "%(" + this.bytesWritten + "/" + this.fileLength + ")");
} else {
System.out.println(bytes + " bytes have been written at this time, upload ratio: unknown" + "("
+ this.bytesWritten + "/...)");
}
break;
case TRANSFER_COMPLETED_EVENT:
this.succeed = true;
// tipsLabel.setText("上传成功!");
fileKey = "";
break;
case TRANSFER_FAILED_EVENT:
tipsLabel.setText("上传失败!");
break;
default:
break;
}
}
public boolean isSucceed() {
return succeed;
}
@Override
public void webinarCreate(String fileKey, String webinarId, String recordId) {
tipsLabel.setText("上传成功,文件ID:" + fileKey + " 生成录播成功,recordID:" + recordId);
}
}
private static void selectFile(Component parent) {
int result = 0;
JFileChooser fileChooser = new JFileChooser();
FileSystemView fsv = FileSystemView.getFileSystemView();
fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
fileChooser.setDialogTitle("请选择要上传的文件...");
fileChooser.setApproveButtonText("确定");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
result = fileChooser.showOpenDialog(parent);
if (JFileChooser.APPROVE_OPTION == result) {
file = new File(fileChooser.getSelectedFile().getPath());
fileLabel.setText("待上传文件:" + file.getAbsolutePath());
// 停止正在上传的文件
if (!TextUtils.isEmpty(fileKey))
stopUpload();
fileKey = "";
}
}
}