PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

/Multimedia/Vlc/LibVlc/VlcMediaPlayer.cs

#
C# | 174 lines | 116 code | 16 blank | 42 comment | 6 complexity | 0f5ae4c807f15bbe630ac7358f06fc7a MD5 | raw file
  1using System;
  2
  3namespace Delta.Multimedia.Vlc.LibVlc
  4{
  5	internal class VlcMediaPlayer : IDisposable
  6	{
  7		#region Drawable (Public)
  8		/// <summary>
  9		/// Get or set the drawing target for the media player.
 10		/// </summary>
 11		public IntPtr Drawable
 12		{
 13			get
 14			{
 15				return drawable;
 16			}
 17			set
 18			{
 19				VlcCore.libvlc_media_player_set_hwnd(handle, value);
 20				drawable = value;
 21			}
 22		}
 23		#endregion
 24
 25		#region Media (Public)
 26		/// <summary>
 27		/// Get or set the current media item being played by this media player.
 28		/// </summary>
 29		public VlcMedia Media
 30		{
 31			get
 32			{
 33				IntPtr media = VlcCore.libvlc_media_player_get_media(handle);
 34				if (media == IntPtr.Zero)
 35				{
 36					return null;
 37				}
 38				return new VlcMedia(media);
 39			}
 40			set
 41			{
 42				VlcCore.libvlc_media_player_set_media(handle, value.Handle);
 43			}
 44		}
 45		#endregion
 46
 47		#region VideoPosition (Public)
 48		/// <summary>
 49		/// Get the current position in the media playback in milliseconds.
 50		/// </summary>
 51		public long VideoPosition
 52		{
 53			get
 54			{
 55				return VlcCore.libvlc_media_player_get_time(handle);
 56			}
 57		}
 58		#endregion
 59
 60		#region VideoLength (Public)
 61		/// <summary>
 62		/// Get the length of the current media item in milliseconds.
 63		/// </summary>
 64		public long VideoLength
 65		{
 66			get
 67			{
 68				return VlcCore.libvlc_media_player_get_length(handle);
 69			}
 70		}
 71		#endregion
 72
 73		#region Volume (Public)
 74		/// <summary>
 75		/// Get or set the volume of the media playback.
 76		/// <para />
 77		/// The value is the volume in percents (0 = mute, 100 = 0dB).
 78		/// </summary>
 79		public int Volume
 80		{
 81			get
 82			{
 83				return VlcCore.libvlc_audio_get_volume(handle);
 84			}
 85			set
 86			{
 87				VlcCore.libvlc_audio_set_volume(handle, value);
 88			}
 89		}
 90		#endregion
 91
 92		#region Private
 93
 94		#region drawable (Private)
 95		private IntPtr drawable;
 96		#endregion
 97
 98		#region handle (Private)
 99		/// <summary>
100		/// The media player handle.
101		/// </summary>
102		private readonly IntPtr handle;
103		#endregion
104
105		#endregion
106
107		#region Constructors
108		/// <summary>
109		/// Create a new vlc media player instance.
110		/// </summary>
111		/// <param name="media">The media item to create the player with.</param>
112		public VlcMediaPlayer(VlcMedia media)
113		{
114			handle = VlcCore.libvlc_media_player_new_from_media(media.Handle);
115			if (handle == IntPtr.Zero)
116			{
117				throw new VlcException();
118			}
119
120			//MethodInfo info = GetType().GetMethod("EventHandlerCallback",
121			//  BindingFlags.NonPublic | BindingFlags.Instance);
122
123			//Delegate deleg = Delegate.CreateDelegate(
124			//  typeof(VlcCore.VlcEventHandlerDelegate), this, info, false);
125			//VlcCore.AttachToEvent(handle, deleg,
126			//  VlcEventType.libvlc_MediaStateChanged);
127		}
128		#endregion
129
130		#region IDisposable Members
131		/// <summary>
132		/// Free the native media player handle.
133		/// </summary>
134		public void Dispose()
135		{
136			VlcCore.libvlc_media_player_release(handle);
137		}
138		#endregion
139
140		#region Play (Public)
141		/// <summary>
142		/// Play the current media item.
143		/// </summary>
144		public void Play()
145		{
146			int ret = VlcCore.libvlc_media_player_play(handle);
147			if (ret == -1)
148			{
149				throw new VlcException();
150			}
151		}
152		#endregion
153
154		#region Pause (Public)
155		/// <summary>
156		/// Pause the current media item.
157		/// </summary>
158		public void Pause()
159		{
160			VlcCore.libvlc_media_player_pause(handle);
161		}
162		#endregion
163
164		#region Stop (Public)
165		/// <summary>
166		/// Stop the current media item.
167		/// </summary>
168		public void Stop()
169		{
170			VlcCore.libvlc_media_player_stop(handle);
171		}
172		#endregion
173	}
174}