在应用音频文件之前,先使用AsyncTask在后台线程中下载音频并保存到本地文件系统。然后,将该本地路径作为参数传递给Imgly Video Editor SDK中的音频应用方法。
代码示例:
private class DownloadAudioTask extends AsyncTask
@Override protected String doInBackground(String... params) {
String url = params[0];
String fileName = params[1];
String path = Environment.getExternalStorageDirectory().toString() + "/" + fileName;
try {
URLConnection connection = new URL(url).openConnection();
InputStream inputStream = new BufferedInputStream(connection.getInputStream());
OutputStream outputStream = new FileOutputStream(path);
byte data[] = new byte[1024];
int count;
while ((count = inputStream.read(data)) != -1) {
outputStream.write(data, 0, count);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
@Override protected void onPostExecute(String path) { applyAudioToVideo(path); }
}
private void applyAudioToVideo(String audioPath) { Video newVideo = new Video(videoPath); Filter videoResizer = new VideoSizeFilter(SizeConstraint.LIMIT_1080); newVideo.applyFilter(videoResizer);
Filter audioFilter = new AudioFilter(audioPath);
newVideo.addFilter(audioFilter);
newVideo.save(resultPath, new CompletionCallback() {
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onCompletion(Uri uri) {
// completed
}
});
}
其中,videoPath为要编辑的视频路径,resultPath为导出的视频路径。
上一篇:android视频编辑sdk
下一篇:Android视频并排合并