videokit-ai / videokit

Low-code, cross-platform media SDK for Unity Engine. Register at https://videokit.ai
https://videokit.ai
Apache License 2.0
107 stars 14 forks source link

0.0.18-alpha.4 unable to record #105

Closed marvpaul closed 8 months ago

marvpaul commented 9 months ago

I tried to record with videokit and I got the following exception: InvalidOperationException: Operation is not valid due to the current state of the object. VideoKit.Internal.VideoKit.Throw (VideoKit.Internal.VideoKit+Status status) (at ./Library/PackageCache/ai.natml.videokit@0.0.18-alpha.4/Runtime/Internal/VideoKit.cs:692) VideoKit.MediaRecorder.get_sampleRate () (at ./Library/PackageCache/ai.natml.videokit@0.0.18-alpha.4/Runtime/MediaRecorder.cs:126) VideoKit.MediaRecorder.Append (VideoKit.AudioBuffer audioBuffer) (at ./Library/PackageCache/ai.natml.videokit@0.0.18-alpha.4/Runtime/MediaRecorder.cs:157) VideoKit.Sources.SceneAudioSource+<>c__DisplayClass11_0.<.ctor>b__0 (System.Single[] data, System.Int32 channels) (at ./Library/PackageCache/ai.natml.videokit@0.0.18-alpha.4/Runtime/Sources/SceneAudioSource.cs:121) VideoKit.Sources.SceneAudioSource+AudioSourceAttachment.OnAudioFilterRead (System.Single[] data, System.Int32 channels) (at ./Library/PackageCache/ai.natml.videokit@0.0.18-alpha.4/Runtime/Sources/SceneAudioSource.cs:131)

My start recording mehtod:

public async void StartRecording()
    {
        gameObject.GetComponent<VideoKitRecorder>().OnRecordingCompleted.RemoveAllListeners(); 
        gameObject.GetComponent<VideoKitRecorder>().OnRecordingCompleted.AddListener(StopRecording); 
#if UNITY_ANDROID
        if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
        {
            Permission.RequestUserPermission(Permission.ExternalStorageWrite);
        }
#endif

        if (GetComponent<VideoKitRecorder>().status == VideoKitRecorder.Status.Recording)
        {
            await GetComponent<VideoKitRecorder>().StopRecording();
            GameObject.Find("RecBtn").GetComponent<Blinking>().stopBlink();
        }
        else if (GetComponent<VideoKitRecorder>().status != VideoKitRecorder.Status.Recording && !recorderIsBusy)
        {
            recorderIsBusy = true;
            GetComponent<VideoKitRecorder>().texture = GameObject.Find("Plane").GetComponent<MeshRenderer>().material.mainTexture;
            GetComponent<VideoKitRecorder>().resolution = VideoKitRecorder.Resolution.Custom;
            bool record1080p = Settings.settingsObj["sections"]["Others"]["Record quality"]["current"]["value"] == 1080;
            int recordingWidth = record1080p ? 1920 : 1280;
            int recordingHeight = record1080p ? 1080 : 720;
            if (Settings.settingsObj["sections"]["Others"]["Record format"]["current"]["value"] == "keepDeviceRatio")
            {
                if (Screen.width > Screen.height)
                {

                    recordingWidth = (int)((float)Screen.width / (float)Screen.height * (float)recordingHeight);
                }
                else
                {

                    recordingWidth = (int)((float)Screen.height / (float)Screen.width * (float)recordingHeight);
                }
                //Make sure width is even as this could cause problems on android 
                if (recordingWidth % 2 == 1)
                {
                    recordingWidth += 1;
                }
            }
            Debug.Log("width" + recordingWidth + "height" + recordingHeight);

            if (Screen.width > Screen.height)
            {
                GetComponent<VideoKitRecorder>().customResolution = new Vector2Int(recordingWidth, recordingHeight);
            }
            else
            {
                GetComponent<VideoKitRecorder>().customResolution = new Vector2Int(recordingHeight, recordingWidth);
            }
            GetComponent<VideoKitRecorder>().keyframeInterval = 1;
            GetComponent<VideoKitRecorder>().videoBitRate = recordingWidth * recordingHeight * 30;

            if (PlayerPrefs.GetInt("mic") == 0 || AudioPeer.instance.audioDevice == null)
            {
                GetComponent<VideoKitRecorder>().audioMode = VideoKitRecorder.AudioMode.AudioListener;
            }
            else
            {
                GetComponent<VideoKitRecorder>().audioMode = VideoKitRecorder.AudioMode.AudioDevice;
            }
            GameObject.Find("RecBtn").GetComponent<Blinking>().startBlink();
            GameObject.Find("Notification").GetComponent<UINotificationManager>().Notify("", "Started recording!", "Recording started successfully.");
            await GetComponent<VideoKitRecorder>().StartRecording();
        }
    }

I used the following settings:

Screenshot 2024-02-22 at 08 19 42

Do you have any idea what could cause this problem?

olokobayusuf commented 9 months ago

@marvpaul are you running this on the Windows editor by any chance?

marvpaul commented 9 months ago

@marvpaul are you running this on the Windows editor by any chance?

No it's Mac os, unity 2022.3.10f1

marvpaul commented 9 months ago

I also created a simple test project where I was able to reproduce the issue. I just added VideoKit to an empty project and started recording when you click on the white square in the scene. Then the error occurs. https://github.com/marvpaul/videokit

olokobayusuf commented 8 months ago

Repro'ed, fix coming!

olokobayusuf commented 8 months ago

Fixed in 0.0.18-alpha.6. Also @marvpaul your access key is public in the repro you shared. I strongly recommend generating a new access key, and deleting that one to prevent leakage.

To omit the VideoKit settings from git tracking, add the following to your .gitignore:

# VideoKit
ProjectSettings/VideoKit.asset
marvpaul commented 8 months ago

@lanreolokoba thank you, I can confirm it's solved! I also deleted my linked repository

olokobayusuf commented 8 months ago

Sounds good.