dotnet/maui

iOS TextToSpeech(TTS) stops background music

Open

#26,652 opened on 2024年12月16日

GitHub で見る
 (2 comments) (1 reaction) (0 assignees)C# (23,245 stars) (1,951 forks)batch import
area-essentialsgood first issueplatform/iosproposal/open

説明

Description

I use built-in TTS in our fitness app to give users better experience during working out. On Android everything works correctly but on iOS running TTS stops user's music.

From what I have found, iOS requires: AVFoundation.AVAudioSessionCategoryOptions.MixWithOthers Can you add something like this into SpeechOptions ?

Steps to Reproduce

  1. Start music player or radio on your iPhone
  2. From your maui app call await TextToSpeech.Default.SpeakAsync("this text will stop music in background");
  3. Music stops

Link to public reproduction project repository

No response

Version with bug

9.0.21 SR2.1

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

In Platforms->iOS create AudioSessionManager with:

using AVFoundation;
using Foundation;

public class AudioSessionManager
{
	public static void ConfigureAudioSession()
	{
		var audioSession = AVAudioSession.SharedInstance();
		NSError error;
		audioSession.SetCategory(AVAudioSessionCategory.Playback, AVAudioSessionCategoryOptions.MixWithOthers, out error);
		audioSession.SetActive(true, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation, out error);
	}
}

and initialize it in AppDelegate. In my case I did it in FinishedLaunching like:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
	AudioSessionManager.ConfigureAudioSession();

	return base.FinishedLaunching(application, launchOptions);
}

コントリビューターガイド

iOS TextToSpeech(TTS) stops background music · dotnet/maui#26652 | Good First Issue