Skip to content

Commit d05215c

Browse files
authored
[AVFoundation] Improve and simplify the manually bound constructors for AVAudioRecorder slightly. (#22529)
Improve and simplify the manually bound constructors for AVAudioRecorder by using the same pattern we use elsewhere for constructors exposed as factory methods. Also add these factory methods to tvOS, since AVAudioRecorder is now available on tvOS.
1 parent ee68fea commit d05215c

5 files changed

Lines changed: 22 additions & 62 deletions

File tree

src/AVFoundation/AVAudioRecorder.cs

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -31,72 +31,41 @@
3131
#nullable enable
3232

3333
namespace AVFoundation {
34-
#if !TVOS
3534
public partial class AVAudioRecorder {
36-
AVAudioRecorder (NSUrl url, AudioSettings settings, out NSError error)
37-
{
38-
// We use this method because it allows us to out NSError but, as a side effect, it is possible for the handle to be null and we will need to check this manually (on the Create method).
39-
Handle = InitWithUrl (url, settings.Dictionary, out error);
40-
}
41-
42-
AVAudioRecorder (NSUrl url, AVAudioFormat format, out NSError error)
43-
{
44-
// We use this method because it allows us to out NSError but, as a side effect, it is possible for the handle to be null and we will need to check this manually (on the Create method).
45-
Handle = InitWithUrl (url, format, out error);
46-
}
47-
48-
/// <param name="url">To be added.</param>
49-
/// <param name="settings">To be added.</param>
50-
/// <param name="error">To be added.</param>
51-
/// <summary>Static factory method for creating an <see cref="T:AVFoundation.AVAudioRecorder" />.</summary>
52-
/// <returns>To be added.</returns>
53-
/// <remarks>To be added.</remarks>
35+
/// <summary>Create a new <see cref="AVAudioRecorder" /> instance.</summary>
36+
/// <param name="url">The url for the new <see cref="AVAudioRecorder" /> instance.</param>
37+
/// <param name="settings">The settings for the new <see cref="AVAudioRecorder" /> instance.</param>
38+
/// <param name="error">Returns the error if creating a new <see cref="AVAudioRecorder" /> instance fails.</param>
39+
/// <returns>A newly created <see cref="AVAudioRecorder" /> instance if successful, null otherwise.</returns>
5440
public static AVAudioRecorder? Create (NSUrl url, AudioSettings settings, out NSError? error)
5541
{
5642
if (settings is null)
5743
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (settings));
58-
error = null;
59-
try {
60-
AVAudioRecorder r = new AVAudioRecorder (url, settings, out error);
61-
if (r.Handle == IntPtr.Zero)
62-
return null;
63-
64-
return r;
65-
} catch {
44+
var rv = new AVAudioRecorder (NSObjectFlag.Empty);
45+
rv.InitializeHandle (rv._InitWithUrl (url, settings.Dictionary, out error), string.Empty, false);
46+
if (rv.Handle == IntPtr.Zero) {
47+
rv.Dispose ();
6648
return null;
6749
}
50+
return rv;
6851
}
6952

70-
/// <param name="url">To be added.</param>
71-
/// <param name="format">To be added.</param>
72-
/// <param name="error">To be added.</param>
73-
/// <summary>To be added.</summary>
74-
/// <returns>To be added.</returns>
75-
/// <remarks>To be added.</remarks>
76-
[SupportedOSPlatform ("ios")]
77-
[SupportedOSPlatform ("macos")]
78-
[SupportedOSPlatform ("maccatalyst")]
79-
[UnsupportedOSPlatform ("tvos")]
53+
/// <summary>Create a new <see cref="AVAudioRecorder" /> instance.</summary>
54+
/// <param name="url">The url for the new <see cref="AVAudioRecorder" /> instance.</param>
55+
/// <param name="format">The format for the new <see cref="AVAudioRecorder" /> instance.</param>
56+
/// <param name="error">Returns the error if creating a new <see cref="AVAudioRecorder" /> instance fails.</param>
57+
/// <returns>A newly created <see cref="AVAudioRecorder" /> instance if successful, null otherwise.</returns>
8058
public static AVAudioRecorder? Create (NSUrl url, AVAudioFormat? format, out NSError? error)
8159
{
8260
if (format is null)
8361
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (format));
84-
error = null;
85-
try {
86-
AVAudioRecorder r = new AVAudioRecorder (url, format, out error);
87-
if (r.Handle == IntPtr.Zero)
88-
return null;
89-
90-
return r;
91-
} catch {
62+
var rv = new AVAudioRecorder (NSObjectFlag.Empty);
63+
rv.InitializeHandle (rv._InitWithUrl (url, format, out error), string.Empty, false);
64+
if (rv.Handle == IntPtr.Zero) {
65+
rv.Dispose ();
9266
return null;
9367
}
94-
}
95-
96-
internal static AVAudioRecorder? ToUrl (NSUrl url, NSDictionary settings, out NSError? error)
97-
{
98-
return Create (url, new AudioSettings (settings), out error);
68+
return rv;
9969
}
10070
}
101-
#endif // !TVOS
10271
}

src/avfoundation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,12 +2375,12 @@ interface AVAudioPlayerNode : AVAudioMixing {
23752375
interface AVAudioRecorder {
23762376
[Export ("initWithURL:settings:error:")]
23772377
[Internal]
2378-
IntPtr InitWithUrl (NSUrl url, NSDictionary settings, out NSError error);
2378+
IntPtr _InitWithUrl (NSUrl url, NSDictionary settings, out NSError error);
23792379

23802380
[Internal]
23812381
[MacCatalyst (13, 1)]
23822382
[Export ("initWithURL:format:error:")]
2383-
IntPtr InitWithUrl (NSUrl url, AVAudioFormat format, out NSError outError);
2383+
IntPtr _InitWithUrl (NSUrl url, AVAudioFormat format, out NSError outError);
23842384

23852385
/// <summary>Prepares the recorder for efficient startup.</summary>
23862386
/// <returns>To be added.</returns>

tests/cecil-tests/ConstructorTest.KnownFailures.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public partial class ConstructorTest {
1111
"AppKit.NSImage::.ctor(Foundation.NSData,System.Boolean)",
1212
"AppKit.NSImage::.ctor(System.String,System.Boolean)",
1313
"AppKit.NSTextContainer::.ctor(CoreGraphics.CGSize,System.Boolean)",
14-
"AVFoundation.AVAudioRecorder::.ctor(Foundation.NSUrl,AVFoundation.AudioSettings,Foundation.NSError&)",
15-
"AVFoundation.AVAudioRecorder::.ctor(Foundation.NSUrl,AVFoundation.AVAudioFormat,Foundation.NSError&)",
1614
"AVFoundation.InternalAVAudioSessionDelegate::.ctor(AVFoundation.AVAudioSession)",
1715
"CarPlay.CPListSection::.ctor(CarPlay.CPListItem[],System.String,System.String)",
1816
"CarPlay.CPListSection::.ctor(CarPlay.CPListItem[])",
@@ -94,8 +92,6 @@ public partial class ConstructorTest {
9492
"AVFoundation.AVAudioFile::.ctor(Foundation.NSUrl,Foundation.NSDictionary,AVFoundation.AVAudioCommonFormat,System.Boolean,Foundation.NSError&)",
9593
"AVFoundation.AVAudioFile::.ctor(Foundation.NSUrl,Foundation.NSDictionary,Foundation.NSError&)",
9694
"AVFoundation.AVAudioFile::.ctor(Foundation.NSUrl,Foundation.NSError&)",
97-
"AVFoundation.AVAudioRecorder::.ctor(Foundation.NSUrl,AVFoundation.AudioSettings,Foundation.NSError&)",
98-
"AVFoundation.AVAudioRecorder::.ctor(Foundation.NSUrl,AVFoundation.AVAudioFormat,Foundation.NSError&)",
9995
"AVFoundation.AVCaptureDeviceInput::.ctor(AVFoundation.AVCaptureDevice,Foundation.NSError&)",
10096
"AVFoundation.AVMidiPlayer::.ctor(Foundation.NSData,Foundation.NSUrl,Foundation.NSError&)",
10197
"AVFoundation.AVMidiPlayer::.ctor(Foundation.NSUrl,Foundation.NSUrl,Foundation.NSError&)",

tests/cecil-tests/SetHandleTest.KnownFailures.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public partial class SetHandleTest {
1313
"AppKit.NSOpenGLPixelFormat::.ctor(AppKit.NSOpenGLPixelFormatAttribute[])",
1414
"AppKit.NSOpenGLPixelFormat::.ctor(System.Object[])",
1515
"AppKit.NSTextContainer::.ctor(CoreGraphics.CGSize,System.Boolean)",
16-
"AVFoundation.AVAudioRecorder::.ctor(Foundation.NSUrl,AVFoundation.AudioSettings,Foundation.NSError&)",
17-
"AVFoundation.AVAudioRecorder::.ctor(Foundation.NSUrl,AVFoundation.AVAudioFormat,Foundation.NSError&)",
1816
"CoreFoundation.CFMutableString::.ctor(CoreFoundation.CFString,System.IntPtr)",
1917
"CoreFoundation.CFMutableString::.ctor(System.String,System.IntPtr)",
2018
"CoreFoundation.CFSocket::Initialize(CoreFoundation.CFRunLoop,CoreFoundation.CFSocket/CreateSocket)",

tests/introspection/ApiSelectorTest.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,9 +1353,6 @@ protected virtual bool SkipInit (string selector, MethodBase m)
13531353
case "initWithRTFD:documentAttributes:":
13541354
case "initWithURL:options:documentAttributes:error:":
13551355
case "initWithFileURL:options:documentAttributes:error:":
1356-
// AVAudioRecorder
1357-
case "initWithURL:settings:error:":
1358-
case "initWithURL:format:error:":
13591356
// NSUrlProtectionSpace
13601357
case "initWithHost:port:protocol:realm:authenticationMethod:":
13611358
case "initWithProxyHost:port:type:realm:authenticationMethod:":

0 commit comments

Comments
 (0)