Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: remove unused defaultToSpeaker in AudioContextIOS and replace with AVAudioSessionOptions.defaultToSpeaker #1374

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/audioplayers/example/lib/tabs/audio_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class _AudioContextTabState extends State<AudioContextTab>
final i = config.buildIOS();
return Column(
children: [
Text('defaultToSpeaker: ${i.defaultToSpeaker}'),
Text('category: ${i.category}'),
Text('options: ${i.options}'),
],
Expand Down
14 changes: 2 additions & 12 deletions packages/audioplayers_darwin/ios/Classes/AudioContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ import MediaPlayer
struct AudioContext {
let category: AVAudioSession.Category
let options: [AVAudioSession.CategoryOptions]
let defaultToSpeaker: Bool

init() {
self.category = .playAndRecord
self.options = [.mixWithOthers]
self.defaultToSpeaker = false
}

init(
category: AVAudioSession.Category,
options: [AVAudioSession.CategoryOptions],
defaultToSpeaker: Bool
options: [AVAudioSession.CategoryOptions]
) {
self.category = category
self.options = options
self.defaultToSpeaker = defaultToSpeaker
}

func activateAudioSession(
Expand Down Expand Up @@ -60,15 +56,9 @@ struct AudioContext {
return nil
}

guard let defaultToSpeaker = args["defaultToSpeaker"] as! Bool? else {
Logger.error("Null value received for defaultToSpeaker")
return nil
}

return AudioContext(
category: category,
options: options,
defaultToSpeaker: defaultToSpeaker
options: options
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class AudioContextConfig {
return AudioContextAndroid(
isSpeakerphoneOn: forceSpeaker,
stayAwake: stayAwake,
contentType: AndroidContentType.music,
usageType: respectSilence
? AndroidUsageType.notificationRingtone
: AndroidUsageType.media,
Expand All @@ -118,12 +117,12 @@ class AudioContextConfig {
validateIOS();
}
return AudioContextIOS(
defaultToSpeaker: forceSpeaker,
category: respectSilence
? AVAudioSessionCategory.ambient
: AVAudioSessionCategory.playback,
options: [AVAudioSessionOptions.mixWithOthers] +
(duckAudio ? [AVAudioSessionOptions.duckOthers] : []),
(duckAudio ? [AVAudioSessionOptions.duckOthers] : []) +
(forceSpeaker ? [AVAudioSessionOptions.defaultToSpeaker] : []),
);
}

Expand All @@ -141,9 +140,9 @@ class AudioContext {
final AudioContextAndroid android;
final AudioContextIOS iOS;

AudioContext({
required this.android,
required this.iOS,
const AudioContext({
this.android = const AudioContextAndroid(),
this.iOS = const AudioContextIOS(),
});

AudioContext copy({
Expand Down Expand Up @@ -178,12 +177,12 @@ class AudioContextAndroid {
final AndroidUsageType usageType;
final AndroidAudioFocus? audioFocus;

AudioContextAndroid({
required this.isSpeakerphoneOn,
required this.stayAwake,
required this.contentType,
required this.usageType,
required this.audioFocus,
const AudioContextAndroid({
this.isSpeakerphoneOn = true,
this.stayAwake = true,
this.contentType = AndroidContentType.music,
this.usageType = AndroidUsageType.media,
this.audioFocus = AndroidAudioFocus.gain,
});

AudioContextAndroid copy({
Expand Down Expand Up @@ -214,31 +213,29 @@ class AudioContextAndroid {
}

class AudioContextIOS {
final bool defaultToSpeaker;
final AVAudioSessionCategory category;
final List<AVAudioSessionOptions> options;

AudioContextIOS({
required this.defaultToSpeaker,
required this.category,
required this.options,
const AudioContextIOS({
this.category = AVAudioSessionCategory.playback,
this.options = const [
AVAudioSessionOptions.mixWithOthers,
AVAudioSessionOptions.defaultToSpeaker
],
});

AudioContextIOS copy({
bool? defaultToSpeaker,
AVAudioSessionCategory? category,
List<AVAudioSessionOptions>? options,
}) {
return AudioContextIOS(
defaultToSpeaker: defaultToSpeaker ?? this.defaultToSpeaker,
category: category ?? this.category,
options: options ?? this.options,
);
}

Map<String, dynamic> toJson() {
return <String, dynamic>{
'defaultToSpeaker': defaultToSpeaker,
'category': category.name,
'options': options.map((e) => e.name).toList(),
};
Expand Down