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

Commit d45c8ec

Browse files
committed
[video_player] Set audio mix options
1 parent b0ddb8e commit d45c8ec

File tree

13 files changed

+103
-14
lines changed

13 files changed

+103
-14
lines changed

packages/video_player/video_player/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.10.6
2+
3+
* Introduce VideoPlayerOptions to set the audio mix mode.
4+
15
## 0.10.5
26

37
* Support `web` by default.

packages/video_player/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,19 @@ final class VideoPlayer {
5757

5858
private boolean isInitialized = false;
5959

60+
private final VideoPlayerOptions options;
61+
6062
VideoPlayer(
6163
Context context,
6264
EventChannel eventChannel,
6365
TextureRegistry.SurfaceTextureEntry textureEntry,
6466
String dataSource,
6567
Result result,
66-
String formatHint) {
68+
String formatHint,
69+
VideoPlayerOptions options) {
6770
this.eventChannel = eventChannel;
6871
this.textureEntry = textureEntry;
72+
this.options = options;
6973

7074
TrackSelector trackSelector = new DefaultTrackSelector();
7175
exoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
@@ -165,7 +169,7 @@ public void onCancel(Object o) {
165169

166170
surface = new Surface(textureEntry.surfaceTexture());
167171
exoPlayer.setVideoSurface(surface);
168-
setAudioAttributes(exoPlayer);
172+
setAudioAttributes(exoPlayer, options.mixWithOthers);
169173

170174
exoPlayer.addListener(
171175
new EventListener() {
@@ -209,10 +213,10 @@ void sendBufferingUpdate() {
209213
}
210214

211215
@SuppressWarnings("deprecation")
212-
private static void setAudioAttributes(SimpleExoPlayer exoPlayer) {
216+
private static void setAudioAttributes(SimpleExoPlayer exoPlayer, boolean isMixMode) {
213217
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
214218
exoPlayer.setAudioAttributes(
215-
new AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MOVIE).build());
219+
new AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MOVIE).build(), !isMixMode);
216220
} else {
217221
exoPlayer.setAudioStreamType(C.STREAM_TYPE_MUSIC);
218222
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.flutter.plugins.videoplayer;
2+
3+
public class VideoPlayerOptions {
4+
public boolean mixWithOthers;
5+
}

packages/video_player/video_player/android/src/main/java/io/flutter/plugins/videoplayer/VideoPlayerPlugin.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class VideoPlayerPlugin implements MethodCallHandler, FlutterPlugin {
2323
private static final String TAG = "VideoPlayerPlugin";
2424
private final LongSparseArray<VideoPlayer> videoPlayers = new LongSparseArray<>();
2525
private FlutterState flutterState;
26+
private VideoPlayerOptions options = new VideoPlayerOptions();
2627

2728
/** Register this with the v2 embedding for the plugin to respond to lifecycle callbacks. */
2829
public VideoPlayerPlugin() {}
@@ -95,6 +96,10 @@ public void onMethodCall(MethodCall call, Result result) {
9596
case "init":
9697
disposeAllPlayers();
9798
break;
99+
case "setMixWithOthers":
100+
options.mixWithOthers = (boolean) call.arguments;
101+
result.success(null);
102+
break;
98103
case "create":
99104
{
100105
TextureRegistry.SurfaceTextureEntry handle =
@@ -120,7 +125,8 @@ public void onMethodCall(MethodCall call, Result result) {
120125
handle,
121126
"asset:///" + assetLookupKey,
122127
result,
123-
null);
128+
null,
129+
options);
124130
videoPlayers.put(handle.id(), player);
125131
} else {
126132
player =
@@ -130,7 +136,8 @@ public void onMethodCall(MethodCall call, Result result) {
130136
handle,
131137
call.argument("uri"),
132138
result,
133-
call.argument("formatHint"));
139+
call.argument("formatHint"),
140+
options);
134141
videoPlayers.put(handle.id(), player);
135142
}
136143
break;

packages/video_player/video_player/example/lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ abstract class _PlayerLifeCycleState extends State<PlayerLifeCycle> {
231231
class _NetworkPlayerLifeCycleState extends _PlayerLifeCycleState {
232232
@override
233233
VideoPlayerController createVideoPlayerController() {
234-
return VideoPlayerController.network(widget.dataSource);
234+
return VideoPlayerController.network(widget.dataSource,
235+
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true));
235236
}
236237
}
237238

packages/video_player/video_player/ios/Classes/FLTVideoPlayerPlugin.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,16 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
462462
}
463463
[_players removeAllObjects];
464464
result(nil);
465+
} else if ([@"setMixWithOthers" isEqualToString:call.method]) {
466+
BOOL mixWithOthers = [call.arguments boolValue];
467+
if (mixWithOthers) {
468+
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
469+
withOptions:AVAudioSessionCategoryOptionMixWithOthers
470+
error:nil];
471+
} else {
472+
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
473+
}
474+
result(nil);
465475
} else if ([@"create" isEqualToString:call.method]) {
466476
NSDictionary* argsMap = call.arguments;
467477
FLTFrameUpdater* frameUpdater = [[FLTFrameUpdater alloc] initWithRegistry:_registry];

packages/video_player/video_player/lib/video_player.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:meta/meta.dart';
1212

1313
import 'package:video_player_platform_interface/video_player_platform_interface.dart';
1414
export 'package:video_player_platform_interface/video_player_platform_interface.dart'
15-
show DurationRange, DataSourceType, VideoFormat;
15+
show DurationRange, DataSourceType, VideoFormat, VideoPlayerOptions;
1616

1717
// This will clear all open videos on the platform when a full restart is
1818
// performed.
@@ -145,7 +145,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
145145
/// The name of the asset is given by the [dataSource] argument and must not be
146146
/// null. The [package] argument must be non-null when the asset comes from a
147147
/// package and null otherwise.
148-
VideoPlayerController.asset(this.dataSource, {this.package})
148+
VideoPlayerController.asset(this.dataSource,
149+
{this.package, this.videoPlayerOptions})
149150
: dataSourceType = DataSourceType.asset,
150151
formatHint = null,
151152
super(VideoPlayerValue(duration: null));
@@ -157,7 +158,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
157158
/// null.
158159
/// **Android only**: The [formatHint] option allows the caller to override
159160
/// the video format detection code.
160-
VideoPlayerController.network(this.dataSource, {this.formatHint})
161+
VideoPlayerController.network(this.dataSource,
162+
{this.formatHint, this.videoPlayerOptions})
161163
: dataSourceType = DataSourceType.network,
162164
package = null,
163165
super(VideoPlayerValue(duration: null));
@@ -166,7 +168,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
166168
///
167169
/// This will load the file from the file-URI given by:
168170
/// `'file://${file.path}'`.
169-
VideoPlayerController.file(File file)
171+
VideoPlayerController.file(File file, {this.videoPlayerOptions})
170172
: dataSource = 'file://${file.path}',
171173
dataSourceType = DataSourceType.file,
172174
package = null,
@@ -187,6 +189,9 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
187189
/// is constructed with.
188190
final DataSourceType dataSourceType;
189191

192+
/// Provide additional configuration options (optional). Like setting the audio mode to mix
193+
final VideoPlayerOptions videoPlayerOptions;
194+
190195
/// Only set for [asset] videos. The package that the asset was loaded from.
191196
final String package;
192197
Timer _timer;
@@ -229,6 +234,11 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
229234
);
230235
break;
231236
}
237+
if (videoPlayerOptions?.mixWithOthers != null) {
238+
await VideoPlayerPlatform.instance
239+
.setMixWithOthers(videoPlayerOptions.mixWithOthers);
240+
}
241+
232242
_textureId =
233243
await VideoPlayerPlatform.instance.create(dataSourceDescription);
234244
_creatingCompleter.complete(null);

