using System; using UnityEngine; namespace UMP { public class MediaPlayer : IPlayer, IPlayerAudio, IPlayerSpu { private object _playerObject; private IPlayer _player; private IPlayerAudio _playerAudio; private IPlayerSpu _playerSpu; #region Constructors /// /// Create new instance of media player object /// /// MonoBehaviour instanse /// Objects that will be rendering video output public MediaPlayer(MonoBehaviour monoObject, GameObject[] videoOutputObjects) : this(monoObject, videoOutputObjects, null) { } /// /// Create instance of media player object with additional arguments /// /// MonoBehaviour instanse /// Objects that will be rendering video output /// Additional player options public MediaPlayer(MonoBehaviour monoObject, GameObject[] videoOutputObjects, PlayerOptions options) { var supportedPlatform = UMPSettings.RuntimePlatform; switch (supportedPlatform) { case UMPSettings.Platforms.Win: case UMPSettings.Platforms.Mac: case UMPSettings.Platforms.Linux: PlayerOptionsStandalone standaloneOptions = null; if (options is PlayerOptionsStandalone) standaloneOptions = options as PlayerOptionsStandalone; else standaloneOptions = new PlayerOptionsStandalone(null); _playerObject = new MediaPlayerStandalone(monoObject, videoOutputObjects, standaloneOptions); break; /* case UMPSettings.Platforms.iOS: PlayerOptionsIPhone iphoneOptions = null; if (options is PlayerOptionsIPhone) iphoneOptions = options as PlayerOptionsIPhone; else iphoneOptions = new PlayerOptionsIPhone(null); _playerObject = new MediaPlayerIPhone(monoObject, videoOutputObjects, iphoneOptions); break; case UMPSettings.Platforms.Android: PlayerOptionsAndroid androidOptions = null; if (options is PlayerOptionsAndroid) androidOptions = options as PlayerOptionsAndroid; else androidOptions = new PlayerOptionsAndroid(null); _playerObject = new MediaPlayerAndroid(monoObject, videoOutputObjects, androidOptions); break; */ case UMPSettings.Platforms.WebGL: _playerObject = new MediaPlayerWebGL(monoObject, videoOutputObjects, options); break; } if (_playerObject is IPlayer) _player = (_playerObject as IPlayer); if (_playerObject is IPlayerAudio) _playerAudio = (_playerObject as IPlayerAudio); if (_playerObject is IPlayerSpu) _playerSpu = (_playerObject as IPlayerSpu); } /// /// Create new instance of MediaPlayer object from another MediaPlayer instance /// /// MonoBehaviour instanse /// Based on MediaPlayer instance public MediaPlayer(MonoBehaviour monoObject, MediaPlayer basedPlayer) : this(monoObject, basedPlayer.VideoOutputObjects, basedPlayer.Options) { if (basedPlayer.DataSource != null && string.IsNullOrEmpty(basedPlayer.DataSource.ToString())) _player.DataSource = basedPlayer.DataSource; _player.EventManager.CopyPlayerEvents(basedPlayer.EventManager); _player.Mute = basedPlayer.Mute; _player.Volume = basedPlayer.Volume; _player.PlaybackRate = basedPlayer.PlaybackRate; } #endregion /// /// Get media player object for current running platform /// (supported: Standalone, WebGL, Android and iOS platforms) /// for get more additional possibilities that exists only for this platform. /// public object PlatformPlayer { get { return _playerObject; } } /// /// Get/Set simple array that consist with Unity 'GameObject' that have 'Mesh Renderer' (with some material) /// or 'Raw Image' component /// public GameObject[] VideoOutputObjects { get { return _player.VideoOutputObjects; } set { _player.VideoOutputObjects = value; } } /// /// Get event manager for current media player to add possibility to attach/detach special playback listeners /// public PlayerManagerEvents EventManager { get { return _player.EventManager; } } /// /// Get player additional options for current running platform /// public PlayerOptions Options { get { return _player.Options; } } /// /// Get current video playback state /// public PlayerState State { get { return _player.State; } } /// /// Get current video playback state value (can be float, long or string type) /// public object StateValue { get { return _player.StateValue; } } /// /// Add new main group of listeners to current media player instance /// /// Group of listeners public void AddMediaListener(IMediaListener listener) { _player.AddMediaListener(listener); } /// /// Remove group of listeners from current media player instance /// /// Group of listeners public void RemoveMediaListener(IMediaListener listener) { _player.RemoveMediaListener(listener); } /// /// Prepare current video playback /// public void Prepare() { _player.Prepare(); } /// /// Play or resume (True if playback started (and was already started), or False on error. /// /// public bool Play() { return _player.Play(); } /// /// Toggle pause current video playback (no effect if there is no media) /// public void Pause() { _player.Pause(); } /// /// Stop current video playback (no effect if there is no media) /// /// Clear the last frame public void Stop(bool resetTexture) { _player.Stop(resetTexture); } /// /// Stop current video playback (no effect if there is no media) /// public void Stop() { Stop(true); } /// /// Release a current media player instance /// public void Release() { _player.Release(); } /// /// Get/Set local path or url link to your video/audio file/stream /// Example of using: /// Local storage space - 'file:///C:\MyFolder\Videos\video1.mp4' or /// 'C:\MyFolder\Videos\video1.mp4' or /// 'file:///DCIM/100ANDRO/MyVideo.mp4' (example for Android platform); /// Remote space (streams) - 'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov'; /// 'StreamingAssets' folder - 'file:///myVideoFile.mp4'; /// public string DataSource { get { return _player.DataSource; } set { _player.DataSource = value; } } /// /// Is media is currently playing /// public bool IsPlaying { get { return _player.IsPlaying; } } /// /// Is media is ready to play (first frame available) /// public bool IsReady { get { return _player.IsReady; } } /// /// Is the player able to play /// public bool AbleToPlay { get { return _player.AbleToPlay; } } /// /// Get the current video length (in milliseconds) /// public long Length { get { return _player.Length; } } /// /// Get the current video formatted length (hh:mm:ss[:ms]). /// /// True: formatted length will be with [:ms] public string GetFormattedLength(bool detail) { return _player.GetFormattedLength(detail); } /// /// Get frames per second (fps) for current video playback. /// * Warning: it's not a predefined value from video file/stream - calculated in video playback process /// public float FrameRate { get { return _player.FrameRate; } } /// /// Get video frames counter /// public int FramesCounter { get { return _player.FramesCounter; } } /// /// Get pixels of current video frame /// Example of using: /// texture.LoadRawTextureData(_player.FramePixels); /// texture.Apply(); /// public byte[] FramePixels { get { return _player.FramePixels; } } /// /// Get/Set the current video time (in milliseconds). /// This has no effect if no media is being played. /// Not all formats and protocols support this /// public long Time { get { return _player.Time; } set { _player.Time = value; } } /// /// Get/Set video position. /// This has no effect if playback is not enabled. /// This might not work depending on the underlying input format and protocol /// public float Position { get { return _player.Position; } set { _player.Position = value; } } /// /// Get/Set the requested video play rate /// public float PlaybackRate { get { return _player.PlaybackRate; } set { _player.PlaybackRate = value; } } /// /// Get/Set current software audio volume /// (by default you can change this value from '0' to '100') /// public int Volume { get { return _player.Volume; } set { _player.Volume = value; } } /// /// Get/Set mute status for current video playback /// public bool Mute { get { return _player.Mute; } set { _player.Mute = value; } } /// /// Get current video width in pixels /// public int VideoWidth { get { return _player.VideoWidth; } } /// /// Get current video height in pixels /// public int VideoHeight { get { return _player.VideoHeight; } } /// /// Get the pixel dimensions of current video /// public Vector2 VideoSize { get { return new Vector2(VideoWidth, VideoHeight); } } /// /// Get the available audio tracks /// public MediaTrackInfo[] AudioTracks { get { if (_playerAudio != null) return _playerAudio.AudioTracks; return null; } } /// /// Get/Set the current audio track /// public MediaTrackInfo AudioTrack { get { if (_playerAudio != null) return _playerAudio.AudioTrack; return null; } set { if (_playerAudio != null) _playerAudio.AudioTrack = value; } } /// /// Get the available subtitle tracks /// public MediaTrackInfo[] SpuTracks { get { if (_playerSpu != null) return _playerSpu.SpuTracks; return null; } } /// /// Get/Set the current subtitle track (supported only on Standalone platform) /// public MediaTrackInfo SpuTrack { get { if (_playerSpu != null) return _playerSpu.SpuTrack; return null; } set { if (_playerSpu != null) _playerSpu.SpuTrack = value; } } /// /// Set new video subtitle file /// /// Path to the new video subtitle file /// public bool SetSubtitleFile(Uri path) { if (_playerSpu != null) return _playerSpu.SetSubtitleFile(path); return false; } } }