package com.base.filerecoveryrecyclebin.help;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.io.File;

import android.os.Build;
import android.util.Log;

import androidx.core.content.FileProvider;

public class VideoPlayerUtil {

    /**
     * 打开外部视频播放器并播放指定路径的MP4文件。
     *
     * @param context   应用上下文
     * @param videoPath MP4文件的本地路径
     */
    public static void playVideo(Context context, String videoPath) {
        File videoFile = new File(videoPath);
        Log.d("glc", videoPath);
        if (!videoFile.exists()) {
            throw new IllegalArgumentException("Video file does not exist: " + videoPath);
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri videoUri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            videoUri = FileProvider.getUriForFile(context, "com.base.filerecoveryrecyclebin.provider", videoFile);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            videoUri = Uri.fromFile(videoFile);
        }
        intent.setDataAndType(videoUri, "video/mp4");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            // 处理异常，例如提示用户或记录日志
        }
    }

    /**
     * 打开外部视频播放器并播放指定路径的MP4文件。
     *
     * @param context   应用上下文
     * @param audioPath MP4文件的本地路径
     */
    public static void playAudio(Context context, String audioPath) {
        File audioFile = new File(audioPath);
        Log.d("glc", audioPath);
        if (!audioFile.exists()) {
            throw new IllegalArgumentException("Video file does not exist: " + audioPath);
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri audioUri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            audioUri = FileProvider.getUriForFile(context, "com.base.filerecoveryrecyclebin.provider", audioFile);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            audioUri = Uri.fromFile(audioFile);
        }
        intent.setDataAndType(audioUri, "audio/mp3");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            // 处理异常，例如提示用户或记录日志
        }
    }
}