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

/Multimedia/OpenTK/OpenTKMusic.cs

#
C# | 147 lines | 97 code | 14 blank | 36 comment | 6 complexity | 976070a57aaa68682563e8b30bb2006b MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using Delta.Multimedia.BaseOpenAL;
  3. using Delta.Utilities;
  4. namespace Delta.Multimedia.OpenTK
  5. {
  6. /// <summary>
  7. /// OpenTK music
  8. /// Sample code for implementing
  9. /// -> http://www.opentk.com/node/1705
  10. /// </summary>
  11. internal class OpenTKMusic : NativeMusic
  12. {
  13. #region State (Public)
  14. /// <summary>
  15. /// The state of the music, just returning the property of the OggMusicData.
  16. /// </summary>
  17. public override MediaState State
  18. {
  19. get
  20. {
  21. return nativeMusicData.State;
  22. }
  23. protected set
  24. {
  25. base.State = value;
  26. }
  27. }
  28. #endregion
  29. #region Private
  30. #region nativeMusicData (Private)
  31. /// <summary>
  32. /// Music handle
  33. /// </summary>
  34. private OggMusicData nativeMusicData;
  35. #endregion
  36. #endregion
  37. #region Constructors
  38. /// <summary>
  39. /// Create OpenTK music
  40. /// </summary>
  41. /// <param name="parentMusic">Parent music instance.</param>
  42. public OpenTKMusic(Music parentMusic)
  43. : base(parentMusic)
  44. {
  45. }
  46. #endregion
  47. #region LoadNativeData (Public)
  48. /// <summary>
  49. /// Load content data
  50. /// </summary>
  51. /// <param name="filename">Filename</param>
  52. /// <returns>True if loading succeeded, otherwise False.</returns>
  53. public override bool LoadNativeData(string filename)
  54. {
  55. try
  56. {
  57. nativeMusicData = new OggMusicData(this, filename);
  58. }
  59. catch (Exception)
  60. {
  61. return false;
  62. }
  63. return true;
  64. }
  65. #endregion
  66. #region Dispose (Public)
  67. /// <summary>
  68. /// Dispose data
  69. /// </summary>
  70. public override void Dispose()
  71. {
  72. if (nativeMusicData != null)
  73. {
  74. nativeMusicData.Dispose();
  75. nativeMusicData = null;
  76. }
  77. }
  78. #endregion
  79. #region Methods (Private)
  80. #region SetVolume
  81. /// <summary>
  82. /// Set the volume of the native OpenTK music.
  83. /// </summary>
  84. /// <param name="setVolume">Volume to set.</param>
  85. protected override void SetVolume(float setVolume)
  86. {
  87. volume = setVolume;
  88. nativeMusicData.Volume = volume;
  89. }
  90. #endregion
  91. #region PlayNative
  92. /// <summary>
  93. /// Play native
  94. /// </summary>
  95. protected override void PlayNative()
  96. {
  97. try
  98. {
  99. nativeMusicData.Play();
  100. }
  101. catch (Exception ex)
  102. {
  103. Log.Warning("Failed to play OpenTKMusic " + Parent.Name + ": " + ex);
  104. }
  105. }
  106. #endregion
  107. #region UpdateNative
  108. /// <summary>
  109. /// Updates the Music
  110. /// </summary>
  111. protected override void UpdateNative()
  112. {
  113. if (nativeMusicData != null)
  114. {
  115. nativeMusicData.Update(ref elapsedPlayTime);
  116. }
  117. }
  118. #endregion
  119. #region StopNative
  120. /// <summary>
  121. /// Stop native
  122. /// </summary>
  123. protected override void StopNative()
  124. {
  125. if (nativeMusicData != null)
  126. {
  127. nativeMusicData.Stop();
  128. }
  129. }
  130. #endregion
  131. #endregion
  132. }
  133. }