dVader
dVader

Reputation: 15

How to make Intent run after the above code execution id completed

I was trying to make an application to cut videos in android studio and later share it to some app. But the sharing seems to take place even before the cutting process is completed

My code:

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

Upvotes: 0

Views: 223

Answers (2)

Reaz Murshed
Reaz Murshed

Reputation: 24211

So you need a callback function which will be called once the cutting is finished. So that you can start the sharing intent.

To achieve this behaviour, you might consider having an interface like this.

public interface CuttingCompleted {
    void onCuttingCompleted(String[] vidUris); 
}

Now take a AsyncTask to do the cutting in a background thread and when it finishes pass the result to the callback function for further execution of your code flow.

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

Now from your Activity you need to implement the interface so that your sharing intent starts when the cutting process is fully completed.

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

Upvotes: 1

Jinsheng.Yu
Jinsheng.Yu

Reputation: 21

Please check if execFFmpegBinary is an asynchronous method.

Upvotes: 2

Related Questions