Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 815ffa8

Browse files
committed
Add option to set video audio to mix mode in iOS
1 parent d0e615c commit 815ffa8

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

packages/video_player/ios/Classes/VideoPlayerPlugin.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
445445
}
446446
[_players removeAllObjects];
447447
result(nil);
448+
} else if ([@"setMixWithOthers" isEqualToString:call.method]) {
449+
BOOL mixWithOthers = call.arguments;
450+
if (mixWithOthers == true) {
451+
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
452+
} else {
453+
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
454+
}
455+
result(nil);
448456
} else if ([@"create" isEqualToString:call.method]) {
449457
NSDictionary* argsMap = call.arguments;
450458
FLTFrameUpdater* frameUpdater = [[FLTFrameUpdater alloc] initWithRegistry:_registry];

packages/video_player/lib/video_player.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
149149
/// The name of the asset is given by the [dataSource] argument and must not be
150150
/// null. The [package] argument must be non-null when the asset comes from a
151151
/// package and null otherwise.
152-
VideoPlayerController.asset(this.dataSource, {this.package})
152+
VideoPlayerController.asset(this.dataSource,
153+
{this.package, this.mixWithOthers})
153154
: dataSourceType = DataSourceType.asset,
154155
super(VideoPlayerValue(duration: null));
155156

@@ -158,7 +159,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
158159
///
159160
/// The URI for the video is given by the [dataSource] argument and must not be
160161
/// null.
161-
VideoPlayerController.network(this.dataSource)
162+
VideoPlayerController.network(this.dataSource, {this.mixWithOthers})
162163
: dataSourceType = DataSourceType.network,
163164
package = null,
164165
super(VideoPlayerValue(duration: null));
@@ -167,7 +168,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
167168
///
168169
/// This will load the file from the file-URI given by:
169170
/// `'file://${file.path}'`.
170-
VideoPlayerController.file(File file)
171+
VideoPlayerController.file(File file, {this.mixWithOthers})
171172
: dataSource = 'file://${file.path}',
172173
dataSourceType = DataSourceType.file,
173174
package = null,
@@ -176,6 +177,10 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
176177
int _textureId;
177178
final String dataSource;
178179

180+
/// True if the video audio should be in mix mode (iOS only). False to stop all other
181+
/// audio playback.
182+
final bool mixWithOthers;
183+
179184
/// Describes the type of data source this [VideoPlayerController]
180185
/// is constructed with.
181186
final DataSourceType dataSourceType;
@@ -208,6 +213,11 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
208213
case DataSourceType.file:
209214
dataSourceDescription = <String, dynamic>{'uri': dataSource};
210215
}
216+
217+
if (mixWithOthers != null) {
218+
await _channel.invokeMethod('setMixWithOthers', mixWithOthers);
219+
}
220+
211221
// TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter.
212222
// https://github.com/flutter/flutter/issues/26431
213223
// ignore: strong_mode_implicit_dynamic_method
@@ -236,7 +246,9 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
236246
size: Size(map['width']?.toDouble() ?? 0.0,
237247
map['height']?.toDouble() ?? 0.0),
238248
);
239-
initializingCompleter.complete(null);
249+
if (initializingCompleter.isCompleted == false) {
250+
initializingCompleter.complete(null);
251+
}
240252
_applyLooping();
241253
_applyVolume();
242254
_applyPlayPause();

0 commit comments

Comments
 (0)