Skip to content

Commit

Permalink
direct play for local assets fix
Browse files Browse the repository at this point in the history
  • Loading branch information
feroult committed Jul 17, 2018
1 parent 9de126f commit 3c300be
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions ios/Classes/AudioplayersPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ @implementation AudioplayersPlugin {
FlutterResult _result;
}

typedef void (^VoidCallback)(NSString * playerId);

NSMutableSet *timeobservers;
FlutterMethodChannel *_channel;

Expand All @@ -44,7 +46,7 @@ - (id)init {
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSString * playerId = call.arguments[@"playerId"];
NSLog(@"iOS => call %@, playerId %@", call.method, playerId);

typedef void (^CaseBlock)();

// Squint and this looks like a proper switch!
Expand Down Expand Up @@ -101,7 +103,12 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSLog(@"setUrl");
NSString *url = call.arguments[@"url"];
int isLocal = [call.arguments[@"isLocal"]intValue];
[self setUrl:url isLocal:isLocal playerId:playerId ];
[ self setUrl:url
isLocal:isLocal
playerId:playerId
onReady:^(NSString * playerId) {
}
];
},
@"setVolume":
^{
Expand All @@ -118,6 +125,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
}
};

[ self initPlayerInfo:playerId ];
CaseBlock c = methods[call.method];
if (c) c(); else {
NSLog(@"not implemented");
Expand All @@ -126,10 +134,17 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
result(@(1));
}

-(void) initPlayerInfo: (NSString *) playerId {
NSMutableDictionary * playerInfo = players[playerId];
if (!playerInfo) {
players[playerId] = [@{@"isPlaying": @false, @"volume": @(1.0), @"looping": @(false)} mutableCopy];
}
}

-(void) setUrl: (NSString*) url
isLocal: (bool) isLocal
playerId: (NSString*) playerId
playerId: (NSString*) playerId
onReady:(VoidCallback)onReady
{
NSMutableDictionary * playerInfo = players[playerId];
AVPlayer *player = playerInfo[@"player"];
Expand All @@ -145,7 +160,7 @@ -(void) setUrl: (NSString*) url
playerItem = [ [ AVPlayerItem alloc ] initWithURL:[ NSURL URLWithString:url ]];
}

if (playerInfo) {
if (playerInfo[@"url"]) {
[[player currentItem] removeObserver:self forKeyPath:@"player.currentItem.status" ];

[ playerInfo setObject:url forKey:@"url" ];
Expand All @@ -158,8 +173,13 @@ -(void) setUrl: (NSString*) url
} else {
player = [[ AVPlayer alloc ] initWithPlayerItem: playerItem ];
observers = [[NSMutableSet alloc] init];
playerInfo = [@{@"player": player, @"url": url, @"isPlaying": @false, @"observers": observers, @"volume": @(1.0), @"looping": @(false)} mutableCopy];
players[playerId] = playerInfo;

[ playerInfo setObject:player forKey:@"player" ];
[ playerInfo setObject:url forKey:@"url" ];
[ playerInfo setObject:observers forKey:@"observers" ];

// playerInfo = [@{@"player": player, @"url": url, @"isPlaying": @false, @"observers": observers, @"volume": @(1.0), @"looping": @(false)} mutableCopy];
// players[playerId] = playerInfo;

// stream player position
CMTime interval = CMTimeMakeWithSeconds(0.2, NSEC_PER_SEC);
Expand All @@ -178,6 +198,7 @@ -(void) setUrl: (NSString*) url
[observers addObject:anobserver];

// is sound ready
[playerInfo setObject:onReady forKey:@"onReady"];
[playerItem addObserver:self
forKeyPath:@"player.currentItem.status"
options:0
Expand All @@ -190,23 +211,26 @@ -(void) play: (NSString*) playerId
url: (NSString*) url
isLocal: (int) isLocal
volume: (float) volume
{
NSMutableDictionary * playerInfo = players[playerId];
AVPlayer *player = playerInfo[@"player"];

[ self setUrl:url isLocal:isLocal playerId:playerId ];
[ self updateDuration:playerId ];
[ player setVolume:volume ];
[ player play];
[playerInfo setObject:@true forKey:@"isPlaying"];
{
[ self setUrl:url
isLocal:isLocal
playerId:playerId
onReady:^(NSString * playerId) {
NSMutableDictionary * playerInfo = players[playerId];
AVPlayer *player = playerInfo[@"player"];
[ player setVolume:volume ];
[ player play];
[ playerInfo setObject:@true forKey:@"isPlaying" ];
}
];
}

-(void) updateDuration: (NSString *) playerId
{
NSMutableDictionary * playerInfo = players[playerId];
AVPlayer *player = playerInfo[@"player"];

CMTime duration = [[player currentItem] duration ];
CMTime duration = [ [player currentItem] duration ];
NSLog(@"ios -> updateDuration...%f", CMTimeGetSeconds(duration));
if(CMTimeGetSeconds(duration)>0){
NSLog(@"ios -> invokechannel");
Expand Down Expand Up @@ -298,6 +322,12 @@ -(void)observeValueForKeyPath:(NSString *)keyPath
// Do something with the status…
if ([[player currentItem] status ] == AVPlayerItemStatusReadyToPlay) {
[self updateDuration:playerId];

VoidCallback onReady = playerInfo[@"onReady"];
if (onReady != nil) {
[playerInfo removeObjectForKey:@"onReady"];
onReady(playerId);
}
} else if ([[player currentItem] status ] == AVPlayerItemStatusFailed) {
[_channel invokeMethod:@"audio.onError" arguments:@{@"playerId": playerId, @"value": @"AVPlayerItemStatus.failed"}];
}
Expand All @@ -324,5 +354,6 @@ - (void)dealloc {
players = nil;
}


@end

0 comments on commit 3c300be

Please sign in to comment.