KITASENJU DESIGN BLOG

memo, html, javascript, unity

Natcorder and Natshare

How to use NatCorder and NatShare. Use Recording and saving the video.

NatcorderとNatshareというAssetの使い方のメモです。動画録画と保存に使用しています。

NatCorder - Video Recording API | 機能統合 | Unity Asset Store

namespace NatSuite.Examples {

    using UnityEngine;
    using System.Collections;
    using Recorders;
    using Recorders.Clocks;
    using Recorders.Inputs;
    using NatSuite.Sharing;

    public class ReplayCam : MonoBehaviour {

        [Header("Recording")]
        public int videoWidth = 1280;
        public int videoHeight = 720;
        public bool recordMicrophone;

        private IMediaRecorder recorder;
        private CameraInput cameraInput;
        private AudioInput audioInput;
        private AudioSource microphoneSource;

        private IEnumerator Start () {
            // Start microphone
            microphoneSource = gameObject.AddComponent<AudioSource>();
            microphoneSource.mute =
            microphoneSource.loop = true;
            microphoneSource.bypassEffects =
            microphoneSource.bypassListenerEffects = false;
            microphoneSource.clip = Microphone.Start(null, true, 10, AudioSettings.outputSampleRate);
            yield return new WaitUntil(() => Microphone.GetPosition(null) > 0);
            microphoneSource.Play();
        }

        private void OnDestroy () {
            // Stop microphone
            microphoneSource.Stop();
            Microphone.End(null);
        }

        public void StartRecording () {
            // Start recording
            var frameRate = 30;
            var sampleRate = recordMicrophone ? AudioSettings.outputSampleRate : 0;
            var channelCount = recordMicrophone ? (int)AudioSettings.speakerMode : 0;
            var clock = new RealtimeClock();
            recorder = new MP4Recorder(videoWidth, videoHeight, frameRate, sampleRate, channelCount);
            // Create recording inputs
            cameraInput = new CameraInput(recorder, clock, Camera.main);
            audioInput = recordMicrophone ? new AudioInput(recorder, clock, microphoneSource, true) : null;
            // Unmute microphone
            microphoneSource.mute = audioInput == null;
        }

        public async void StopRecording () {
            
            // Mute microphone
            microphoneSource.mute = true;
            // Stop recording
            audioInput?.Dispose();
            cameraInput.Dispose();
            var path = await recorder.FinishWriting();
            // Playback recording
            Debug.Log($"Saved recording to: {path}");
            var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";
            Handheld.PlayFullScreenMovie($"{prefix}{path}");
            Debug.Log($"{prefix}{path}");

            var payload = new SavePayload();
            payload.AddMedia(path);//you dont need prefix
            payload.Commit();
            
        }
        
    }
}

撮影した動画のプレビュー用スクリプト

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using NatSuite.Sharing;

    public class MyReplayPlayer : MonoBehaviour {

        [SerializeField] private VideoPlayer _videoPlayer;
        [SerializeField] private RawImage _rawImage;
        private string _path;
        private string _path2;

        void Start(){
            //gameObject.SetActive(false);
        }

        /// <summary>
        /// _player.PlayVideo( $"{prefix}{path}", path );
        /// </summary>
        /// <param name="path"></param>
        /// <param name="path2"></param>
        public void PlayVideo(string path, string path2){

            Debug.Log("playVideo");
            Debug.Log(path+"__"+path2);
            _path = path;
            _path2 = path2;

            var enumerator =  startPreview(path);
            while (enumerator.MoveNext())
            {
                //object v = e.Current;
                //System.WriteLine(v);
            }

        }

        //ongui
        private void OnGUI(){
            if( GUI.Button(new Rect(Screen.width/2-50, Screen.height-100, 100, 50), "save") ){
                    //hozon
                    var payload = new SavePayload();
                    payload.AddMedia(_path2);//you dont need prefix
                    payload.Commit();

                    gameObject.SetActive(false);

            }
        }

        public IEnumerator startPreview (string path){
            _videoPlayer.source = VideoSource.Url;
            _videoPlayer.url = path;
            _videoPlayer.isLooping=true;
            //_videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
            //_videoPlayer.EnableAudioTrack(0, true);
            //_videoPlayer.SetTargetAudioSource(0, _videoAudio);
            _videoPlayer.Prepare ();
        
            //Wait until Movie is prepared
            WaitForSeconds waitTime = new WaitForSeconds(1);
            while (!_videoPlayer.isPrepared)
            {
                Debug.Log("Preparing Movie");
                yield return waitTime;
                break;
            }

            Debug.Log("Done Preparing Movie");
            _videoPlayer.enabled=true;
            _videoPlayer.Play();
        }

        void Update(){

            _rawImage.texture = _videoPlayer.texture;

        }

    }

使い方

light11.hatenadiary.com

"FOOTER"