Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
2.6 kB
1
Indexable
Never
package com.sangdev.miniproject;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    private ProgressBar progressBar;
    private TextView outputTextView;
    private ExecutorService executorService;
    private Handler mainHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar = findViewById(R.id.progressBar);
        outputTextView = findViewById(R.id.outputTextView);
        
        executorService = Executors.newSingleThreadExecutor();
        mainHandler = new Handler(Looper.getMainLooper());

        // Example usage
        Uri videoUri = Uri.parse("content://path/to/video");
        String outputPath = getExternalFilesDir(null) + "/output_video.mp4";
        removeAudioFromVideo(this, videoUri, outputPath);
    }

    private void removeAudioFromVideo(Context context, Uri inputUri, String outputPath) {
        progressBar.setVisibility(View.VISIBLE);
        outputTextView.setVisibility(View.GONE);

        executorService.execute(() -> {
            boolean result;
            try {
                VideoUtils.removeAudioFromVideo(context, inputUri, outputPath);
                result = true;
            } catch (IOException e) {
                e.printStackTrace();
                result = false;
            }

            final boolean finalResult = result;
            mainHandler.post(() -> {
                progressBar.setVisibility(View.GONE);

                if (finalResult) {
                    outputTextView.setText("Output file: " + outputPath);
                    outputTextView.setVisibility(View.VISIBLE);

                    // Play the output video
                    Uri outputUri = Uri.parse(outputPath);
                    Intent intent = new Intent(Intent.ACTION_VIEW, outputUri);
                    intent.setDataAndType(outputUri, "video/mp4");
                    startActivity(intent);
                } else {
                    outputTextView.setText("Failed to remove audio.");
                    outputTextView.setVisibility(View.VISIBLE);
                }
            });
        });
    }
}
Leave a Comment