Search Results for

    Show / Hide Table of Contents

    Loading Media

    The location of media can be specified in two main ways:

    1. Media Reference
    2. Path

    Media Reference

    alt_text

    The MediaReference asset allows media to be specified and stored within Unity, media hints to be set (eg transparency and stereo) and referenced easily. This is the preferred way to specify media if the media is permanent.

    Path

    alt_text

    Alternatively a file path / URL can be specified directly in the MediaPlayer. This is suitable for media that isn't permanent and therefore isn't worth creating a MediaReference for. With this method the media hints need to be specified in the MediaPlayer.

    Scripting

    // Opening media via a MediaReference
    MediaReference mediaReference = _myMediaReference;
    bool isOpening = mediaPlayer.OpenMedia(mediaReference, autoPlay:true);
    
    // Opening media URL via a Path
    bool isOpening = mediaPlayer.OpenMedia(new MediaPath("https://www.myvideos.com/stream.m3u8", MediaPathType.AbsolutePathOrURL), autoPlay:true);
    
    // Opening local file media via a Path
    bool isOpening = mediaPlayer.OpenMedia(new MediaPath("myvideo.mp4", MediaPathType.RelativeToStreamingAssetsFolder), autoPlay:true);
    
    // Changing the media hints for content loaded via Path
    MediaHints hints = mediaPlayer.FallbackMediaHints;
    hints.stereoPacking = StereoPacking.TopBottom;
    mediaPlayer.FallbackMediaHints = hints;
    

    Android

    On Android keeping very large video files in the StreamingAssets folder is not a good idea because this folder is converted into a JAR file and uses a lot of memory to open. In these cases it's best to store the videos in Application.persistentDataPath (MediaPathType.RelativeToPersistentDataFolder), usually by downloading it to this folder. You may also need to set the permissions to access this folder which is in Player Settings > Android > Write Permission > External (SDCard).

    From Android 13 and onwards, accessing the default 'Media' folder requires a special android.permission.READ_MEDIA_VIDEO permission that is not automatically requested by a Unity built application. This permission must therefore, if required, be requested of the user programmatically. With the asset, we include a script (RequestPermission.cs) to perform this action.

    In This Article