-
-
Notifications
You must be signed in to change notification settings - Fork 890
Expand file tree
/
Copy pathaudio_context.dart
More file actions
476 lines (412 loc) · 15.7 KB
/
Copy pathaudio_context.dart
File metadata and controls
476 lines (412 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import 'package:flutter/foundation.dart';
/// An Audio Context is a set of secondary, platform-specific aspects of audio
/// playback, typically related to how the act of playing audio interacts with
/// other features of the device. [AudioContext] is containing platform specific
/// configurations: [AudioContextAndroid] and [AudioContextIOS].
class AudioContext {
final AudioContextAndroid android;
final AudioContextIOS iOS;
const AudioContext({
this.android = const AudioContextAndroid(),
this.iOS = const AudioContextIOS(),
});
AudioContext copy({
AudioContextAndroid? android,
AudioContextIOS? iOS,
}) {
return AudioContext(
android: android ?? this.android,
iOS: iOS ?? this.iOS,
);
}
Map<String, dynamic> toJson() {
// we need to check web first because `defaultTargetPlatform` is not
// available for web.
if (kIsWeb) {
return <String, dynamic>{};
} else if (defaultTargetPlatform == TargetPlatform.android) {
return android.toJson();
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return iOS.toJson();
} else {
return <String, dynamic>{};
}
}
}
/// A platform-specific class to encapsulate a collection of attributes about an
/// Android audio stream.
class AudioContextAndroid {
/// Sets the speakerphone on or off, globally.
///
/// This method should only be used by applications that replace the
/// platform-wide management of audio settings or the main telephony
/// application.
final bool isSpeakerphoneOn;
/// Sets the audio mode, globally.
///
/// This method should only be used by applications that replace the
/// platform-wide management of audio settings or the main telephony
/// application, see [AndroidAudioMode].
final AndroidAudioMode audioMode;
final bool stayAwake;
final AndroidContentType contentType;
final AndroidUsageType usageType;
final AndroidAudioFocus? audioFocus;
const AudioContextAndroid({
this.isSpeakerphoneOn = true,
this.audioMode = AndroidAudioMode.normal,
this.stayAwake = true,
this.contentType = AndroidContentType.music,
this.usageType = AndroidUsageType.media,
this.audioFocus = AndroidAudioFocus.gain,
});
AudioContextAndroid copy({
bool? isSpeakerphoneOn,
AndroidAudioMode? audioMode,
bool? stayAwake,
AndroidContentType? contentType,
AndroidUsageType? usageType,
AndroidAudioFocus? audioFocus,
}) {
return AudioContextAndroid(
isSpeakerphoneOn: isSpeakerphoneOn ?? this.isSpeakerphoneOn,
audioMode: audioMode ?? this.audioMode,
stayAwake: stayAwake ?? this.stayAwake,
contentType: contentType ?? this.contentType,
usageType: usageType ?? this.usageType,
audioFocus: audioFocus ?? this.audioFocus,
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'isSpeakerphoneOn': isSpeakerphoneOn,
'audioMode': audioMode.value,
'stayAwake': stayAwake,
'contentType': contentType.value,
'usageType': usageType.value,
'audioFocus': audioFocus?.value,
};
}
}
/// A platform-specific class to encapsulate a collection of attributes about an
/// iOS audio stream.
class AudioContextIOS {
final AVAudioSessionCategory category;
final List<AVAudioSessionOptions> options;
const AudioContextIOS({
this.category = AVAudioSessionCategory.playback,
this.options = const [
AVAudioSessionOptions.mixWithOthers,
AVAudioSessionOptions.defaultToSpeaker
],
});
AudioContextIOS copy({
AVAudioSessionCategory? category,
List<AVAudioSessionOptions>? options,
}) {
return AudioContextIOS(
category: category ?? this.category,
options: options ?? this.options,
);
}
Map<String, dynamic> toJson() {
return <String, dynamic>{
'category': category.name,
'options': options.map((e) => e.name).toList(),
};
}
}
/// "what" you are playing. The content type expresses the general category of
/// the content. This information is optional. But in case it is known (for
/// instance [movie] for a movie streaming service or [music] for a music
/// playback application) this information might be used by the audio framework
/// to selectively configure some audio post-processing blocks.
enum AndroidContentType {
/// Content type value to use when the content type is unknown, or other than
/// the ones defined.
unknown,
/// Content type value to use when the content type is speech.
speech,
/// Content type value to use when the content type is music.
music,
/// Content type value to use when the content type is a soundtrack, typically
/// accompanying a movie or TV program.
movie,
/// Content type value to use when the content type is a sound used to
/// accompany a user action, such as a beep or sound effect expressing a key
/// click, or event, such as the type of a sound for a bonus being received in
/// a game. These sounds are mostly synthesized or short Foley sounds.
sonification,
}
extension AndroidContentTypeValue on AndroidContentType {
int get value {
switch (this) {
case AndroidContentType.unknown:
return 0;
case AndroidContentType.speech:
return 1;
case AndroidContentType.music:
return 2;
case AndroidContentType.movie:
return 3;
case AndroidContentType.sonification:
return 4;
}
}
}
/// "why" you are playing a sound, what is this sound used for. This is achieved
/// with the "usage" information. Examples of usage are [media] and [alarm].
/// These two examples are the closest to stream types, but more detailed use
/// cases are available. Usage information is more expressive than a stream
/// type, and allows certain platforms or routing policies to use this
/// information for more refined volume or routing decisions. Usage is the most
/// important information to supply in [AudioContextAndroid] and it is
/// recommended to build any instance with this information supplied.
enum AndroidUsageType {
/// Usage value to use when the usage is unknown.
unknown,
/// Usage value to use when the usage is media, such as music, or movie
/// soundtracks.
media,
/// Usage value to use when the usage is voice communications, such as
/// telephony or VoIP.
voiceCommunication,
/// Usage value to use when the usage is in-call signalling, such as with a
/// "busy" beep, or DTMF tones.
voiceCommunicationSignalling,
/// Usage value to use when the usage is an alarm (e.g. wake-up alarm).
alarm,
/// Usage value to use when the usage is notification. See other notification
/// usages for more specialized uses.
notification,
/// Usage value to use when the usage is telephony ringtone.
notificationRingtone,
/// Usage value to use when the usage is a request to enter/end a
/// communication, such as a VoIP communication or video-conference.
notificationCommunicationRequest,
/// Usage value to use when the usage is notification for an "instant"
/// communication such as a chat, or SMS.
notificationCommunicationInstant,
/// Usage value to use when the usage is notification for a non-immediate type
/// of communication such as e-mail.
notificationCommunicationDelayed,
/// Usage value to use when the usage is to attract the user's attention, such
/// as a reminder or low battery warning.
notificationEvent,
/// Usage value to use when the usage is for accessibility, such as with a
/// screen reader.
assistanceAccessibility,
/// Usage value to use when the usage is driving or navigation directions.
assistanceNavigationGuidance,
/// Usage value to use when the usage is sonification, such as with user
/// interface sounds.
assistanceSonification,
/// Usage value to use when the usage is for game audio.
game,
/// @hide
///
/// Usage value to use when feeding audio to the platform and replacing
/// "traditional" audio source, such as audio capture devices.
virtualSource,
/// Usage value to use for audio responses to user queries, audio instructions
/// or help utterances.
assistant,
}
extension AndroidUsageTypeValue on AndroidUsageType {
int get value {
switch (this) {
case AndroidUsageType.unknown:
return 0;
case AndroidUsageType.media:
return 1;
case AndroidUsageType.voiceCommunication:
return 2;
case AndroidUsageType.voiceCommunicationSignalling:
return 3;
case AndroidUsageType.alarm:
return 4;
case AndroidUsageType.notification:
return 5;
case AndroidUsageType.notificationRingtone:
return 6;
case AndroidUsageType.notificationCommunicationRequest:
return 7;
case AndroidUsageType.notificationCommunicationInstant:
return 8;
case AndroidUsageType.notificationCommunicationDelayed:
return 9;
case AndroidUsageType.notificationEvent:
return 10;
case AndroidUsageType.assistanceAccessibility:
return 11;
case AndroidUsageType.assistanceNavigationGuidance:
return 12;
case AndroidUsageType.assistanceSonification:
return 13;
case AndroidUsageType.game:
return 14;
case AndroidUsageType.virtualSource:
return 15;
case AndroidUsageType.assistant:
return 16;
}
}
}
enum AndroidAudioFocus {
/// Used to indicate no audio focus has been gained or lost, or requested.
none,
/// Used to indicate a gain of audio focus, or a request of audio focus, of
/// unknown duration.
///
/// @see OnAudioFocusChangeListener#onAudioFocusChange(int)
/// @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
gain,
/// Used to indicate a temporary gain or request of audio focus, anticipated
/// to last a short amount of time. Examples of temporary changes are the
/// playback of driving directions, or an event notification.
///
/// @see OnAudioFocusChangeListener#onAudioFocusChange(int)
/// @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
gainTransient,
/// Used to indicate a temporary request of audio focus, anticipated to last a
/// short amount of time, and where it is acceptable for other audio
/// applications to keep playing after having lowered their output level
/// (also referred to as "ducking").
/// Examples of temporary changes are the playback of driving directions where
/// playback of music in the background is acceptable.
///
/// @see OnAudioFocusChangeListener#onAudioFocusChange(int)
/// @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
gainTransientMayDuck,
/// Used to indicate a temporary request of audio focus, anticipated to last a
/// short amount of time, during which no other applications, or system
/// components, should play anything. Examples of exclusive and transient
/// audio focus requests are voice memo recording and speech recognition,
/// during which the system shouldn't play any notifications, and media
/// playback should have paused.
///
/// @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
gainTransientExclusive,
}
extension AndroidAudioFocusValue on AndroidAudioFocus {
int get value {
switch (this) {
case AndroidAudioFocus.none:
return 0;
case AndroidAudioFocus.gain:
return 1;
case AndroidAudioFocus.gainTransient:
return 2;
case AndroidAudioFocus.gainTransientMayDuck:
return 3;
case AndroidAudioFocus.gainTransientExclusive:
return 4;
}
}
}
/// The audio mode encompasses audio routing AND the behavior of the telephony
/// layer. Therefore this flag should only be used by applications that
/// replace the platform-wide management of audio settings or the main telephony
/// application. In particular, the [inCall] mode should only be used by the
/// telephony application when it places a phone call, as it will cause signals
/// from the radio layer to feed the platform mixer.
enum AndroidAudioMode {
/// Normal audio mode: not ringing and no call established.
normal,
/// Ringing audio mode. An incoming is being signaled.
ringtone,
/// In call audio mode. A telephony call is established.
inCall,
/// In communication audio mode. An audio/video chat or VoIP call is established.
inCommunication,
/// Call screening in progress. Call is connected and audio is accessible to
/// call screening applications but other audio use cases are still possible.
callScreening
}
extension AndroidAudioModeValue on AndroidAudioMode {
int get value {
switch (this) {
case AndroidAudioMode.normal:
return 0;
case AndroidAudioMode.ringtone:
return 1;
case AndroidAudioMode.inCall:
return 2;
case AndroidAudioMode.inCommunication:
return 3;
case AndroidAudioMode.callScreening:
return 4;
}
}
}
/// This is a Dart representation of the equivalent enum on Swift.
///
/// Audio session category identifiers.
/// An audio session category defines a set of audio behaviors.
/// Choose a category that most accurately describes the audio behavior you
/// require.
enum AVAudioSessionCategory {
/// Silenced by the Ring/Silent switch and by screen locking = Yes
/// Interrupts nonmixable app’s audio = No
/// Output only
ambient,
/// Silenced by the Ring/Silent switch and by screen locking = Yes
/// Interrupts nonmixable app’s audio = Yes
/// Output only
/// This is the platform's default (not AP's default witch is playAndRecord).
soloAmbient,
/// Silenced by the Ring/Silent switch and by screen locking = No
/// Interrupts nonmixable app’s audio = Yes by default; no by using override
/// switch.
/// Note: the switch is the `.mixWithOthers` option
/// (+ other options like `.duckOthers`).
/// Output only
playback,
/// Silenced by the Ring/Silent switch and by screen locking = No (recording
/// continues with screen locked)
/// Interrupts nonmixable app’s audio = Yes
/// Input only
record,
/// Silenced by the Ring/Silent switch and by screen locking = No
/// Interrupts nonmixable app’s audio = Yes by default; no by using override
/// switch.
/// Note: the switch is the `.mixWithOthers` option
/// (+ other options like `.duckOthers`).
/// Input and output
playAndRecord,
/// Silenced by the Ring/Silent switch and by screen locking = No
/// Interrupts nonmixable app’s audio = Yes
/// Input and output
multiRoute,
}
/// This is a Dart representation of the equivalent enum on Swift.
///
/// Constants that specify optional audio behaviors. Each option is valid only
/// for specific audio session categories.
enum AVAudioSessionOptions {
/// An option that indicates whether audio from this session mixes with audio
/// from active sessions in other audio apps.
mixWithOthers,
/// An option that reduces the volume of other audio sessions while audio from
/// this session plays.
duckOthers,
/// An option that determines whether to pause spoken audio content from other
/// sessions when your app plays its audio.
interruptSpokenAudioAndMixWithOthers,
/// An option that determines whether Bluetooth hands-free devices appear as
/// available input routes.
allowBluetooth,
/// An option that determines whether you can stream audio from this session
/// to Bluetooth devices that support the Advanced Audio Distribution Profile
/// (A2DP).
allowBluetoothA2DP,
/// An option that determines whether you can stream audio from this session
/// to AirPlay devices.
allowAirPlay,
/// An option that determines whether audio from the session defaults to the
/// built-in speaker instead of the receiver.
defaultToSpeaker,
/// An option that indicates whether the system interrupts the audio session
/// when it mutes the built-in microphone.
overrideMutedMicrophoneInterruption,
}