Skip to content

Commit

Permalink
iOS: set up audio session
Browse files Browse the repository at this point in the history
This allows both background audio as well as proper mixing of outputs.

Fixes #3996
  • Loading branch information
osy committed Aug 2, 2022
1 parent 327d062 commit edba4eb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions Platform/iOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<true/>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>location</string>
</array>
<key>UIFileSharingEnabled</key>
Expand Down
14 changes: 14 additions & 0 deletions Platform/iOS/VMSessionState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

import Foundation
import AVFAudio
import SwiftUI

/// Represents the UI state for a single VM session.
Expand Down Expand Up @@ -326,6 +327,13 @@ extension VMSessionState: CSUSBManagerDelegate {

extension VMSessionState {
func start() {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playAndRecord, options: [.mixWithOthers, .defaultToSpeaker, .allowBluetooth, .allowBluetoothA2DP, .allowAirPlay])
try audioSession.setActive(true)
} catch {
logger.warning("Error starting audio session: \(error.localizedDescription)")
}
Self.currentSession = self
NotificationCenter.default.post(name: .vmSessionCreated, object: nil, userInfo: ["Session": self])
vm.requestVmStart()
Expand All @@ -336,6 +344,12 @@ extension VMSessionState {
}

func stop() {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setActive(false)
} catch {
logger.warning("Error stopping audio session: \(error.localizedDescription)")
}
// tell other screens to shut down
Self.currentSession = nil
NotificationCenter.default.post(name: .vmSessionEnded, object: nil, userInfo: ["Session": self])
Expand Down
6 changes: 6 additions & 0 deletions UTM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
8471772827CD3CAB00D3A50B /* DetailedSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8471772727CD3CAB00D3A50B /* DetailedSection.swift */; };
8471772927CD3CAB00D3A50B /* DetailedSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8471772727CD3CAB00D3A50B /* DetailedSection.swift */; };
8471772A27CD3CAB00D3A50B /* DetailedSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8471772727CD3CAB00D3A50B /* DetailedSection.swift */; };
84818C0C2898A07A009EDB67 /* AVFAudio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 84818C0B2898A07A009EDB67 /* AVFAudio.framework */; };
84818C0D2898A07F009EDB67 /* AVFAudio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 84818C0B2898A07A009EDB67 /* AVFAudio.framework */; };
848308D5278A1F2200E3E474 /* Virtualization.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 848308D4278A1F2200E3E474 /* Virtualization.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
848A98B0286A0F74006F0550 /* UTMAppleConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848A98AF286A0F74006F0550 /* UTMAppleConfiguration.swift */; };
848A98B2286A0FDE006F0550 /* UTMAppleConfigurationSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848A98B1286A0FDE006F0550 /* UTMAppleConfigurationSystem.swift */; };
Expand Down Expand Up @@ -1613,6 +1615,7 @@
8471770527CC974F00D3A50B /* DefaultTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultTextField.swift; sourceTree = "<group>"; };
8471770927CCA60600D3A50B /* DefaultPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultPicker.swift; sourceTree = "<group>"; };
8471772727CD3CAB00D3A50B /* DetailedSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailedSection.swift; sourceTree = "<group>"; };
84818C0B2898A07A009EDB67 /* AVFAudio.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFAudio.framework; path = System/Library/Frameworks/AVFAudio.framework; sourceTree = SDKROOT; };
848308D4278A1F2200E3E474 /* Virtualization.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Virtualization.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Virtualization.framework; sourceTree = DEVELOPER_DIR; };
848A98AF286A0F74006F0550 /* UTMAppleConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMAppleConfiguration.swift; sourceTree = "<group>"; };
848A98B1286A0FDE006F0550 /* UTMAppleConfigurationSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMAppleConfigurationSystem.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2235,6 +2238,7 @@
CE2D934724AD46670059923A /* gstriff-1.0.0.framework in Frameworks */,
B329049C270FE136002707AC /* AltKit in Frameworks */,
CEA9058F25FC6A1400801E7C /* usbredirhost.1.framework in Frameworks */,
84818C0C2898A07A009EDB67 /* AVFAudio.framework in Frameworks */,
CE2D934924AD46670059923A /* gstreamer-1.0.0.framework in Frameworks */,
CE2D934B24AD46670059923A /* json-glib-1.0.0.framework in Frameworks */,
CE2D934C24AD46670059923A /* ffi.7.framework in Frameworks */,
Expand Down Expand Up @@ -2365,6 +2369,7 @@
CEA45F29263519B5002FA97D /* IQKeyboardManagerSwift in Frameworks */,
CEA45F2A263519B5002FA97D /* MetalKit.framework in Frameworks */,
84CF5DF3288E433F00D01721 /* SwiftUIVisualEffects in Frameworks */,
84818C0D2898A07F009EDB67 /* AVFAudio.framework in Frameworks */,
CEA45F2B263519B5002FA97D /* libgstvolume.a in Frameworks */,
CEA45F2C263519B5002FA97D /* libgstcoreelements.a in Frameworks */,
CEA45F2D263519B5002FA97D /* libgstvideorate.a in Frameworks */,
Expand Down Expand Up @@ -2477,6 +2482,7 @@
CE2D63D622653C7300FC7E63 /* Frameworks */ = {
isa = PBXGroup;
children = (
84818C0B2898A07A009EDB67 /* AVFAudio.framework */,
84937EFE28960789003148F4 /* zstd.1.framework */,
848308D4278A1F2200E3E474 /* Virtualization.framework */,
8453DCB0278CE33E0037A0DA /* qemu-img.framework */,
Expand Down

0 comments on commit edba4eb

Please sign in to comment.