java - 尝试使用 yovenny/VideoCompress 编写可以压缩小尺寸视频的代码
问题描述
自从我尝试编写一个可以将视频压缩为 WhatsApp 等小尺寸视频文件的活动以来,已经有两天了。我已经尝试了我在互联网上找到的所有解决方案,但这些工作人员中没有一个可能是我目前以错误的方式使用它们我在https://github.com/yovenny/VideoCompress中有一个解决方案,所以我试图使用它仍然会出现一些错误。错误说
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.nanb.alpha-2/base.apk"],nativeLibraryDirectories=[/data/app/com.nanb.alpha-2/lib/arm64, /vendor/lib64, /system/lib64]]] couldn't find "libcompress.so"
如果您有其他解决方案,请与我分享并告诉我如何在我的应用程序中实施该解决方案。代码如下。
public class videocompress extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
return MediaController.getInstance().convertVideo(params[0],params[1]);
}
@Override
protected void onPostExecute(Boolean compressed) {
super.onPostExecute(compressed);
if(compressed){
Log.d("video compress","Compression successfully!");
}
}
}
解决方案
这是一些示例代码:
视频压缩类
class VideoCompressAsyncTask extends AsyncTask<String, String, String> {
Context mContext;
String thumbnail;
public VideoCompressAsyncTask(Context context, String thumbnail){
mContext = context;
this.thumbnail = thumbnail;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Uploading Video...");
pd.show();
}
@Override
protected String doInBackground(String... paths) {
String filePath = null;
try {
filePath = SiliCompressor.with(mContext).compressVideo(paths[0], paths[1]);
} catch (URISyntaxException e) {
e.printStackTrace();
}
return filePath;
}
@Override
protected void onPostExecute(String compressedFilePath) {
super.onPostExecute(compressedFilePath);
UploadMedia(compressedFilePath, thumbnail,"video");
Log.i("Silicompressor", "Path: "+compressedFilePath);
}
}
在onActivityResult
你叫这个类
if(requestCode == SELECT_VIDEO && resultCode == RESULT_OK)
{
Uri uri = data.getData();
String[] projection = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if(cursor!=null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + "/media/videos");
if (f.mkdirs() || f.isDirectory())
//compress and output new video specs
new VideoCompressAsyncTask(this,getThumbnailPathForLocalFile(this,uri)).execute(cursor.getString(column_index), f.getPath());
cursor.close();
}else
Toast.makeText(ChatScreen.this, "No Video Found", Toast.LENGTH_SHORT).show();
}
您也可以通过此方法获取视频缩略图
public static String getThumbnailPathForLocalFile(ChatScreen context, Uri fileUri ) {
long fileId = getFileId(context, fileUri);
Log.e("ID",fileId+"");
MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),
fileId, MediaStore.Video.Thumbnails.MICRO_KIND, null);
Cursor thumbCursor = null;
try {
thumbCursor = context.managedQuery(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + " = " + fileId, null, null);
if (thumbCursor.moveToFirst()) {
String thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
Log.e("ThumbPath",thumbPath);
return thumbPath;
}
} finally {
}
return null;
}