Add setIosCategory
This commit is contained in:
parent
64dc836b7f
commit
02631bfe84
|
@ -30,6 +30,9 @@ public class JustAudioPlugin implements MethodCallHandler {
|
|||
new AudioPlayer(registrar, id);
|
||||
result.success(null);
|
||||
break;
|
||||
case "setIosCategory":
|
||||
result.success(null);
|
||||
break;
|
||||
default:
|
||||
result.notImplemented();
|
||||
break;
|
||||
|
|
|
@ -18,13 +18,15 @@
|
|||
id _endObserver;
|
||||
id _timeObserver;
|
||||
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];
|
||||
NSAssert(self, @"super init cannot be nil");
|
||||
_registrar = registrar;
|
||||
_playerId = idParam;
|
||||
_configuredSession = configuredSession;
|
||||
_methodChannel = [FlutterMethodChannel
|
||||
methodChannelWithName:[NSMutableString stringWithFormat:@"com.ryanheise.just_audio.methods.%@", _playerId]
|
||||
binaryMessenger:[registrar messenger]];
|
||||
|
@ -285,6 +287,9 @@
|
|||
// TODO: dynamically adjust the lag.
|
||||
//int lag = 6;
|
||||
//int start = [self getCurrentPosition];
|
||||
if (_configuredSession) {
|
||||
[[AVAudioSession sharedInstance] setActive:YES error:nil];
|
||||
}
|
||||
[_player play];
|
||||
if (!@available(macOS 10.12, iOS 10.0, *)) {[self setPlaybackState:playing];}
|
||||
// TODO: convert this Android code to iOS
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
#import "JustAudioPlugin.h"
|
||||
#import "AudioPlayer.h"
|
||||
#import "AudioPlayer.h"
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
|
||||
@implementation JustAudioPlugin {
|
||||
NSObject<FlutterPluginRegistrar>* _registrar;
|
||||
BOOL _configuredSession;
|
||||
}
|
||||
|
||||
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
|
||||
|
@ -25,7 +27,23 @@
|
|||
if ([@"init" isEqualToString:call.method]) {
|
||||
NSArray* args = (NSArray*)call.arguments;
|
||||
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);
|
||||
} else {
|
||||
result(FlutterMethodNotImplemented);
|
||||
|
|
|
@ -25,4 +25,4 @@ SPEC CHECKSUMS:
|
|||
|
||||
PODFILE CHECKSUM: f32fb4e7c14f8b3ca19a369d7be425dd9241af27
|
||||
|
||||
COCOAPODS: 1.7.5
|
||||
COCOAPODS: 1.9.1
|
||||
|
|
|
@ -315,7 +315,6 @@
|
|||
/* Begin XCBuildConfiguration section */
|
||||
249021D3217E4FDB00AE95B9 /* Profile */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
|
@ -388,7 +387,6 @@
|
|||
};
|
||||
97C147031CF9000F007C117D /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
baseConfigurationReference = 9740EEB21CF90195004384FC /* Debug.xcconfig */;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
|
@ -444,7 +442,6 @@
|
|||
};
|
||||
97C147041CF9000F007C117D /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
|
|
|
@ -18,6 +18,7 @@ class _MyAppState extends State<MyApp> {
|
|||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
AudioPlayer.setIosCategory(IosCategory.playback);
|
||||
_player = AudioPlayer();
|
||||
_player
|
||||
.setUrl(
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
@interface AudioPlayer : NSObject<FlutterStreamHandler>
|
||||
|
||||
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar playerId:(NSString*)idParam;
|
||||
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar playerId:(NSString*)idParam configuredSession:(BOOL)configuredSession;
|
||||
|
||||
@end
|
||||
|
||||
|
|
|
@ -50,6 +50,22 @@ class AudioPlayer {
|
|||
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 int _id;
|
||||
|
@ -540,3 +556,14 @@ class IcyMetadata {
|
|||
|
||||
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,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue