Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
3.0 kB
1
Indexable
Never
package com.example.mergevideos;

import android.Manifest;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.VideoView;
import android.widget.MediaController;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_PERMISSIONS = 1;
    private static final String TAG = "MainActivity";

    private VideoView videoView;

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

        videoView = findViewById(R.id.video_view);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSIONS);
        } else {
            setupMergeButton();
        }
    }

    private void setupMergeButton() {
        Button mergeButton = findViewById(R.id.merge_button);
        mergeButton.setOnClickListener(view -> {
            try {
                String video1Path = "/path/to/first/video.mp4";
                String video2Path = "/path/to/second/video.mp4";
                String outputPath = "/path/to/output/merged_video.mp4";

                VideoMerger.mergeVideos(video1Path, video2Path, outputPath);
                
                playMergedVideo(outputPath);
            } catch (IOException e) {
                Log.e(TAG, "Error merging videos: ", e);
            }
        });
    }

    private void playMergedVideo(String videoPath) {
        Uri videoUri = Uri.parse(videoPath);
        videoView.setVideoURI(videoUri);
        MediaController mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);
        mediaController.setAnchorView(videoView);
        videoView.start();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSIONS) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                setupMergeButton();
            } else {
                // Permission was denied
                Log.e(TAG, "Permissions denied.");
            }
        }
    }
}
Leave a Comment