packages/video_player/video_player/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: video_player
22
description: Flutter plugin for displaying inline video with other Flutter
33
widgets on Android and iOS.
4-
version: 0.10.5
4+
version: 0.10.6
55
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player/video_player
66

77
flutter:
@@ -17,7 +17,7 @@ flutter:
1717

1818
dependencies:
1919
meta: "^1.0.5"
20-
video_player_platform_interface: ^1.0.1
20+
video_player_platform_interface: ^1.0.5
2121
# The design on https://flutter.dev/go/federated-plugins was to leave
2222
# this constraint as "any". We cannot do it right now as it fails pub publish
2323
# validation, so we set a ^ constraint.

packages/video_player/video_player/test/video_player_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class FakeController extends ValueNotifier<VideoPlayerValue>
4747

4848
@override
4949
VideoFormat get formatHint => null;
50+
51+
@override
52+
VideoPlayerOptions get videoPlayerOptions => null;
5053
}
5154

5255
void main() {
@@ -424,6 +427,27 @@ void main() {
424427
expect(colors.bufferedColor, bufferedColor);
425428
expect(colors.backgroundColor, backgroundColor);
426429
});
430+
431+
test('setMixWithOthers', () {
432+
bool mixWithOthers = false;
433+
const MethodChannel('flutter.io/videoPlayer')
434+
.setMockMethodCallHandler((MethodCall methodCall) async {
435+
switch (methodCall.method) {
436+
case 'setMixWithOthers':
437+
return mixWithOthers = methodCall.arguments;
438+
case 'create':
439+
return <String, dynamic>{'textureId': 100};
440+
default:
441+
return null;
442+
}
443+
});
444+
445+
final VideoPlayerController controller = VideoPlayerController.file(
446+
File(''),
447+
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true));
448+
controller.initialize();
449+
expect(mixWithOthers, true);
450+
});
427451
}
428452

429453
class FakeVideoPlayerPlatform {

packages/video_player/video_player_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.5
2+
3+
* Add VideoPlayerOptions with audo mix mode
4+
15
## 1.0.4
26

37
* Remove the deprecated `author:` field from pubspec.yaml

0 commit comments

Comments
 (0)