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

Commit b074e04

Browse files
committed
[video_player] Set audio mix options
1 parent 1c9529a commit b074e04

File tree

18 files changed

+252
-35
lines changed

18 files changed

+252
-35
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.12
2+
3+
* Introduce VideoPlayerOptions to set the audio mix mode.
4+
15
## 0.10.11+2
26

37
* Fix aspectRatio calculation when size.width or size.height are zero.

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,31 @@ static PositionMessage fromMap(HashMap map) {
223223
}
224224
}
225225

226+
/** Generated class from Pigeon that represents data sent in messages. */
227+
public static class MixWithOthersMessage {
228+
private Boolean mixWithOthers;
229+
230+
public Boolean getMixWithOthers() {
231+
return mixWithOthers;
232+
}
233+
234+
public void setMixWithOthers(Boolean setterArg) {
235+
this.mixWithOthers = setterArg;
236+
}
237+
238+
HashMap toMap() {
239+
HashMap<String, Object> toMapResult = new HashMap<String, Object>();
240+
toMapResult.put("mixWithOthers", mixWithOthers);
241+
return toMapResult;
242+
}
243+
244+
static MixWithOthersMessage fromMap(HashMap map) {
245+
MixWithOthersMessage fromMapResult = new MixWithOthersMessage();
246+
fromMapResult.mixWithOthers = (Boolean) map.get("mixWithOthers");
247+
return fromMapResult;
248+
}
249+
}
250+
226251
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
227252
public interface VideoPlayerApi {
228253
void initialize();
@@ -243,6 +268,8 @@ public interface VideoPlayerApi {
243268

244269
void pause(TextureMessage arg);
245270

271+
void setMixWithOthers(MixWithOthersMessage arg);
272+
246273
/** Sets up an instance of `VideoPlayerApi` to handle messages through the `binaryMessenger` */
247274
public static void setup(BinaryMessenger binaryMessenger, VideoPlayerApi api) {
248275
{
@@ -469,6 +496,31 @@ public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
469496
channel.setMessageHandler(null);
470497
}
471498
}
499+
{
500+
BasicMessageChannel<Object> channel =
501+
new BasicMessageChannel<Object>(
502+
binaryMessenger,
503+
"dev.flutter.pigeon.VideoPlayerApi.setMixWithOthers",
504+
new StandardMessageCodec());
505+
if (api != null) {
506+
channel.setMessageHandler(
507+
new BasicMessageChannel.MessageHandler<Object>() {
508+
public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
509+
MixWithOthersMessage input = MixWithOthersMessage.fromMap((HashMap) message);
510+
HashMap<String, HashMap> wrapped = new HashMap<String, HashMap>();
511+
try {
512+
api.setMixWithOthers(input);
513+
wrapped.put("result", null);
514+
} catch (Exception exception) {
515+
wrapped.put("error", wrapError(exception));
516+
}
517+
reply.reply(wrapped);
518+
}
519+
});
520+
} else {
521+
channel.setMessageHandler(null);
522+
}
523+
}
472524
}
473525
}
474526

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
@@ -56,14 +56,18 @@ final class VideoPlayer {
5656

5757
private boolean isInitialized = false;
5858

59+
private final VideoPlayerOptions options;
60+
5961
VideoPlayer(
6062
Context context,
6163
EventChannel eventChannel,
6264
TextureRegistry.SurfaceTextureEntry textureEntry,
6365
String dataSource,
64-
String formatHint) {
66+
String formatHint,
67+
VideoPlayerOptions options) {
6568
this.eventChannel = eventChannel;
6669
this.textureEntry = textureEntry;
70+
this.options = options;
6771

6872
TrackSelector trackSelector = new DefaultTrackSelector();
6973
exoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
@@ -163,7 +167,7 @@ public void onCancel(Object o) {
163167

164168
surface = new Surface(textureEntry.surfaceTexture());
165169
exoPlayer.setVideoSurface(surface);
166-
setAudioAttributes(exoPlayer);
170+
setAudioAttributes(exoPlayer, options.mixWithOthers);
167171

168172
exoPlayer.addListener(
169173
new EventListener() {
@@ -203,10 +207,10 @@ void sendBufferingUpdate() {
203207
}
204208

205209
@SuppressWarnings("deprecation")
206-
private static void setAudioAttributes(SimpleExoPlayer exoPlayer) {
210+
private static void setAudioAttributes(SimpleExoPlayer exoPlayer, boolean isMixMode) {
207211
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
208212
exoPlayer.setAudioAttributes(
209-
new AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MOVIE).build());
213+
new AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MOVIE).build(), !isMixMode);
210214
} else {
211215
exoPlayer.setAudioStreamType(C.STREAM_TYPE_MUSIC);
212216
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.flutter.plugins.videoplayer;
2+
3+
class VideoPlayerOptions {
4+
public boolean mixWithOthers;
5+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.flutter.plugin.common.PluginRegistry.Registrar;
1414
import io.flutter.plugins.videoplayer.Messages.CreateMessage;
1515
import io.flutter.plugins.videoplayer.Messages.LoopingMessage;
16+
import io.flutter.plugins.videoplayer.Messages.MixWithOthersMessage;
1617
import io.flutter.plugins.videoplayer.Messages.PositionMessage;
1718
import io.flutter.plugins.videoplayer.Messages.TextureMessage;
1819
import io.flutter.plugins.videoplayer.Messages.VideoPlayerApi;
@@ -25,6 +26,7 @@ public class VideoPlayerPlugin implements FlutterPlugin, VideoPlayerApi {
2526
private static final String TAG = "VideoPlayerPlugin";
2627
private final LongSparseArray<VideoPlayer> videoPlayers = new LongSparseArray<>();
2728
private FlutterState flutterState;
29+
private VideoPlayerOptions options = new VideoPlayerOptions();
2830

2931
/** Register this with the v2 embedding for the plugin to respond to lifecycle callbacks. */
3032
public VideoPlayerPlugin() {}
@@ -113,7 +115,8 @@ public TextureMessage create(CreateMessage arg) {
113115
eventChannel,
114116
handle,
115117
"asset:///" + assetLookupKey,
116-
null);
118+
null,
119+
options);
117120
videoPlayers.put(handle.id(), player);
118121
} else {
119122
player =
@@ -122,7 +125,8 @@ public TextureMessage create(CreateMessage arg) {
122125
eventChannel,
123126
handle,
124127
arg.getUri(),
125-
arg.getFormatHint());
128+
arg.getFormatHint(),
129+
options);
126130
videoPlayers.put(handle.id(), player);
127131
}
128132

@@ -170,6 +174,11 @@ public void pause(TextureMessage arg) {
170174
player.pause();
171175
}
172176

177+
@Override
178+
public void setMixWithOthers(MixWithOthersMessage arg) {
179+
options.mixWithOthers = arg.getMixWithOthers();
180+
}
181+
173182
private interface KeyForAssetFn {
174183
String get(String asset);
175184
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class _BumbleBeeRemoteVideoState extends State<_BumbleBeeRemoteVideo> {
220220
_controller = VideoPlayerController.network(
221221
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4',
222222
closedCaptionFile: _loadCaptions(),
223+
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
223224
);
224225

225226
_controller.addListener(() {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,4 +560,15 @@ - (void)pause:(FLTTextureMessage*)input error:(FlutterError**)error {
560560
[player pause];
561561
}
562562

563+
- (void)setMixWithOthers:(FLTMixWithOthersMessage*)input
564+
error:(FlutterError* _Nullable __autoreleasing*)error {
565+
if ([input.mixWithOthers boolValue]) {
566+
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
567+
withOptions:AVAudioSessionCategoryOptionMixWithOthers
568+
error:nil];
569+
} else {
570+
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
571+
}
572+
}
573+
563574
@end

packages/video_player/video_player/ios/Classes/messages.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
1212
@class FLTLoopingMessage;
1313
@class FLTVolumeMessage;
1414
@class FLTPositionMessage;
15+
@class FLTMixWithOthersMessage;
1516

1617
@interface FLTTextureMessage : NSObject
1718
@property(nonatomic, strong, nullable) NSNumber *textureId;
@@ -39,21 +40,27 @@ NS_ASSUME_NONNULL_BEGIN
3940
@property(nonatomic, strong, nullable) NSNumber *position;
4041
@end
4142

43+
@interface FLTMixWithOthersMessage : NSObject
44+
@property(nonatomic, strong, nullable) NSNumber *mixWithOthers;
45+
@end
46+
4247
@protocol FLTVideoPlayerApi
4348
- (void)initialize:(FlutterError *_Nullable *_Nonnull)error;
44-
- (nullable FLTTextureMessage *)create:(FLTCreateMessage *)input
45-
error:(FlutterError *_Nullable *_Nonnull)error;
49+
- (FLTTextureMessage *)create:(FLTCreateMessage *)input
50+
error:(FlutterError *_Nullable *_Nonnull)error;
4651
- (void)dispose:(FLTTextureMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
4752
- (void)setLooping:(FLTLoopingMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
4853
- (void)setVolume:(FLTVolumeMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
4954
- (void)play:(FLTTextureMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
50-
- (nullable FLTPositionMessage *)position:(FLTTextureMessage *)input
51-
error:(FlutterError *_Nullable *_Nonnull)error;
55+
- (FLTPositionMessage *)position:(FLTTextureMessage *)input
56+
error:(FlutterError *_Nullable *_Nonnull)error;
5257
- (void)seekTo:(FLTPositionMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
5358
- (void)pause:(FLTTextureMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
59+
- (void)setMixWithOthers:(FLTMixWithOthersMessage *)input
60+
error:(FlutterError *_Nullable *_Nonnull)error;
5461
@end
5562

5663
extern void FLTVideoPlayerApiSetup(id<FlutterBinaryMessenger> binaryMessenger,
57-
id<FLTVideoPlayerApi> _Nullable api);
64+
id<FLTVideoPlayerApi> api);
5865

5966
NS_ASSUME_NONNULL_END

packages/video_player/video_player/ios/Classes/messages.m

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ @interface FLTPositionMessage ()
4040
+ (FLTPositionMessage *)fromMap:(NSDictionary *)dict;
4141
- (NSDictionary *)toMap;
4242
@end
43+
@interface FLTMixWithOthersMessage ()
44+
+ (FLTMixWithOthersMessage *)fromMap:(NSDictionary *)dict;
45+
- (NSDictionary *)toMap;
46+
@end
4347

4448
@implementation FLTTextureMessage
4549
+ (FLTTextureMessage *)fromMap:(NSDictionary *)dict {
@@ -51,9 +55,9 @@ + (FLTTextureMessage *)fromMap:(NSDictionary *)dict {
5155
return result;
5256
}
5357
- (NSDictionary *)toMap {
54-
return [NSDictionary
55-
dictionaryWithObjectsAndKeys:(self.textureId != nil ? self.textureId : [NSNull null]),
56-
@"textureId", nil];
58+
return
59+
[NSDictionary dictionaryWithObjectsAndKeys:(self.textureId ? self.textureId : [NSNull null]),
60+
@"textureId", nil];
5761
}
5862
@end
5963

@@ -82,9 +86,9 @@ - (NSDictionary *)toMap {
8286
return [NSDictionary
8387
dictionaryWithObjectsAndKeys:(self.asset ? self.asset : [NSNull null]), @"asset",
8488
(self.uri ? self.uri : [NSNull null]), @"uri",
85-
(self.packageName != nil ? self.packageName : [NSNull null]),
89+
(self.packageName ? self.packageName : [NSNull null]),
8690
@"packageName",
87-
(self.formatHint != nil ? self.formatHint : [NSNull null]),
91+
(self.formatHint ? self.formatHint : [NSNull null]),
8892
@"formatHint", nil];
8993
}
9094
@end
@@ -104,10 +108,9 @@ + (FLTLoopingMessage *)fromMap:(NSDictionary *)dict {
104108
}
105109
- (NSDictionary *)toMap {
106110
return [NSDictionary
107-
dictionaryWithObjectsAndKeys:(self.textureId != nil ? self.textureId : [NSNull null]),
108-
@"textureId",
109-
(self.isLooping != nil ? self.isLooping : [NSNull null]),
110-
@"isLooping", nil];
111+
dictionaryWithObjectsAndKeys:(self.textureId ? self.textureId : [NSNull null]), @"textureId",
112+
(self.isLooping ? self.isLooping : [NSNull null]), @"isLooping",
113+
nil];
111114
}
112115
@end
113116

@@ -126,9 +129,8 @@ + (FLTVolumeMessage *)fromMap:(NSDictionary *)dict {
126129
}
127130
- (NSDictionary *)toMap {
128131
return [NSDictionary
129-
dictionaryWithObjectsAndKeys:(self.textureId != nil ? self.textureId : [NSNull null]),
130-
@"textureId", (self.volume != nil ? self.volume : [NSNull null]),
131-
@"volume", nil];
132+
dictionaryWithObjectsAndKeys:(self.textureId ? self.textureId : [NSNull null]), @"textureId",
133+
(self.volume ? self.volume : [NSNull null]), @"volume", nil];
132134
}
133135
@end
134136

@@ -147,10 +149,25 @@ + (FLTPositionMessage *)fromMap:(NSDictionary *)dict {
147149
}
148150
- (NSDictionary *)toMap {
149151
return [NSDictionary
150-
dictionaryWithObjectsAndKeys:(self.textureId != nil ? self.textureId : [NSNull null]),
151-
@"textureId",
152-
(self.position != nil ? self.position : [NSNull null]),
153-
@"position", nil];
152+
dictionaryWithObjectsAndKeys:(self.textureId ? self.textureId : [NSNull null]), @"textureId",
153+
(self.position ? self.position : [NSNull null]), @"position",
154+
nil];
155+
}
156+
@end
157+
158+
@implementation FLTMixWithOthersMessage
159+
+ (FLTMixWithOthersMessage *)fromMap:(NSDictionary *)dict {
160+
FLTMixWithOthersMessage *result = [[FLTMixWithOthersMessage alloc] init];
161+
result.mixWithOthers = dict[@"mixWithOthers"];
162+
if ((NSNull *)result.mixWithOthers == [NSNull null]) {
163+
result.mixWithOthers = nil;
164+
}
165+
return result;
166+
}
167+
- (NSDictionary *)toMap {
168+
return [NSDictionary
169+
dictionaryWithObjectsAndKeys:(self.mixWithOthers ? self.mixWithOthers : [NSNull null]),
170+
@"mixWithOthers", nil];
154171
}
155172
@end
156173

@@ -289,4 +306,19 @@ void FLTVideoPlayerApiSetup(id<FlutterBinaryMessenger> binaryMessenger, id<FLTVi
289306
[channel setMessageHandler:nil];
290307
}
291308
}
309+
{
310+
FlutterBasicMessageChannel *channel = [FlutterBasicMessageChannel
311+
messageChannelWithName:@"dev.flutter.pigeon.VideoPlayerApi.setMixWithOthers"
312+
binaryMessenger:binaryMessenger];
313+
if (api) {
314+
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
315+
FlutterError *error;
316+
FLTMixWithOthersMessage *input = [FLTMixWithOthersMessage fromMap:message];
317+
[api setMixWithOthers:input error:&error];
318+
callback(wrapResult(nil, error));
319+
}];
320+
} else {
321+
[channel setMessageHandler:nil];
322+
}
323+
}
292324
}

0 commit comments

Comments
 (0)