Skip to content

Commit

Permalink
[AudioToolbox] Make P/Invokes in SystemSound.cs have blittable signat…
Browse files Browse the repository at this point in the history
…ures. (#19677)

Contributes towards #15684.
  • Loading branch information
rolfbjarne authored Jan 2, 2024
1 parent 36cc531 commit 3398a0b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/AudioToolbox/SystemSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,20 @@ public Task PlaySystemSoundAsync ()
unsafe static extern void AudioServicesPlaySystemSoundWithCompletion (uint inSystemSoundID, BlockLiteral* inCompletionBlock);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AudioServicesError AudioServicesCreateSystemSoundID (IntPtr fileUrl, out uint soundId);
unsafe static extern AudioServicesError AudioServicesCreateSystemSoundID (IntPtr fileUrl, uint* soundId);

static uint Create (NSUrl fileUrl)
{
AudioServicesError error;
uint soundId;

if (fileUrl is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (fileUrl));

var error = AudioServicesCreateSystemSoundID (fileUrl.Handle, out var soundId);
unsafe {
error = AudioServicesCreateSystemSoundID (fileUrl.Handle, &soundId);
}

if (error != AudioServicesError.None)
throw new InvalidOperationException (string.Format ("Could not create system sound ID for url {0}; error={1}",
fileUrl, error));
Expand All @@ -312,22 +318,32 @@ public SystemSound (NSUrl fileUrl)

public static SystemSound? FromFile (NSUrl fileUrl)
{
AudioServicesError error;
uint soundId;

if (fileUrl is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (fileUrl));

var error = AudioServicesCreateSystemSoundID (fileUrl.Handle, out var soundId);
unsafe {
error = AudioServicesCreateSystemSoundID (fileUrl.Handle, &soundId);
}
if (error != AudioServicesError.None)
return null;
return new SystemSound (soundId, true);
}

public static SystemSound? FromFile (string filename)
{
AudioServicesError error;
uint soundId;

if (filename is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (filename));

using (var url = new NSUrl (filename)) {
var error = AudioServicesCreateSystemSoundID (url.Handle, out var soundId);
unsafe {
error = AudioServicesCreateSystemSoundID (url.Handle, &soundId);
}
if (error != AudioServicesError.None)
return null;
return new SystemSound (soundId, true);
Expand Down
1 change: 0 additions & 1 deletion tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public partial class BlittablePInvokes {
"AudioToolbox.AudioQueueStatus AudioToolbox.OutputAudioQueue::AudioQueueSetOfflineRenderFormat(System.IntPtr,AudioToolbox.AudioStreamBasicDescription&,System.IntPtr)",
"AudioToolbox.AudioServicesError AudioToolbox.AudioServices::AudioServicesGetProperty(AudioToolbox.AudioServicesPropertyKey,System.UInt32,System.UInt32&,System.UInt32&,System.UInt32&)",
"AudioToolbox.AudioServicesError AudioToolbox.AudioServices::AudioServicesSetProperty(AudioToolbox.AudioServicesPropertyKey,System.UInt32,System.UInt32&,System.UInt32,System.UInt32&)",
"AudioToolbox.AudioServicesError AudioToolbox.SystemSound::AudioServicesCreateSystemSoundID(System.IntPtr,System.UInt32&)",
"AudioToolbox.MusicPlayerStatus AudioToolbox.MusicPlayer::MusicPlayerGetBeatsForHostTime(System.IntPtr,System.Int64,System.Double&)",
"AudioToolbox.MusicPlayerStatus AudioToolbox.MusicPlayer::MusicPlayerGetHostTimeForBeats(System.IntPtr,System.Double,System.Int64&)",
"AudioToolbox.MusicPlayerStatus AudioToolbox.MusicPlayer::MusicPlayerGetPlayRateScalar(System.IntPtr,System.Double&)",
Expand Down

6 comments on commit 3398a0b

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.