我希望在iOS应用程序中继续播放背景音乐。然而,当我进入我的应用程序时,背景音乐会被取消。我将问题隔离到AVCaptureSession的音频部分:
var audioDevice = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio);
var audioInput = AVCaptureDeviceInput.FromDevice(audioDevice);
captureSession.AddInput(audioInput);
我尝试用两种方式立即设置AudioSession:
AudioSession.Initialize();
AudioSession.Category = AudioSessionCategory.AmbientSound;
AudioSession.SetActive(true);
或者像这样:
AudioSession.Initialize();
AudioSession.Category = AudioSessionCategory.MediaPlayback;
AudioSession.OverrideCategoryMixWithOthers = true;
AudioSession.SetActive(true);
但两者都不起作用。
您是否尝试过显式创建会话对象的实例并设置MixWithOthers,如这里所述?我想Xamarin的等效物应该是
AVAudioSession session = AVAudioSession.SharedInstance();
session.SetCategory(AVAudioSessionCategory.PlayAndRecord, AVAudioSessionCategoryOptions.MixWithOthers | AVAudioSessionCategoryOptions.DefaultToSpeaker);
或者,如上面链接中所建议的,尝试在AppDelegate中设置以下内容:
AudioSession.Category = AudioSessionCategory.PlayAndRecord;
AudioSession.AudioShouldDuck = true;
AudioSession.OverrideCategoryDefaultToSpeaker = true;
然后创建这样的AVCaptureSession:
AVCaptureSession session2 = new AVCaptureSession();
session2.AutomaticallyConfiguresApplicationAudioSession = false;