Add setIosCategory

This commit is contained in:
Ryan Heise 2020-05-21 00:38:26 +10:00
parent 64dc836b7f
commit 02631bfe84
8 changed files with 58 additions and 7 deletions

View File

@ -30,6 +30,9 @@ public class JustAudioPlugin implements MethodCallHandler {
new AudioPlayer(registrar, id); new AudioPlayer(registrar, id);
result.success(null); result.success(null);
break; break;
case "setIosCategory":
result.success(null);
break;
default: default:
result.notImplemented(); result.notImplemented();
break; break;

View File

@ -18,13 +18,15 @@
id _endObserver; id _endObserver;
id _timeObserver; id _timeObserver;
BOOL _automaticallyWaitsToMinimizeStalling; BOOL _automaticallyWaitsToMinimizeStalling;
BOOL _configuredSession;
} }
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar playerId:(NSString*)idParam { - (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar playerId:(NSString*)idParam configuredSession:(BOOL)configuredSession {
self = [super init]; self = [super init];
NSAssert(self, @"super init cannot be nil"); NSAssert(self, @"super init cannot be nil");
_registrar = registrar; _registrar = registrar;
_playerId = idParam; _playerId = idParam;
_configuredSession = configuredSession;
_methodChannel = [FlutterMethodChannel _methodChannel = [FlutterMethodChannel
methodChannelWithName:[NSMutableString stringWithFormat:@"com.ryanheise.just_audio.methods.%@", _playerId] methodChannelWithName:[NSMutableString stringWithFormat:@"com.ryanheise.just_audio.methods.%@", _playerId]
binaryMessenger:[registrar messenger]]; binaryMessenger:[registrar messenger]];
@ -285,6 +287,9 @@
// TODO: dynamically adjust the lag. // TODO: dynamically adjust the lag.
//int lag = 6; //int lag = 6;
//int start = [self getCurrentPosition]; //int start = [self getCurrentPosition];
if (_configuredSession) {
[[AVAudioSession sharedInstance] setActive:YES error:nil];
}
[_player play]; [_player play];
if (!@available(macOS 10.12, iOS 10.0, *)) {[self setPlaybackState:playing];} if (!@available(macOS 10.12, iOS 10.0, *)) {[self setPlaybackState:playing];}
// TODO: convert this Android code to iOS // TODO: convert this Android code to iOS

View File

@ -1,9 +1,11 @@
#import "JustAudioPlugin.h" #import "JustAudioPlugin.h"
#import "AudioPlayer.h" #import "AudioPlayer.h"
#import "AudioPlayer.h" #import "AudioPlayer.h"
#import <AVFoundation/AVFoundation.h>
@implementation JustAudioPlugin { @implementation JustAudioPlugin {
NSObject<FlutterPluginRegistrar>* _registrar; NSObject<FlutterPluginRegistrar>* _registrar;
BOOL _configuredSession;
} }
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar { + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
@ -25,7 +27,23 @@
if ([@"init" isEqualToString:call.method]) { if ([@"init" isEqualToString:call.method]) {
NSArray* args = (NSArray*)call.arguments; NSArray* args = (NSArray*)call.arguments;
NSString* playerId = args[0]; NSString* playerId = args[0];
AudioPlayer* player = [[AudioPlayer alloc] initWithRegistrar:_registrar playerId:playerId]; AudioPlayer* player = [[AudioPlayer alloc] initWithRegistrar:_registrar playerId:playerId configuredSession:_configuredSession];
result(nil);
} else if ([@"setIosCategory" isEqualToString:call.method]) {
NSNumber* categoryIndex = (NSNumber*)call.arguments;
AVAudioSessionCategory category = nil;
switch (categoryIndex.integerValue) {
case 0: category = AVAudioSessionCategoryAmbient; break;
case 1: category = AVAudioSessionCategorySoloAmbient; break;
case 2: category = AVAudioSessionCategoryPlayback; break;
case 3: category = AVAudioSessionCategoryRecord; break;
case 4: category = AVAudioSessionCategoryPlayAndRecord; break;
case 5: category = AVAudioSessionCategoryMultiRoute; break;
}
if (category) {
_configuredSession = YES;
}
[[AVAudioSession sharedInstance] setCategory:category error:nil];
result(nil); result(nil);
} else { } else {
result(FlutterMethodNotImplemented); result(FlutterMethodNotImplemented);

View File

@ -25,4 +25,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: f32fb4e7c14f8b3ca19a369d7be425dd9241af27 PODFILE CHECKSUM: f32fb4e7c14f8b3ca19a369d7be425dd9241af27
COCOAPODS: 1.7.5 COCOAPODS: 1.9.1

View File

@ -315,7 +315,6 @@
/* Begin XCBuildConfiguration section */ /* Begin XCBuildConfiguration section */
249021D3217E4FDB00AE95B9 /* Profile */ = { 249021D3217E4FDB00AE95B9 /* Profile */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = { buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO; ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_NONNULL = YES;
@ -388,7 +387,6 @@
}; };
97C147031CF9000F007C117D /* Debug */ = { 97C147031CF9000F007C117D /* Debug */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */;
buildSettings = { buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO; ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_NONNULL = YES;
@ -444,7 +442,6 @@
}; };
97C147041CF9000F007C117D /* Release */ = { 97C147041CF9000F007C117D /* Release */ = {
isa = XCBuildConfiguration; isa = XCBuildConfiguration;
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
buildSettings = { buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO; ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES; CLANG_ANALYZER_NONNULL = YES;

View File

@ -18,6 +18,7 @@ class _MyAppState extends State<MyApp> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
AudioPlayer.setIosCategory(IosCategory.playback);
_player = AudioPlayer(); _player = AudioPlayer();
_player _player
.setUrl( .setUrl(

View File

@ -2,7 +2,7 @@
@interface AudioPlayer : NSObject<FlutterStreamHandler> @interface AudioPlayer : NSObject<FlutterStreamHandler>
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar playerId:(NSString*)idParam; - (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar playerId:(NSString*)idParam configuredSession:(BOOL)configuredSession;
@end @end

View File

@ -50,6 +50,22 @@ class AudioPlayer {
return MethodChannel('com.ryanheise.just_audio.methods.$id'); return MethodChannel('com.ryanheise.just_audio.methods.$id');
} }
/// Configure the audio session category on iOS. Has no effect on Android.
///
/// Note that the default category on iOS is [IosCategory.soloAmbient], but
/// for a typical media app, you will probably want to change this to
/// [IosCategory.playback]. If you wish to override the default, call this
/// method before playing any audio.
///
/// Note: If you use other audio plugins in conjunction with this one, it is
/// possible that the other audio plugin may override the setting you choose
/// here. (You may consider asking the developer of each other plugin you use
/// to provide a similar method so that you can configure the same category
/// universally.)
static Future<void> setIosCategory(IosCategory category) async {
await _mainChannel.invokeMethod('setIosCategory', category.index);
}
final Future<MethodChannel> _channel; final Future<MethodChannel> _channel;
final int _id; final int _id;
@ -540,3 +556,14 @@ class IcyMetadata {
IcyMetadata({@required this.info, @required this.headers}); IcyMetadata({@required this.info, @required this.headers});
} }
/// The audio session categories on iOS, to be used with
/// [AudioPlayer.setIosCategory].
enum IosCategory {
ambient,
soloAmbient,
playback,
record,
playAndRecord,
multiRoute,
}