PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/AVFoundation/AVAudioPlayer.cs

https://github.com/kjpou1/maccore
C# | 164 lines | 117 code | 24 blank | 23 comment | 14 complexity | 898482aa8cca16922980fbf481de65f0 MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright 2009, Novell, Inc.
  2. // Copyright 2010, Novell, Inc.
  3. // Copyright 2011 Xamarin Inc
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. using MonoMac.Foundation;
  25. using MonoMac.ObjCRuntime;
  26. using MonoMac.AudioToolbox;
  27. using System;
  28. namespace MonoMac.AVFoundation {
  29. [Advice ("Use AudioSettings instead")]
  30. public class AVAudioPlayerSettings {
  31. NSDictionary dict;
  32. internal AVAudioPlayerSettings (NSDictionary dictionary)
  33. {
  34. dict = dictionary;
  35. }
  36. public AudioChannelLayout AudioChannelLayout {
  37. get {
  38. var data = dict.ObjectForKey (AVAudioSettings.AVChannelLayoutKey) as NSData;
  39. if (data == null)
  40. return new AudioChannelLayout ();
  41. return AudioChannelLayout.FromHandle (data.Bytes);
  42. }
  43. }
  44. public int EncoderBitRateKey {
  45. get {
  46. var rate = dict.ObjectForKey (AVAudioSettings.AVEncoderBitRateKey) as NSNumber;
  47. return rate == null ? 0 : rate.Int32Value;
  48. }
  49. }
  50. public AudioFormatType AudioFormat {
  51. get {
  52. var ft = dict.ObjectForKey (AVAudioSettings.AVFormatIDKey) as NSNumber;
  53. return (AudioFormatType) (ft == null ? -1 : ft.Int32Value);
  54. }
  55. }
  56. public int NumberChannels {
  57. get {
  58. var n = dict.ObjectForKey (AVAudioSettings.AVNumberOfChannelsKey) as NSNumber;
  59. return n == null ? 1 : n.Int32Value;
  60. }
  61. }
  62. public float SampleRate {
  63. get {
  64. var r = dict.ObjectForKey (AVAudioSettings.AVSampleRateKey) as NSNumber;
  65. return r == null ? 0 : r.FloatValue;
  66. }
  67. }
  68. public static implicit operator NSDictionary (AVAudioPlayerSettings settings)
  69. {
  70. return settings.dict;
  71. }
  72. }
  73. public partial class AVAudioPlayer {
  74. public static AVAudioPlayer FromUrl (NSUrl url, out NSError error)
  75. {
  76. unsafe {
  77. IntPtr errhandle;
  78. IntPtr ptrtohandle = (IntPtr) (&errhandle);
  79. var ap = new AVAudioPlayer (url, ptrtohandle);
  80. if (ap.Handle == IntPtr.Zero){
  81. error = (NSError) Runtime.GetNSObject (errhandle);
  82. return null;
  83. } else
  84. error = null;
  85. return ap;
  86. }
  87. }
  88. public static AVAudioPlayer FromUrl (NSUrl url)
  89. {
  90. unsafe {
  91. var ap = new AVAudioPlayer (url, IntPtr.Zero);
  92. if (ap.Handle == IntPtr.Zero)
  93. return null;
  94. return ap;
  95. }
  96. }
  97. public static AVAudioPlayer FromData (NSData data, out NSError error)
  98. {
  99. unsafe {
  100. IntPtr errhandle;
  101. IntPtr ptrtohandle = (IntPtr) (&errhandle);
  102. var ap = new AVAudioPlayer (data, ptrtohandle);
  103. if (ap.Handle == IntPtr.Zero){
  104. error = (NSError) Runtime.GetNSObject (errhandle);
  105. return null;
  106. } else
  107. error = null;
  108. return ap;
  109. }
  110. }
  111. public static AVAudioPlayer FromData (NSData data)
  112. {
  113. unsafe {
  114. var ap = new AVAudioPlayer (data, IntPtr.Zero);
  115. if (ap.Handle == IntPtr.Zero)
  116. return null;
  117. return ap;
  118. }
  119. }
  120. [Obsolete ("This method had an invalid signature in MonoMac 1.0.3, use AVAudioPlayer.FromUrl instead")]
  121. public AVAudioPlayer (NSUrl url, NSError error) : this (url, IntPtr.Zero)
  122. {
  123. }
  124. [Obsolete ("This method had an invalid signature in MonoMac 1.0.3, use AVAudioPlayer.FromData instead")]
  125. public AVAudioPlayer (NSData data, NSError error) : this (data, IntPtr.Zero)
  126. {
  127. }
  128. [Advice ("Use SoundSettings")]
  129. public AVAudioPlayerSettings Settings {
  130. get {
  131. return new AVAudioPlayerSettings (WeakSettings);
  132. }
  133. }
  134. [Advice ("This method was incorrectly named, use PlayAtTime instead")]
  135. public bool PlayAtTimetime (double time)
  136. {
  137. return PlayAtTime (time);
  138. }
  139. }
  140. }