Skip to content

Commit

Permalink
ios: fix AudioSessionCategory reset to ambient #76
Browse files Browse the repository at this point in the history
  • Loading branch information
yosemiteyss committed Apr 18, 2024
1 parent 2cd914c commit c4956f0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
43 changes: 43 additions & 0 deletions example/integration_test/main_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,49 @@ void main() {

expect(actual, targets);
});

if (Platform.isIOS) {
testWidgets(
'should keeps iOS AudioSessionCategory the same after get volume',
(tester) async {
await FlutterVolumeController.setIOSAudioSessionCategory(
category: AudioSessionCategory.playback);
final before = await FlutterVolumeController.getIOSAudioSessionCategory();

await FlutterVolumeController.getVolume();
final after = await FlutterVolumeController.getIOSAudioSessionCategory();

expect(after, before);
});
}

if (Platform.isIOS) {
testWidgets('should keeps iOS AudioSessionCategory the same after get mute',
(tester) async {
await FlutterVolumeController.setIOSAudioSessionCategory(
category: AudioSessionCategory.playback);
final before = await FlutterVolumeController.getIOSAudioSessionCategory();

await FlutterVolumeController.getMute();
final after = await FlutterVolumeController.getIOSAudioSessionCategory();

expect(after, before);
});
}

if (Platform.isIOS) {
testWidgets('should keeps iOS AudioSessionCategory the same after set mute',
(tester) async {
await FlutterVolumeController.setIOSAudioSessionCategory(
category: AudioSessionCategory.playback);
final before = await FlutterVolumeController.getIOSAudioSessionCategory();

await FlutterVolumeController.setMute(true);
final after = await FlutterVolumeController.getIOSAudioSessionCategory();

expect(after, before);
});
}
}

Future<void> _insertDelay() async {
Expand Down
6 changes: 3 additions & 3 deletions ios/Classes/VolumeController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class VolumeController {
}

func getVolume() throws -> Float {
try audioSession.activate(with: VolumeController.defaultCategory)
try resumeAudioSession()
let volume = audioSession.outputVolume
return volume
}
Expand All @@ -45,12 +45,12 @@ class VolumeController {
}

func getMute() throws -> Bool {
try audioSession.activate(with: VolumeController.defaultCategory)
try resumeAudioSession()
return audioSession.outputVolume == 0
}

func setMute(_ isMuted: Bool, showSystemUI: Bool) throws {
try audioSession.activate(with: VolumeController.defaultCategory)
try resumeAudioSession()

// Save current volume level before mute.
if isMuted {
Expand Down

0 comments on commit c4956f0

Please sign in to comment.