Skip to content

Pick or Capture a Video

levching edited this page Apr 8, 2020 · 3 revisions

Warnign: Please note that iOS Camera view does not support landscape orientation. So if your application/game is locked to the landscaper mode, your application may crash when attempting to present camera view:

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [CAMViewfinderViewController shouldAutorotate] is returning YES'

The workaround is to allow all the orientations (or at least add one portrait option).

PickCaptureaVideo

Then if you want your game to be locked in a landscape, you can lock orientation when the game starts:

    using UnityEngine;
    ...

    creen.orientation = ScreenOrientation.LandscapeLeft;

Then before you about to present camera view, make sure you allow portrait orientation before showing the camera view.

    using UnityEngine;
    ...

    Screen.orientation = ScreenOrientation.AutoRotation;

And once you are done with the camera, you can lock back to the landscape orientation.

The samples below show how to quickly configure ISN_UIImagePickerController for common operations with a video.

Pick a Video from Photo Library

    using SA.iOS.UIKit;
    using SA.iOS.AVFoundation;
    ...

    ISN_UIImagePickerController picker = new ISN_UIImagePickerController();
    picker.SourceType = ISN_UIImagePickerControllerSourceType.Album;
    picker.MediaTypes = new List<string>() {ISN_UIMediaType.MOVIE};

    picker.Present((result) => {
        if (result.IsSucceeded) {
            Debug.Log("MOVIE local path: " + result.MediaURL);

            // We only have movie path when movie is picked.
            // So we need to generate a thumbnail manually
            // Let's ise ISN_AVAssetImageGenerator and generate video thumbnail
            // form the video firts frame
            Texture2D image = ISN_AVAssetImageGenerator.CopyCGImageAtTime(result.MediaURL, 0);

            //Example how to assing thumbnail to Image m_image
            m_image.sprite = image.ToSprite();
            //Example how to assing thumbnail to GameObject m_go;
            m_go.GetComponent<Renderer>().material.mainTexture = image;
        } else {
            Debug.Log("Madia picker failed with reason: " + result.Error.Message);
        }
    });

Capture a Video from Camera

    using SA.iOS.UIKit;
    using SA.iOS.AVFoundation;
    ...

    ISN_UIImagePickerController picker = new ISN_UIImagePickerController();
    picker.SourceType = ISN_UIImagePickerControllerSourceType.Camera;
    picker.MediaTypes = new List<string>() {ISN_UIMediaType.MOVIE};

    picker.Present((result) => {
        if (result.IsSucceeded) {
            Debug.Log("MOVIE local path: " + result.MediaURL);

            // We only have movie path when movie is picked.
            // So we need to generate a thumbnail manually
            // Let's ise ISN_AVAssetImageGenerator and generate video thumbnail
            // form the video firts frame
            Texture2D image = ISN_AVAssetImageGenerator.CopyCGImageAtTime(result.MediaURL, 0);

            //Example how to assing thumbnail to Image m_image
            m_image.sprite = image.ToSprite();
            //Example how to assing thumbnail to GameObject m_go;
            m_go.GetComponent<Renderer>().material.mainTexture = image;
        } else {
            Debug.Log("Madia picker failed with reason: " + result.Error.Message);
        }
    });

Picker Result

Once a video is picked, the picker result is represented by an ISN_UIPickerControllerResult object. So here is a couple of possibilities you can do with the picked video.

  • You may get video thumbnail and attach somewhere in your UI to acknowledge user that video was recognized. Use may use CopyCGImageAtTime method as shown in a previous code snippet.
    using SA.iOS.AVFoundation;
    ...

    //video position that will be used
    //for generaiting an image 
    float seconds = 0f;
    Texture2D image = ISN_AVAssetImageGenerator.CopyCGImageAtTime(result.MediaURL, seconds);
  • You may read a video file and for example, upload it to your server, or use it in any other way you want since you have access to picked video file. See the example below how to fully read picker video file content.
    using System.IO;
    using SA.iOS.UIKit;
    ...

    ISN_UIImagePickerController picker = new ISN_UIImagePickerController();
    picker.SourceType = ISN_UIImagePickerControllerSourceType.Album;
    picker.MediaTypes = new List<string>() { ISN_UIMediaType.MOVIE };

    picker.Present((result) => {
        if (result.IsSucceeded) {
            Debug.Log("MOVIE local path: " + result.MediaURL);
            byte[] movieBytes = File.ReadAllBytes(result.MediaURL);
            Debug.Log("movie size bytes: " + movieBytes.Length);
        } else {
            Debug.Log("Media picker failed with reason: " + result.Error.Message);
        }
    });
  • You may also play the video using the AVPlayerViewController. The Code snippet below only demonstrates basic native player use example, if you want to have more control over it, like for example control the playback volume, check out full AVPlayerViewController guide.
    using SA.iOS.Foundation;
    using SA.iOS.AVFoundation;
    using SA.iOS.AVKit;
    ...

    var url = ISN_NSURL.URLWithString(media.Path);
    var player = new ISN_AVPlayer(url);

    var viewController = new ISN_AVPlayerViewController();
    viewController.Player = player;

    viewController.Show();

Additional Options

The same as for the Image Picker.

About

Foundation

AV Foundation

App Tracking Transparency

Game Kit

Store Kit

UI Kit

Social

Replay Kit

Contacts

AVKit

Photos

App Delegate

User Notifications

MediaPlayer

Core Location

AdSupport

EventKit

CloudKit

Authentication Services

XCode

Knowledge Base

Clone this wiki locally