Skip to content

Instantly share code, notes, and snippets.

@olokobayusuf
Last active December 29, 2023 00:10
Show Gist options
  • Select an option

  • Save olokobayusuf/df6672fb86908dccaa656c4db7e99e3a to your computer and use it in GitHub Desktop.

Select an option

Save olokobayusuf/df6672fb86908dccaa656c4db7e99e3a to your computer and use it in GitHub Desktop.
An example illustrating recording video and microphone audio in Unity with NatCorder API and NatDevice API.
class VideoRecorder {
MP4Recorder recorder; // Recorder that will record an MP4
CameraInput cameraInput; // Recorder input for recording video from a game camera
AudioDevice audioDevice; // Microphone for recording user audio
void StartRecording () {
// Get a microphone
var query = new MediaDeviceQuery(MediaDeviceCriteria.AudioDevice);
audioDevice = query.current as AudioDevice;
// Create recorder
var clock = new RealtimeClock();
recorder = new MP4Recorder(1280, 720, 30, audioDevice.sampleRate, audioDevice.channelCount);
// Stream media samples
cameraInput = new CameraInput(recorder, clock, Camera.main);
audioDevice.StartRunning(audioBuffer => recorder.CommitSamples(audioBuffer.sampleBuffer.ToArray(), clock.timestamp));
}
async void StopRecording () {
// Stop streaming media to the recorder
cameraInput.Dispose();
audioDevice.StopRunning();
// Finish writing video
var recordingPath = await recorder.FinishWriting();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment