2017-06-23 34 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

相關問題