PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Multimedia/Vlc/LibVlc/VlcMediaPlayer.cs

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