mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
In-keyboard dictation records through WebKit (getUserMedia), whose audio session lives in WebKit's GPU process. WebKit sets it to .playAndRecord in .videoChat mode without .mixWithOthers (AudioSessionIOS::setCategory) and deactivates it with setActive:NO withOptions:0 (AudioSessionCocoa), and neither the page (navigator.audioSession only picks the category) nor a WKWebView setting changes that. So it interrupts another app's video or music when the capture starts and never tells it it may resume, which is the report from TestFlight build 56. The keyboard's own setCategory with .mixWithOthers and its setActive(false, .notifyOthersOnDeactivation) acted on the keyboard process's session, which never went active, so they did nothing for this. A native capture in the keyboard isn't an option: iOS refuses recording to extensions. Now the keyboard notes whether other audio was playing as a capture began (isOtherAudioPlaying). When the capture ends (done, cancel, discard, failure), 0.5 s later, once WebKit has let its session go and unless a new capture is running, if nothing plays any more it activates its own .playback session (nothing else plays, so it interrupts nothing) and deactivates it with .notifyOthersOnDeactivation, off the main thread, for players that resume on .shouldResume (InterruptedAudio). The video still pauses while in-keyboard dictation records; only Hex's own recording (the app and a warm session) can keep it playing. README and the start(...) comment no longer say in-keyboard capture mixes. Verified on an iOS 27.2 iPhone 17 Pro Max simulator on the Mac Studio, in Safari with a looping MP4 playing: WebKit's log shows the keyboard's GPU process setting PlayAndRecord/VideoChat and going active at the Hex key, then AmbientSound and inactive at Done; with the other-audio check forced on in a throwaway build, "asked the audio the capture interrupted to resume" followed 0.5 s after WebKit's deactivation and dictation was unaffected. The simulator has no inter-app interruptions and reports no other audio playing, so the video never paused there before or after, and the real build skips the request there. Whether iOS passes the resume on to a player WebKit interrupted needs an iPhone. xcodebuild test: 467 tests, 0 failures, including testTheKeyboardAsksAudioItsCaptureStoppedToResume.
40 lines
2 KiB
Swift
40 lines
2 KiB
Swift
import AVFAudio
|
|
|
|
/// The `AVAudioSession` calls Hex makes, so tests can check how it records and lets go.
|
|
protocol AudioSessionControlling: AnyObject {
|
|
var isOtherAudioPlaying: Bool { get }
|
|
var availableInputs: [AVAudioSessionPortDescription]? { get }
|
|
func setCategory(
|
|
_ category: AVAudioSession.Category,
|
|
mode: AVAudioSession.Mode,
|
|
options: AVAudioSession.CategoryOptions
|
|
) throws
|
|
func setPreferredSampleRate(_ sampleRate: Double) throws
|
|
func setPreferredInput(_ inPort: AVAudioSessionPortDescription?) throws
|
|
func setActive(_ active: Bool, options: AVAudioSession.SetActiveOptions) throws
|
|
}
|
|
|
|
extension AVAudioSession: AudioSessionControlling {}
|
|
|
|
/// Lets other apps' audio that in-keyboard dictation interrupted play again.
|
|
///
|
|
/// The keyboard records through WebKit (`getUserMedia`), whose audio session lives in
|
|
/// WebKit's GPU process: WebKit sets `.playAndRecord` in `.videoChat` mode without
|
|
/// `.mixWithOthers`, and no page or app setting changes that. Going active, it interrupts
|
|
/// another app's video or music; ending, it deactivates without `.notifyOthersOnDeactivation`,
|
|
/// so the player is never told it may resume. When other audio played as the capture began
|
|
/// and nothing plays once it has ended, the keyboard activates its own session (nothing else
|
|
/// plays, so it interrupts nothing) and deactivates it with `.notifyOthersOnDeactivation`,
|
|
/// for players that honour `.shouldResume`. Whether iOS passes that on to a player WebKit
|
|
/// interrupted can only be seen on an iPhone: the simulator has no interruptions.
|
|
nonisolated enum InterruptedAudio {
|
|
static func shouldAskToResume(playingAtStart: Bool, playingNow: Bool) -> Bool {
|
|
playingAtStart && !playingNow
|
|
}
|
|
|
|
static func askToResume(_ session: AudioSessionControlling) throws {
|
|
try session.setCategory(.playback, mode: .default, options: [])
|
|
try session.setActive(true, options: [])
|
|
try session.setActive(false, options: .notifyOthersOnDeactivation)
|
|
}
|
|
}
|