Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide options to allow for customising the iOS/macOS specific session settings #101

Merged
merged 13 commits into from
Apr 23, 2024

Conversation

bijington
Copy link
Collaborator

@bijington bijington commented Mar 12, 2024

The aim of this PR is to allow developers to configure the AVAudioSession options when running on the Apple based platforms. It opens us up to being able to configure other platforms should we need also.

The result gives the following API changes/additions:

AddAudio extension method to configure the shared settings much like the options based approach in modern apps:

var builder = MauiApp.CreateBuilder();
    builder
        .UseMauiApp<App>()
	.UseSkiaSharp()
	.AddAudio(
		playbackOptions =>
		{
#if IOS || MACCATALYST
			playbackOptions.Category = AVFoundation.AVAudioSessionCategory.Playback;
#endif
		},
		recordingOptions =>
		{
#if IOS || MACCATALYST
			recordingOptions.Category = AVFoundation.AVAudioSessionCategory.Record;
			recordingOptions.Mode = AVFoundation.AVAudioSessionMode.Default;
			recordingOptions.CategoryOptions = AVFoundation.AVAudioSessionCategoryOptions.DefaultToSpeaker;
#endif
		})

This fits with developers making use of dependency injection, for those that aren't there is the following:

AudioManager.Current.SharedPlayerOptions = new
{
    Category = AVFoundation.AVAudioSessionCategory.Playback;
}

And then finally there is the ability to pass in an instance of the options class when creating a player or reader which will override these shared settings:

var player = AudioManager.Current.CreatePlayer(new
    {
        Category = AVFoundation.AVAudioSessionCategory.Playback;
    });

Fixes #91
Fixes #53
Fixes #70

@bijington
Copy link
Collaborator Author

This is a draft for now but it would be great to hear what @jfversluis and @borrmann think. And of course anyone else in the community :)

@borrmann
Copy link
Contributor

I like that approach. It makes clear how it works on iOS.
I think it's worth to consider one more parameter. Something like this:

enum SessionOptions 
{ 
  KeepSessionAlive,
  EndSession,
  EndSessionAndNotifyOthers
 }

KeepSessionAlive would be the current implementation. EndSession would end the audio session on FinishedPlaying and EndSessionAndNotifyOthers would end the audio session and additionally notify other audio players that listen to this event.
I don't know if there are similar methods available on Android or Windows.

@bijington
Copy link
Collaborator Author

Thanks. Is your suggestion aimed at determining whether we effectively call AVAudioSession.SetActive();?

@borrmann
Copy link
Contributor

yes exactly, for EndSession and EndSessionAndNotifyOthers
AVAudioSession.SharedInstance().SetActive(false);
would need to be called, maybe in void OnPlayerFinishedPlaying(object? sender, AVStatusEventArgs e) or when the audio player is disposed.
for EndSessionAndNotifyOthers, additionally, NotifyOthersOnDeactivation needs to be passed on activation.
audioSession.SetActive(true, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation)

@bijington bijington marked this pull request as ready for review March 16, 2024 00:25
@bijington
Copy link
Collaborator Author

I haven't been able to test these changes on iOS or macOS yet but I believe this API surface should be good for a review.

@bijington bijington changed the title Initial thoughts on using an options style approach Provide options to allow for customising the iOS/macOS specific session settings Mar 16, 2024
@jfversluis
Copy link
Owner

Not sure how exactly, but I guess it would be nice to surface something for this in the sample app to try out the different options as well? But for now this seems great to me!

@bijington
Copy link
Collaborator Author

Not sure how exactly, but I guess it would be nice to surface something for this in the sample app to try out the different options as well? But for now this seems great to me!

I did update the sample to use the new approach. We could add some comments in there to cover different scenarios?

@jfversluis
Copy link
Owner

Yeah what I was thinking is more some controls to switch between the options. But that can be a follow up, if it can even be done at all? Maybe not for all options, not sure.

@jfversluis jfversluis merged commit 6397cae into main Apr 23, 2024
2 checks passed
@jfversluis jfversluis deleted the feature/sl-issue-91-ios-session branch April 23, 2024 09:05
@bijington
Copy link
Collaborator Author

Yeah what I was thinking is more some controls to switch between the options. But that can be a follow up, if it can even be done at all? Maybe not for all options, not sure.

Oh yes good idea! Yes we can support that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants