Fix bugs in dynamic concatenated audio source on iOS.

This commit is contained in:
Ryan Heise 2020-08-12 04:12:18 +10:00
parent 0a772d1927
commit 67b7afceac
1 changed files with 15 additions and 9 deletions

View File

@ -127,7 +127,7 @@
[self concatenatingInsertAll:(NSString*)args[0] index:[args[1] intValue] sources:(NSArray*)args[2]]; [self concatenatingInsertAll:(NSString*)args[0] index:[args[1] intValue] sources:(NSArray*)args[2]];
result(nil); result(nil);
} else if ([@"concatenating.removeAt" isEqualToString:call.method]) { } else if ([@"concatenating.removeAt" isEqualToString:call.method]) {
[self concatenatingRemoveAt:(NSString*)args[0] index:(int)args[1]]; [self concatenatingRemoveAt:(NSString*)args[0] index:[args[1] intValue]];
result(nil); result(nil);
} else if ([@"concatenating.removeRange" isEqualToString:call.method]) { } else if ([@"concatenating.removeRange" isEqualToString:call.method]) {
[self concatenatingRemoveRange:(NSString*)args[0] start:[args[1] intValue] end:[args[2] intValue]]; [self concatenatingRemoveRange:(NSString*)args[0] start:[args[1] intValue] end:[args[2] intValue]];
@ -251,7 +251,8 @@
// Re-index the audio sources. // Re-index the audio sources.
_indexedAudioSources = [[NSMutableArray alloc] init]; _indexedAudioSources = [[NSMutableArray alloc] init];
[_audioSource buildSequence:_indexedAudioSources treeIndex:0]; [_audioSource buildSequence:_indexedAudioSources treeIndex:0];
_index = [self indexForItem:_player.currentItem]; [self updateOrder];
[self enqueueFrom:[self indexForItem:_player.currentItem]];
[self broadcastPlaybackEvent]; [self broadcastPlaybackEvent];
} }
@ -329,7 +330,7 @@
return 0; return 0;
} else if (CMTIME_IS_VALID(_seekPos)) { } else if (CMTIME_IS_VALID(_seekPos)) {
return (int)(1000 * CMTimeGetSeconds(_seekPos)); return (int)(1000 * CMTimeGetSeconds(_seekPos));
} else if (_indexedAudioSources) { } else if (_indexedAudioSources && _indexedAudioSources.count > 0) {
int ms = (int)(1000 * CMTimeGetSeconds(_indexedAudioSources[_index].position)); int ms = (int)(1000 * CMTimeGetSeconds(_indexedAudioSources[_index].position));
if (ms < 0) ms = 0; if (ms < 0) ms = 0;
return ms; return ms;
@ -341,7 +342,7 @@
- (int)getBufferedPosition { - (int)getBufferedPosition {
if (_processingState == none || _processingState == loading) { if (_processingState == none || _processingState == loading) {
return 0; return 0;
} else if (_indexedAudioSources) { } else if (_indexedAudioSources && _indexedAudioSources.count > 0) {
int ms = (int)(1000 * CMTimeGetSeconds(_indexedAudioSources[_index].bufferedPosition)); int ms = (int)(1000 * CMTimeGetSeconds(_indexedAudioSources[_index].bufferedPosition));
if (ms < 0) ms = 0; if (ms < 0) ms = 0;
return ms; return ms;
@ -353,7 +354,7 @@
- (int)getDuration { - (int)getDuration {
if (_processingState == none) { if (_processingState == none) {
return -1; return -1;
} else if (_indexedAudioSources) { } else if (_indexedAudioSources && _indexedAudioSources.count > 0) {
int v = (int)(1000 * CMTimeGetSeconds(_indexedAudioSources[_index].duration)); int v = (int)(1000 * CMTimeGetSeconds(_indexedAudioSources[_index].duration));
return v; return v;
} else { } else {
@ -425,7 +426,6 @@
} }
- (void)enqueueFrom:(int)index { - (void)enqueueFrom:(int)index {
int oldIndex = _index;
_index = index; _index = index;
// Update the queue while keeping the currently playing item untouched. // Update the queue while keeping the currently playing item untouched.
@ -436,22 +436,27 @@
// First, remove all _player items except for the currently playing one (if any). // First, remove all _player items except for the currently playing one (if any).
IndexedPlayerItem *oldItem = _player.currentItem; IndexedPlayerItem *oldItem = _player.currentItem;
IndexedPlayerItem *existingItem = nil; IndexedPlayerItem *existingItem = nil;
IndexedPlayerItem *newItem = _indexedAudioSources.count > 0 ? _indexedAudioSources[_index].playerItem : nil;
NSArray *oldPlayerItems = [NSArray arrayWithArray:_player.items]; NSArray *oldPlayerItems = [NSArray arrayWithArray:_player.items];
// In the first pass, preserve the old and new items. // In the first pass, preserve the old and new items.
for (int i = 0; i < oldPlayerItems.count; i++) { for (int i = 0; i < oldPlayerItems.count; i++) {
if (oldPlayerItems[i] == _indexedAudioSources[_index].playerItem) { if (oldPlayerItems[i] == newItem) {
// Preserve and tag new item if it is already in the queue. // Preserve and tag new item if it is already in the queue.
existingItem = oldPlayerItems[i]; existingItem = oldPlayerItems[i];
//NSLog(@"Preserving existing item %d", [self indexForItem:existingItem]);
} else if (oldPlayerItems[i] == oldItem) { } else if (oldPlayerItems[i] == oldItem) {
//NSLog(@"Preserving old item %d", [self indexForItem:oldItem]);
// Temporarily preserve old item, just to avoid jumping to // Temporarily preserve old item, just to avoid jumping to
// intermediate queue positions unnecessarily. We only want to jump // intermediate queue positions unnecessarily. We only want to jump
// once to _index. // once to _index.
} else { } else {
//NSLog(@"Removing item %d", [self indexForItem:oldPlayerItems[i]]);
[_player removeItem:oldPlayerItems[i]]; [_player removeItem:oldPlayerItems[i]];
} }
} }
// In the second pass, remove the old item (if different from new item). // In the second pass, remove the old item (if different from new item).
if (_index != oldIndex) { if (oldItem && newItem != oldItem) {
//NSLog(@"removing old item %d", [self indexForItem:oldItem]);
[_player removeItem:oldItem]; [_player removeItem:oldItem];
} }
@ -464,6 +469,7 @@
int si = [_order[i] intValue]; int si = [_order[i] intValue];
if (si == _index) include = YES; if (si == _index) include = YES;
if (include && _indexedAudioSources[si].playerItem != existingItem) { if (include && _indexedAudioSources[si].playerItem != existingItem) {
//NSLog(@"inserting item %d", si);
[_player insertItem:_indexedAudioSources[si].playerItem afterItem:nil]; [_player insertItem:_indexedAudioSources[si].playerItem afterItem:nil];
} }
} }
@ -471,7 +477,7 @@
/* NSLog(@"after reorder: _player.items.count: ", _player.items.count); */ /* NSLog(@"after reorder: _player.items.count: ", _player.items.count); */
/* [self dumpQueue]; */ /* [self dumpQueue]; */
if (_processingState != loading && oldItem != _indexedAudioSources[_index].playerItem) { if (_processingState != loading && oldItem != newItem) {
// || !_player.currentItem.playbackLikelyToKeepUp; // || !_player.currentItem.playbackLikelyToKeepUp;
if (_player.currentItem.playbackBufferEmpty) { if (_player.currentItem.playbackBufferEmpty) {
[self enterBuffering:@"enqueueFrom playbackBufferEmpty"]; [self enterBuffering:@"enqueueFrom playbackBufferEmpty"];