Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/Plugin.Maui.Audio/AudioRecorder/AudioRecorder.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ partial class AudioRecorder : IAudioRecorder
public AudioRecorder(AudioRecorderOptions audioRecorderOptions)
{
this.audioRecorderOptions = audioRecorderOptions;

ActiveSessionHelper.FinishSession(audioRecorderOptions);
}


Expand All @@ -42,6 +40,9 @@ public Task StartAsync(string filePath, AudioRecorderOptions? options = null)
audioRecorderOptions = options;
}

// Clean up any previous recorder instance
CleanupRecorderResources();

ActiveSessionHelper.InitializeSession(audioRecorderOptions);

var url = NSUrl.FromFilename(filePath);
Expand Down Expand Up @@ -86,7 +87,12 @@ destinationFilePath is null ||

ActiveSessionHelper.FinishSession(audioRecorderOptions);

return new FileAudioSource(destinationFilePath);
var audioSource = new FileAudioSource(destinationFilePath);

// Clean up resources (event unsubscribed on line 86)
CleanupRecorderResources(unsubscribeEvent: false);

return audioSource;
}

static readonly NSObject[] keys = new NSObject[]
Expand All @@ -105,6 +111,22 @@ void Recorder_FinishedRecording(object? sender, AVStatusEventArgs e)
finishedRecordingCompletionSource?.SetResult(true);
}

void CleanupRecorderResources(bool unsubscribeEvent = true)
{
if (recorder is not null)
{
if (unsubscribeEvent)
{
recorder.FinishedRecording -= Recorder_FinishedRecording;
}
recorder.Dispose();
recorder = null;
}

destinationFilePath = null;
finishedRecordingCompletionSource = null;
}

AudioFormatType SharedEncodingToiOSEncoding(Encoding type, bool throwIfNotSupported)
{
return type switch
Expand Down