PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/AVFoundation/Events.cs

https://github.com/kjpou1/maccore
C# | 377 lines | 294 code | 54 blank | 29 comment | 30 complexity | 1a59f9b42fc3e8e699ba19faed35bddd MD5 | raw file
Possible License(s): Apache-2.0
  1. //
  2. // C#-like events for AVFoundation classes
  3. //
  4. // Author:
  5. // Miguel de Icaza (miguel@novell.com)
  6. // Copyright 2009, Novell, Inc.
  7. // Copyright 2010, Novell, Inc.
  8. // Copyright 2011, 2012 Xamarin Inc.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. //
  30. using System;
  31. using MonoMac.Foundation;
  32. using MonoMac.ObjCRuntime;
  33. namespace MonoMac.AVFoundation {
  34. public class AVErrorEventArgs : EventArgs {
  35. public AVErrorEventArgs (NSError error)
  36. {
  37. Error = error;
  38. }
  39. public NSError Error { get; private set; }
  40. }
  41. public class AVStatusEventArgs : EventArgs {
  42. public AVStatusEventArgs (bool status)
  43. {
  44. Status = status;
  45. }
  46. public bool Status { get; private set; }
  47. }
  48. #pragma warning disable 672
  49. sealed class InternalAVAudioPlayerDelegate : AVAudioPlayerDelegate {
  50. internal EventHandler cbEndInterruption, cbBeginInterruption;
  51. internal EventHandler<AVStatusEventArgs> cbFinishedPlaying;
  52. internal EventHandler<AVErrorEventArgs> cbDecoderError;
  53. [Preserve (Conditional = true)]
  54. public override void FinishedPlaying (AVAudioPlayer player, bool flag)
  55. {
  56. if (cbFinishedPlaying != null)
  57. cbFinishedPlaying (player, new AVStatusEventArgs (flag));
  58. if (player.Handle == IntPtr.Zero)
  59. throw new ObjectDisposedException ("player", "the player object was Dispose()d during the callback, this has corrupted the state of the program");
  60. }
  61. [Preserve (Conditional = true)]
  62. public override void DecoderError (AVAudioPlayer player, NSError error)
  63. {
  64. if (cbDecoderError != null)
  65. cbDecoderError (player, new AVErrorEventArgs (error));
  66. }
  67. #if !MONOMAC
  68. [Preserve (Conditional = true)]
  69. public override void BeginInterruption (AVAudioPlayer player)
  70. {
  71. if (cbBeginInterruption != null)
  72. cbBeginInterruption (player, EventArgs.Empty);
  73. }
  74. [Preserve (Conditional = true)]
  75. public override void EndInterruption (AVAudioPlayer player)
  76. {
  77. if (cbEndInterruption != null)
  78. cbEndInterruption (player, EventArgs.Empty);
  79. }
  80. #endif
  81. }
  82. #pragma warning restore 672
  83. public partial class AVAudioPlayer {
  84. InternalAVAudioPlayerDelegate EnsureEventDelegate ()
  85. {
  86. var del = WeakDelegate as InternalAVAudioPlayerDelegate;
  87. if (del == null){
  88. del = new InternalAVAudioPlayerDelegate ();
  89. WeakDelegate = del;
  90. }
  91. return del;
  92. }
  93. public event EventHandler<AVStatusEventArgs> FinishedPlaying {
  94. add {
  95. EnsureEventDelegate ().cbFinishedPlaying += value;
  96. }
  97. remove {
  98. EnsureEventDelegate ().cbFinishedPlaying -= value;
  99. }
  100. }
  101. public event EventHandler<AVErrorEventArgs> DecoderError {
  102. add {
  103. EnsureEventDelegate ().cbDecoderError += value;
  104. }
  105. remove {
  106. EnsureEventDelegate ().cbDecoderError -= value;
  107. }
  108. }
  109. public event EventHandler BeginInterruption {
  110. add {
  111. EnsureEventDelegate ().cbBeginInterruption += value;
  112. }
  113. remove {
  114. EnsureEventDelegate ().cbBeginInterruption -= value;
  115. }
  116. }
  117. public event EventHandler EndInterruption {
  118. add {
  119. EnsureEventDelegate ().cbEndInterruption += value;
  120. }
  121. remove {
  122. EnsureEventDelegate ().cbEndInterruption -= value;
  123. }
  124. }
  125. }
  126. internal class InternalAVAudioRecorderDelegate : AVAudioRecorderDelegate {
  127. internal EventHandler cbEndInterruption, cbBeginInterruption;
  128. internal EventHandler<AVStatusEventArgs> cbFinishedRecording;
  129. internal EventHandler<AVErrorEventArgs> cbEncoderError;
  130. [Preserve (Conditional = true)]
  131. public override void FinishedRecording (AVAudioRecorder recorder, bool flag)
  132. {
  133. if (cbFinishedRecording != null)
  134. cbFinishedRecording (recorder, new AVStatusEventArgs (flag));
  135. }
  136. [Preserve (Conditional = true)]
  137. public override void EncoderError (AVAudioRecorder recorder, NSError error)
  138. {
  139. if (cbEncoderError != null)
  140. cbEncoderError (recorder, new AVErrorEventArgs (error));
  141. }
  142. #if !MONOMAC
  143. [Preserve (Conditional = true)]
  144. public override void BeginInterruption (AVAudioRecorder recorder)
  145. {
  146. if (cbBeginInterruption != null)
  147. cbBeginInterruption (recorder, EventArgs.Empty);
  148. }
  149. [Preserve (Conditional = true)]
  150. [Obsolete ("Deprecated in iOS 6.0")]
  151. public override void EndInterruption (AVAudioRecorder recorder)
  152. {
  153. if (cbEndInterruption != null)
  154. cbEndInterruption (recorder, EventArgs.Empty);
  155. }
  156. #endif
  157. }
  158. public partial class AVAudioRecorder {
  159. InternalAVAudioRecorderDelegate EnsureEventDelegate ()
  160. {
  161. var del = WeakDelegate as InternalAVAudioRecorderDelegate;
  162. if (del == null){
  163. del = new InternalAVAudioRecorderDelegate ();
  164. WeakDelegate = del;
  165. }
  166. return del;
  167. }
  168. public event EventHandler<AVStatusEventArgs> FinishedRecording {
  169. add {
  170. EnsureEventDelegate ().cbFinishedRecording += value;
  171. }
  172. remove {
  173. EnsureEventDelegate ().cbFinishedRecording -= value;
  174. }
  175. }
  176. public event EventHandler<AVErrorEventArgs> EncoderError {
  177. add {
  178. EnsureEventDelegate ().cbEncoderError += value;
  179. }
  180. remove {
  181. EnsureEventDelegate ().cbEncoderError -= value;
  182. }
  183. }
  184. public event EventHandler BeginInterruption {
  185. add {
  186. EnsureEventDelegate ().cbBeginInterruption += value;
  187. }
  188. remove {
  189. EnsureEventDelegate ().cbBeginInterruption -= value;
  190. }
  191. }
  192. public event EventHandler EndInterruption {
  193. add {
  194. EnsureEventDelegate ().cbEndInterruption += value;
  195. }
  196. remove {
  197. EnsureEventDelegate ().cbEndInterruption -= value;
  198. }
  199. }
  200. }
  201. public class AVSampleRateEventArgs : EventArgs {
  202. public AVSampleRateEventArgs (double sampleRate)
  203. {
  204. SampleRate = sampleRate;
  205. }
  206. public double SampleRate { get; private set; }
  207. }
  208. public class AVChannelsEventArgs : EventArgs {
  209. public AVChannelsEventArgs (int numberOfChannels)
  210. {
  211. NumberOfChannels = numberOfChannels;
  212. }
  213. public int NumberOfChannels { get; private set; }
  214. }
  215. public class AVCategoryEventArgs : EventArgs {
  216. public AVCategoryEventArgs (string category)
  217. {
  218. Category = category;
  219. }
  220. public string Category { get; private set; }
  221. }
  222. #if !MONOMAC
  223. internal class InternalAVAudioSessionDelegate : AVAudioSessionDelegate {
  224. internal EventHandler cbEndInterruption, cbBeginInterruption;
  225. internal EventHandler<AVCategoryEventArgs> cbCategoryChanged;
  226. internal EventHandler<AVStatusEventArgs> cbInputAvailabilityChanged;
  227. internal EventHandler<AVSampleRateEventArgs> cbSampleRateChanged;
  228. internal EventHandler<AVChannelsEventArgs> cbInputChanged;
  229. internal EventHandler<AVChannelsEventArgs> cbOutputChanged;
  230. AVAudioSession session;
  231. [Preserve (Conditional = true)]
  232. public InternalAVAudioSessionDelegate (AVAudioSession session)
  233. {
  234. this.session = session;
  235. }
  236. [Preserve (Conditional = true)]
  237. public override void BeginInterruption ()
  238. {
  239. if (cbBeginInterruption != null)
  240. cbBeginInterruption (session, EventArgs.Empty);
  241. }
  242. [Preserve (Conditional = true)]
  243. public override void EndInterruption ()
  244. {
  245. if (cbEndInterruption != null)
  246. cbEndInterruption (session, EventArgs.Empty);
  247. }
  248. [Preserve (Conditional = true)]
  249. public override void InputIsAvailableChanged (bool isInputAvailable)
  250. {
  251. if (cbInputAvailabilityChanged != null)
  252. cbInputAvailabilityChanged (session, new AVStatusEventArgs (isInputAvailable));
  253. }
  254. }
  255. public partial class AVAudioSession {
  256. InternalAVAudioSessionDelegate EnsureEventDelegate ()
  257. {
  258. var del = WeakDelegate as InternalAVAudioSessionDelegate;
  259. if (del == null){
  260. del = new InternalAVAudioSessionDelegate (this);
  261. WeakDelegate = del;
  262. }
  263. return del;
  264. }
  265. public event EventHandler BeginInterruption {
  266. add {
  267. EnsureEventDelegate ().cbBeginInterruption += value;
  268. }
  269. remove {
  270. EnsureEventDelegate ().cbBeginInterruption -= value;
  271. }
  272. }
  273. public event EventHandler EndInterruption {
  274. add {
  275. EnsureEventDelegate ().cbEndInterruption += value;
  276. }
  277. remove {
  278. EnsureEventDelegate ().cbBeginInterruption -= value;
  279. }
  280. }
  281. public event EventHandler<AVCategoryEventArgs> CategoryChanged {
  282. add {
  283. EnsureEventDelegate ().cbCategoryChanged += value;
  284. }
  285. remove {
  286. EnsureEventDelegate ().cbCategoryChanged -= value;
  287. }
  288. }
  289. public event EventHandler<AVStatusEventArgs> InputAvailabilityChanged {
  290. add {
  291. EnsureEventDelegate ().cbInputAvailabilityChanged += value;
  292. }
  293. remove {
  294. EnsureEventDelegate ().cbInputAvailabilityChanged -= value;
  295. }
  296. }
  297. public event EventHandler<AVSampleRateEventArgs> SampleRateChanged {
  298. add {
  299. EnsureEventDelegate ().cbSampleRateChanged += value;
  300. }
  301. remove {
  302. EnsureEventDelegate ().cbSampleRateChanged -= value;
  303. }
  304. }
  305. public event EventHandler<AVChannelsEventArgs> InputChannelsChanged {
  306. add {
  307. EnsureEventDelegate ().cbInputChanged += value;
  308. }
  309. remove {
  310. EnsureEventDelegate ().cbOutputChanged += value;
  311. }
  312. }
  313. public event EventHandler<AVChannelsEventArgs> OutputChannelsChanged {
  314. add {
  315. EnsureEventDelegate ().cbOutputChanged += value;
  316. }
  317. remove {
  318. EnsureEventDelegate ().cbOutputChanged -= value;
  319. }
  320. }
  321. }
  322. #endif
  323. }