我试图制作一个应用程序来在android studio中剪切视频,然后将其分享到某个应用程序。但是,即使在切割过程完成之前,分享也似乎已经发生了
我的代码:
vidUris.add(Uri.fromFile(new File(dest.getAbsolutePath())));
String[] complexCommand = {"-i", yourRealPath, "-ss", "" + startMs, "-t", ""+leng , dest.getAbsolutePath()};
execFFmpegBinary(complexCommand);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, vidUris);
shareIntent.setType("video/*");
startActivity(shareIntent);
答案 0 :(得分:1)
请检查execFFmpegBinary是否是异步方法。
答案 1 :(得分:0)
所以你需要一个回调函数,一旦剪切完成就会调用它。这样您就可以开始共享意图了。
要实现此行为,您可以考虑使用这样的界面。
public interface CuttingCompleted {
void onCuttingCompleted(String[] vidUris);
}
现在使用AsyncTask
在后台线程中进行切割,并在完成后将结果传递给回调函数,以进一步执行代码流。
public class CuttingVideoAsyncTask extends AsyncTask<Void, Void, String[]> {
private final Context mContext;
public CuttingCompleted mCuttingCompleted;
CuttingVideoAsyncTask(Context context, CuttingCompleted listener) {
// Pass extra parameters as you need for cutting the video
this.mContext = context;
this.mCuttingCompleted = listener;
}
@Override
protected String[] doInBackground(Void... params) {
// This is just an example showing here to run the process of cutting.
String[] complexCommand = {"-i", yourRealPath, "-ss", "" + startMs, "-t", ""+leng , dest.getAbsolutePath()};
execFFmpegBinary(complexCommand);
return complexCommand;
}
@Override
protected void onPostExecute(String[] vidUris) {
// Pass the result to the calling Activity
mCuttingCompleted.onCuttingCompleted(vidUris);
}
@Override
protected void onCancelled() {
mCuttingCompleted.onCuttingCompleted(null);
}
}
现在,从Activity
开始,您需要实现界面,以便在切割过程完全完成时启动共享意图。
public class YourActivity extends Activity implements CuttingCompleted {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ... Other code
new CuttingVideoAsyncTask(this, this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public void onCuttingCompleted(String[] vidUris) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, vidUris);
shareIntent.setType("video/*");
startActivity(shareIntent);
}
}