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

Reland "[video_player] Set audio mix options (#2922)" #2939

Merged
merged 1 commit into from
Aug 18, 2020
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
4 changes: 4 additions & 0 deletions packages/video_player/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.10.12

* Introduce VideoPlayerOptions to set the audio mix mode.

## 0.10.11+2

* Fix aspectRatio calculation when size.width or size.height are zero.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,31 @@ static PositionMessage fromMap(HashMap map) {
}
}

/** Generated class from Pigeon that represents data sent in messages. */
public static class MixWithOthersMessage {
private Boolean mixWithOthers;

public Boolean getMixWithOthers() {
return mixWithOthers;
}

public void setMixWithOthers(Boolean setterArg) {
this.mixWithOthers = setterArg;
}

HashMap toMap() {
HashMap<String, Object> toMapResult = new HashMap<String, Object>();
toMapResult.put("mixWithOthers", mixWithOthers);
return toMapResult;
}

static MixWithOthersMessage fromMap(HashMap map) {
MixWithOthersMessage fromMapResult = new MixWithOthersMessage();
fromMapResult.mixWithOthers = (Boolean) map.get("mixWithOthers");
return fromMapResult;
}
}

/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
public interface VideoPlayerApi {
void initialize();
Expand All @@ -243,6 +268,8 @@ public interface VideoPlayerApi {

void pause(TextureMessage arg);

void setMixWithOthers(MixWithOthersMessage arg);

/** Sets up an instance of `VideoPlayerApi` to handle messages through the `binaryMessenger` */
public static void setup(BinaryMessenger binaryMessenger, VideoPlayerApi api) {
{
Expand Down Expand Up @@ -469,6 +496,31 @@ public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<Object>(
binaryMessenger,
"dev.flutter.pigeon.VideoPlayerApi.setMixWithOthers",
new StandardMessageCodec());
if (api != null) {
channel.setMessageHandler(
new BasicMessageChannel.MessageHandler<Object>() {
public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) {
MixWithOthersMessage input = MixWithOthersMessage.fromMap((HashMap) message);
HashMap<String, HashMap> wrapped = new HashMap<String, HashMap>();
try {
api.setMixWithOthers(input);
wrapped.put("result", null);
} catch (Exception exception) {
wrapped.put("error", wrapError(exception));
}
reply.reply(wrapped);
}
});
} else {
channel.setMessageHandler(null);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ final class VideoPlayer {

private boolean isInitialized = false;

private final VideoPlayerOptions options;

VideoPlayer(
Context context,
EventChannel eventChannel,
TextureRegistry.SurfaceTextureEntry textureEntry,
String dataSource,
String formatHint) {
String formatHint,
VideoPlayerOptions options) {
this.eventChannel = eventChannel;
this.textureEntry = textureEntry;
this.options = options;

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

surface = new Surface(textureEntry.surfaceTexture());
exoPlayer.setVideoSurface(surface);
setAudioAttributes(exoPlayer);
setAudioAttributes(exoPlayer, options.mixWithOthers);

exoPlayer.addListener(
new EventListener() {
Expand Down Expand Up @@ -203,10 +207,10 @@ void sendBufferingUpdate() {
}

@SuppressWarnings("deprecation")
private static void setAudioAttributes(SimpleExoPlayer exoPlayer) {
private static void setAudioAttributes(SimpleExoPlayer exoPlayer, boolean isMixMode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
exoPlayer.setAudioAttributes(
new AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MOVIE).build());
new AudioAttributes.Builder().setContentType(C.CONTENT_TYPE_MOVIE).build(), !isMixMode);
} else {
exoPlayer.setAudioStreamType(C.STREAM_TYPE_MUSIC);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.flutter.plugins.videoplayer;

class VideoPlayerOptions {
public boolean mixWithOthers;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugins.videoplayer.Messages.CreateMessage;
import io.flutter.plugins.videoplayer.Messages.LoopingMessage;
import io.flutter.plugins.videoplayer.Messages.MixWithOthersMessage;
import io.flutter.plugins.videoplayer.Messages.PositionMessage;
import io.flutter.plugins.videoplayer.Messages.TextureMessage;
import io.flutter.plugins.videoplayer.Messages.VideoPlayerApi;
Expand All @@ -25,6 +26,7 @@ public class VideoPlayerPlugin implements FlutterPlugin, VideoPlayerApi {
private static final String TAG = "VideoPlayerPlugin";
private final LongSparseArray<VideoPlayer> videoPlayers = new LongSparseArray<>();
private FlutterState flutterState;
private VideoPlayerOptions options = new VideoPlayerOptions();

/** Register this with the v2 embedding for the plugin to respond to lifecycle callbacks. */
public VideoPlayerPlugin() {}
Expand Down Expand Up @@ -113,7 +115,8 @@ public TextureMessage create(CreateMessage arg) {
eventChannel,
handle,
"asset:///" + assetLookupKey,
null);
null,
options);
videoPlayers.put(handle.id(), player);
} else {
player =
Expand All @@ -122,7 +125,8 @@ public TextureMessage create(CreateMessage arg) {
eventChannel,
handle,
arg.getUri(),
arg.getFormatHint());
arg.getFormatHint(),
options);
videoPlayers.put(handle.id(), player);
}

Expand Down Expand Up @@ -170,6 +174,11 @@ public void pause(TextureMessage arg) {
player.pause();
}

@Override
public void setMixWithOthers(MixWithOthersMessage arg) {
options.mixWithOthers = arg.getMixWithOthers();
}

private interface KeyForAssetFn {
String get(String asset);
}
Expand Down
1 change: 1 addition & 0 deletions packages/video_player/video_player/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class _BumbleBeeRemoteVideoState extends State<_BumbleBeeRemoteVideo> {
_controller = VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4',
closedCaptionFile: _loadCaptions(),
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
);

_controller.addListener(() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,15 @@ - (void)pause:(FLTTextureMessage*)input error:(FlutterError**)error {
[player pause];
}

- (void)setMixWithOthers:(FLTMixWithOthersMessage*)input
error:(FlutterError* _Nullable __autoreleasing*)error {
if ([input.mixWithOthers boolValue]) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
} else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
}

@end
7 changes: 7 additions & 0 deletions packages/video_player/video_player/ios/Classes/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
@class FLTLoopingMessage;
@class FLTVolumeMessage;
@class FLTPositionMessage;
@class FLTMixWithOthersMessage;

@interface FLTTextureMessage : NSObject
@property(nonatomic, strong, nullable) NSNumber *textureId;
Expand Down Expand Up @@ -39,6 +40,10 @@ NS_ASSUME_NONNULL_BEGIN
@property(nonatomic, strong, nullable) NSNumber *position;
@end

@interface FLTMixWithOthersMessage : NSObject
@property(nonatomic, strong, nullable) NSNumber *mixWithOthers;
@end

@protocol FLTVideoPlayerApi
- (void)initialize:(FlutterError *_Nullable *_Nonnull)error;
- (nullable FLTTextureMessage *)create:(FLTCreateMessage *)input
Expand All @@ -51,6 +56,8 @@ NS_ASSUME_NONNULL_BEGIN
error:(FlutterError *_Nullable *_Nonnull)error;
- (void)seekTo:(FLTPositionMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
- (void)pause:(FLTTextureMessage *)input error:(FlutterError *_Nullable *_Nonnull)error;
- (void)setMixWithOthers:(FLTMixWithOthersMessage *)input
error:(FlutterError *_Nullable *_Nonnull)error;
@end

extern void FLTVideoPlayerApiSetup(id<FlutterBinaryMessenger> binaryMessenger,
Expand Down
35 changes: 35 additions & 0 deletions packages/video_player/video_player/ios/Classes/messages.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ @interface FLTPositionMessage ()
+ (FLTPositionMessage *)fromMap:(NSDictionary *)dict;
- (NSDictionary *)toMap;
@end
@interface FLTMixWithOthersMessage ()
+ (FLTMixWithOthersMessage *)fromMap:(NSDictionary *)dict;
- (NSDictionary *)toMap;
@end

@implementation FLTTextureMessage
+ (FLTTextureMessage *)fromMap:(NSDictionary *)dict {
Expand Down Expand Up @@ -154,6 +158,22 @@ - (NSDictionary *)toMap {
}
@end

@implementation FLTMixWithOthersMessage
+ (FLTMixWithOthersMessage *)fromMap:(NSDictionary *)dict {
FLTMixWithOthersMessage *result = [[FLTMixWithOthersMessage alloc] init];
result.mixWithOthers = dict[@"mixWithOthers"];
if ((NSNull *)result.mixWithOthers == [NSNull null]) {
result.mixWithOthers = nil;
}
return result;
}
- (NSDictionary *)toMap {
return [NSDictionary
dictionaryWithObjectsAndKeys:(self.mixWithOthers != nil ? self.mixWithOthers : [NSNull null]),
@"mixWithOthers", nil];
}
@end

void FLTVideoPlayerApiSetup(id<FlutterBinaryMessenger> binaryMessenger, id<FLTVideoPlayerApi> api) {
{
FlutterBasicMessageChannel *channel = [FlutterBasicMessageChannel
Expand Down Expand Up @@ -289,4 +309,19 @@ void FLTVideoPlayerApiSetup(id<FlutterBinaryMessenger> binaryMessenger, id<FLTVi
[channel setMessageHandler:nil];
}
}
{
FlutterBasicMessageChannel *channel = [FlutterBasicMessageChannel
messageChannelWithName:@"dev.flutter.pigeon.VideoPlayerApi.setMixWithOthers"
binaryMessenger:binaryMessenger];
if (api) {
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
FlutterError *error;
FLTMixWithOthersMessage *input = [FLTMixWithOthersMessage fromMap:message];
[api setMixWithOthers:input error:&error];
callback(wrapResult(nil, error));
}];
} else {
[channel setMessageHandler:nil];
}
}
}
18 changes: 14 additions & 4 deletions packages/video_player/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:meta/meta.dart';

import 'package:video_player_platform_interface/video_player_platform_interface.dart';
export 'package:video_player_platform_interface/video_player_platform_interface.dart'
show DurationRange, DataSourceType, VideoFormat;
show DurationRange, DataSourceType, VideoFormat, VideoPlayerOptions;

import 'src/closed_caption_file.dart';
export 'src/closed_caption_file.dart';
Expand Down Expand Up @@ -168,7 +168,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
/// null. The [package] argument must be non-null when the asset comes from a
/// package and null otherwise.
VideoPlayerController.asset(this.dataSource,
{this.package, this.closedCaptionFile})
{this.package, this.closedCaptionFile, this.videoPlayerOptions})
: dataSourceType = DataSourceType.asset,
formatHint = null,
super(VideoPlayerValue(duration: null));
Expand All @@ -181,7 +181,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
/// **Android only**: The [formatHint] option allows the caller to override
/// the video format detection code.
VideoPlayerController.network(this.dataSource,
{this.formatHint, this.closedCaptionFile})
{this.formatHint, this.closedCaptionFile, this.videoPlayerOptions})
: dataSourceType = DataSourceType.network,
package = null,
super(VideoPlayerValue(duration: null));
Expand All @@ -190,7 +190,8 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
///
/// This will load the file from the file-URI given by:
/// `'file://${file.path}'`.
VideoPlayerController.file(File file, {this.closedCaptionFile})
VideoPlayerController.file(File file,
{this.closedCaptionFile, this.videoPlayerOptions})
: dataSource = 'file://${file.path}',
dataSourceType = DataSourceType.file,
package = null,
Expand All @@ -211,6 +212,9 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
/// is constructed with.
final DataSourceType dataSourceType;

/// Provide additional configuration options (optional). Like setting the audio mode to mix
final VideoPlayerOptions videoPlayerOptions;

/// Only set for [asset] videos. The package that the asset was loaded from.
final String package;

Expand Down Expand Up @@ -262,6 +266,12 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
);
break;
}

if (videoPlayerOptions?.mixWithOthers != null) {
await _videoPlayerPlatform
.setMixWithOthers(videoPlayerOptions.mixWithOthers);
}

_textureId = await _videoPlayerPlatform.create(dataSourceDescription);
_creatingCompleter.complete(null);
final Completer<void> initializingCompleter = Completer<void>();
Expand Down
5 changes: 5 additions & 0 deletions packages/video_player/video_player/pigeons/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class CreateMessage {
String formatHint;
}

class MixWithOthersMessage {
bool mixWithOthers;
}

@HostApi()
abstract class VideoPlayerApi {
void initialize();
Expand All @@ -37,6 +41,7 @@ abstract class VideoPlayerApi {
PositionMessage position(TextureMessage msg);
void seekTo(PositionMessage msg);
void pause(TextureMessage msg);
void setMixWithOthers(MixWithOthersMessage msg);
}

void configurePigeon(PigeonOptions opts) {
Expand Down
2 changes: 1 addition & 1 deletion packages/video_player/video_player/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Flutter plugin for displaying inline video with other Flutter
# 0.10.y+z is compatible with 1.0.0, if you land a breaking change bump
# the version to 2.0.0.
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
version: 0.10.11+2
version: 0.10.12
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player/video_player

flutter:
Expand Down
Loading