[Unity]Record Video on iOS

Example

How it works

Installation

Usage

Setup MediaCreator for mov file (no audio)

string cachePath = "file://" + Application.temporaryCachePath + "/tmp.mov";
MediaCreator.InitAsMovWithAudio(cachePath, "h264", width, height);

Setup MediaCreator for mov file (with audio)

string cachePath = "file://" + Application.temporaryCachePath + "/tmp.mov";
MediaCreator.InitAsMovWithAudio(cachePath, "h264", texture.width, texture.height, 1, 48_000);

Setup MediaCreator for Live Photos

string uuid = System.Guid.NewGuid().ToString();
string cachePath = "file://" + Application.temporaryCachePath + "/tmp.mov";
MediaCreator.InitAsMovWithAudio(cachePath, "h264", width, height, uuid);

Setup MediaCreator for wav file

string cachePath = "file://" + Application.temporaryCachePath + "/tmp.wav";
MediaCreator.InitAsWav(cachePath, 1, 48000, 32);

Start Recording

long startTimeOffset = 0;
MediaCreator.Start(startTimeOffset);

Write Texture

Texture texture = Get Texture;
long time = startTimeOffset + Elapsed time from Start;
MediaCreator.WriteVideo(texture, time);

Write Audio PCM

float[] pcm = Get PCM float array;
long time = startTimeOffset + Elapsed time from Start;
MediaCreator.WriteAudio(pcm, time);
var source = gameObject.AddComponent<AudioSource>();
var clip = Microphone.Start(null, true, 1, 48_000);
source.clip = clip;
source.loop = true;
while (Microphone.GetPosition(null) < 0) { }

source.Play();
void OnAudioFilterRead(float[] data, int channels)
{
writeAudio(data, channels);

for (int i = 0; i < data.Length; i++)
{
data[i] = 0;
}
}

Finish Recording

MediaCreator.FinishSync();

Save mov to album app

MediaSaver.SaveVideo(cachePath);

Save Live Photos to album app

MediaSaver.SaveLivePhotos(texture, uuid, cachePath);

Save Texture to album app as picture

MediaSaver.SaveImage(texture, "png");

--

--

--

Hello! I’m yuki! ios, swift, golang, unity etc…

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How To Remove Background From Image In CorelDraw Easily

Nested Optionals in Swift, design mistake?

MapKit vs Google Places SDK

Starting auto-layout programmatically

UserDefaults in iOS swift

Clean specific folder before building in Xcode

What’s new in InAppWebView 5? Null-safety, new features, bug fixes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
f.yuki

f.yuki

Hello! I’m yuki! ios, swift, golang, unity etc…

More from Medium

Creating an iOS AR Portal App using Occlusion Materials

Real life + virtual reality — my first experience

Comparison of IOS and ANDROID

Comparison of IOS and ANDROID

Open sourcing our BR Radio PlayButton