Unity录屏插件(Cross Platform Replay Kit)

作者:追风剑情 发布于:2022-12-5 17:05 分类:Unity3d

支持 Android 和 IOS 录屏,并保存到相册中。

官方示例场景:Plugins/VoxelBusters/ReplayKit/Examples/Scenes/ReplayKitDemo.unity

亲测成功!
Redmi Note 11T Pro

导入SDK,打包报错 Failed to update Android SDK package list. See the Console for details.
解决方法:
1、【Windows】-> Preferences -> External Tools -> 取消所有 xxxxx packages 的勾选
2、重启 Unity
3、将刚才取消的勾选重新勾上
4、点击 Play,强制刷新
5、重新打包
1111.png

打包报错 This project uses AndroidX dependencies, but the 'android.useAndroidX' property is not enabled. Set this property to true in the gradle.properties file and retry.
解决方法:在gradleTemplate.properties文件中增加以下两句
android.useAndroidX=true
android.enableJetifier=true


using UnityEngine;
using VoxelBusters.CoreLibrary;
using VoxelBusters.ReplayKit;
/// <summary>
/// Cross Platform Replay Kit Screen Recording
/// </summary>
public class ReplayKitSDK : MonoBehaviour
{
    #region Unity Life Cycle
    private void OnEnable()
    {
        Debug.Log("Registered for ReplayKit Callbacks");
        ReplayKitManager.DidInitialise += DidInitialiseEvent;
        ReplayKitManager.DidRecordingStateChange += DidRecordingStateChangeEvent;
    }

    private void OnDisable()
    {
        ReplayKitManager.DidInitialise -= DidInitialiseEvent;
        ReplayKitManager.DidRecordingStateChange -= DidRecordingStateChangeEvent;
    }
    #endregion

    #region Initialise
    public void Initialise()
    {
        //初始化SDK
        ReplayKitManager.Initialise();
    }
    #endregion

    #region Query
    public void IsRecordingAPIAvailable()
    {
        //是否支持录制
        bool isAvailable = ReplayKitManager.IsRecordingAPIAvailable();
        Debug.Log($"Recording API Available: {isAvailable}");
    }

    public void IsRecording()
    {
        //是否正在录制...
        bool isRecording = ReplayKitManager.IsRecording();
        Debug.Log($"Is currently recording: {isRecording}");
    }

    public void IsPreviewAvailable()
    {
        //是否已保存且可播放录制的视频
        bool isPreviewAvailable = ReplayKitManager.IsPreviewAvailable();
        Debug.Log($"Is preview available: {isPreviewAvailable}");
    }

    public void GetPreviewFilePath()
    {
        //获取视频地址
        string filePath = ReplayKitManager.GetPreviewFilePath();
        Debug.Log($"Recorded video preview file path: {filePath}");
    }
    #endregion

    #region Utilities
    public void SavePreview() //Saves preview to gallery
    {
        if (ReplayKitManager.IsPreviewAvailable())
        {
            //保存到相册
            ReplayKitManager.SavePreview((result, error) =>
            {
                if (error == null)
                {
                    Debug.Log($"Saved preview to gallery.");
                }
                else
                {
                    Debug.Log($"Saved preview to gallery failed with error: {error}");
                }
            });
        }
        else
        {
            Debug.Log("Preview recording not available. If you have already recoded, wait for ReplayKitRecordingState.Available status event and try saving again.");
        }
    }

    public void SharePreview()
    {
        if (ReplayKitManager.IsPreviewAvailable())
        {
            //调用系统分享接口
            ReplayKitManager.SharePreview();
            Debug.Log("Shared video preview");
        }
    }
    #endregion

    #region Recording
    public void SetMicrophoneStatus()
    {
        //true: 录制麦克风音频
        ReplayKitManager.SetMicrophoneStatus(true);
    }

    public void PrepareRecording()
    {
        //准备录制,调用此接口申请所需权限
        ReplayKitManager.PrepareRecording((result, error) =>
        {
            if (error == null)
            {
                Debug.Log("Prepare recording successfull.");
            }
            else
            {
                Debug.Log($"Prepare recording failed with error : {error}");
            }
        });
    }

    public void StartRecording()
    {
        //开始录制
        ReplayKitManager.StartRecording();
    }

    public void StopRecording()
    {
        //停止录制
        ReplayKitManager.StopRecording((result, error) => {
            if (error == null)
            {
                Debug.Log($"File path: {result.Path}");
            }
            else
            {
                Debug.Log($"Failed with error: {error}");
            }
        });
    }

    public void Preview()
    {
        //预览录制完成的视频
        bool success = ReplayKitManager.Preview();
        Debug.Log($"Preview recording: {(success ? "Success" : "Failed")}");
    }

    public void Discard()
    {
        //删除录制完成的视频
        bool success = ReplayKitManager.Discard();
        Debug.Log($"Discard  recording: {(success ? "Success" : "Failed")}");
    }
    #endregion

    #region Event Callbacks
    private void DidInitialiseEvent(InitialiseCompleteResult result, Error error)
    {
        Debug.Log("Received Event Callback : DidInitialiseEvent [State:" + result.State.ToString() + " " + "Error:" + error);

        switch (result.State)
        {
            //SDK初始化成功
            case ReplayKitInitialisationState.Success:
                Debug.Log("ReplayKitManager.DidInitialiseEvent : Initialisation Success");
                break;
            //SDK初始化失败
            case ReplayKitInitialisationState.Failed:
                Debug.Log("ReplayKitManager.DidInitialiseEvent : Initialisation Failed with message[" + error + "]");
                break;

            default:
                Debug.Log("Unknown State");
                break;
        }
    }

    private void DidRecordingStateChangeEvent(RecordingStateChangeResult result, Error error)
    {
        Debug.Log("Received Event Callback : DidRecordingStateChangeEvent [State:" + result.State.ToString() + " " + "Error:" + error);

        switch (result.State)
        {
            //录制开始
            case ReplayKitRecordingState.Started:
                Debug.Log("ReplayKitManager.DidRecordingStateChangeEvent : Video Recording Started");
                break;
            //录制停止
            case ReplayKitRecordingState.Stopped:
                Debug.Log("ReplayKitManager.DidRecordingStateChangeEvent : Video Recording Stopped");
                break;
            //录制失败
            case ReplayKitRecordingState.Failed:
                Debug.Log("ReplayKitManager.DidRecordingStateChangeEvent : Video Recording Failed with message [" + error + "]");
                break;
            //视频录制完成并且可预览
            case ReplayKitRecordingState.Available:
                Debug.Log("ReplayKitManager.DidRecordingStateChangeEvent : Video Recording available for preview");
                break;

            default:
                Debug.Log("ReplayKit Unknown State");
                break;
        }
    }
    #endregion
}



标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号