2017-06-23 35 views
0

我试图让一个应用程序在android studio中剪切视频,然后将它分享给一些应用程序。但是,共享似乎发生在完成切割过程甚至在如何使上面的代码执行id后完成意向运行

我的代码:

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); 

回答

1

请检查execFFmpegBinary是异步方法。

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); 
    } 
} 
+0

尝试使用AsyncTask,但活动在切割被调用后立即关闭。给D/AndroidRuntime:关闭虚拟机I /进程:发送信号。 PID:2346 SIG:9 – abhinavtk

相关问题