//#define DShowDebug using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; public class DShowPlayer { private enum HapTextureFormat { HapTextureFormat_RGB_DXT1 = 0x83F0, HapTextureFormat_RGBA_DXT5 = 0x83F3, HapTextureFormat_YCoCg_DXT5 = 0x01 }; public string FilePath { get; private set; } private uint id = 0; public static bool showWarning = true; public DShowPlayer() { //release(); id = CreateNative(); } public void release() { if (id != 0) { if (videoTexture != null) #if UNITY_EDITOR UnityEngine.Object.DestroyImmediate(videoTexture); #else UnityEngine.Object.Destroy(videoTexture); #endif ReleaseNative(id); id = 0; } } public void updateTexture(bool force = false) { if (videoTexture == null) { int width = GetWidthNative(id); int height = GetHeightNative(id); TextureFormat fmt = getHapFormat(); videoTexture = new Texture2D(width, height, fmt, false, true); } UpdateTextureNative(id, videoTexture.GetNativeTexturePtr()); } private TextureFormat getHapFormat() { switch ((HapTextureFormat)GetHapFormatNative(id)) { case HapTextureFormat.HapTextureFormat_RGB_DXT1: return TextureFormat.DXT1; default: case HapTextureFormat.HapTextureFormat_RGBA_DXT5: return TextureFormat.DXT5; } } #region MoviePlayerBase // virtual function public bool Load(DShowClip path) { //if (id == 0) // create(); if (videoTexture != null) #if UNITY_EDITOR GameObject.DestroyImmediate(videoTexture); #else GameObject.Destroy(videoTexture); #endif if (path!=null && LoadNative(id, path.fullPath)) { FilePath = path.fullPath; updateTexture(true); return true; }else { } return false; } Texture2D videoTexture = null; public void Play() { PlayNative(id); } public void Pause(){ PauseNative(id); } public void Stop() { StopNative(id); } public bool Loop { set { SetLoopNative(id, value); } get { return IsLoopNative(id); } } public Texture2D Texture { get { if (videoTexture == null && showWarning) Debug.LogWarning("videoTexture is null!!!"); return videoTexture; } } public uint Frame { set { SetFrameNative(id, value); } get { return GetFrameNative(id); } } public bool IsPaused { get { return !IsPlaying; } } public bool IsLoaded { get { return IsLoadedNative(id); } } public bool IsPlaying { get { return IsPlayingNative(id); } } public bool IsFinished { get { return IsMovieDoneNative(id); } } public uint TotalNumFrames { get { return GetTotalNumFramesNative(id); } } public float GetDuration { get { return (float)GetDurationNative(id); } } public float GetCurrentTime { get { return (float)GetCurrentTimeNative(id); } } public float AudioVolume { get { return GetVolumeNative(id); } set { SetVolumeNative(id, value); } } ////////////////////////////////////////////////////////////////////////// #endregion ////////////////////////////////////////////////////////////////////////// #region DirectShowHapVideoPlayer.dll #if DShowDebug const string DShowDll = "DirectShowHapVideoPlayerD"; #else const string DShowDll = "DirectShowHapVideoPlayer"; #endif [DllImport(DShowDll)] public static extern uint CreateNative(); [DllImport(DShowDll)] public static extern void ReleaseNative(uint id); [DllImport(DShowDll)] public static extern uint GetHapFormatNative(uint id); [DllImport(DShowDll)] public static extern int GetWidthNative(uint id); [DllImport(DShowDll)] public static extern int GetHeightNative(uint id); [DllImport(DShowDll)] public static extern int GetFrameDataLengthNative(uint id); [DllImport(DShowDll)] public static extern bool IsFrameNewNative(uint id); [DllImport(DShowDll)] public static extern void SetVolumeNative(uint id, float volume); [DllImport(DShowDll)] public static extern float GetVolumeNative(uint id); ////////////////////////////////////////////////////////////////////////// [DllImport(DShowDll)] public static extern bool LoadNative(uint id, [MarshalAs(UnmanagedType.LPWStr)] string fileName); [DllImport(DShowDll)] public static extern void PlayNative(uint id); [DllImport(DShowDll)] public static extern void PauseNative(uint id); [DllImport(DShowDll)] public static extern void StopNative(uint id); [DllImport(DShowDll)] public static extern void SetLoopNative(uint id, bool loop); [DllImport(DShowDll)] public static extern bool IsLoopNative(uint id); [DllImport(DShowDll)] public static extern bool IsMovieDoneNative(uint id); [DllImport(DShowDll)] public static extern void SetFrameNative(uint id, uint frame); [DllImport(DShowDll)] public static extern uint GetFrameNative(uint id); [DllImport(DShowDll)] public static extern bool IsPlayingNative(uint id); [DllImport(DShowDll)] public static extern bool IsLoadedNative(uint id); [DllImport(DShowDll)] public static extern uint GetTotalNumFramesNative(uint id); [DllImport(DShowDll)] public static extern double GetDurationNative(uint id); [DllImport(DShowDll)] public static extern double GetCurrentTimeNative(uint id); [DllImport(DShowDll)] public static extern void UpdateTextureNative(uint id, IntPtr tex); #endregion }