Skip to content

Commit 1161332

Browse files
committed
perf(ios): optimize audio track selection in video player
- Added caching for audio selection options to avoid repeated media group queries - Simplified current audio track selection by using direct item method - Optimized selectAudioTrack to use cached options instead of re-fetching media group - Added cleanup of cached options in dispose method to prevent memory leaks
1 parent 5b1e5c7 commit 1161332

File tree

1 file changed

+19
-12
lines changed
  • packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation

1 file changed

+19
-12
lines changed

packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation/FVPVideoPlayer.m

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ static void FVPRemoveKeyValueObservers(NSObject *observer,
7373
@implementation FVPVideoPlayer {
7474
// Whether or not player and player item listeners have ever been registered.
7575
BOOL _listenersRegistered;
76+
// Cached media selection options for audio tracks (HLS streams)
77+
NSArray<AVMediaSelectionOption *> *_cachedAudioSelectionOptions;
7678
}
7779

7880
- (instancetype)initWithPlayerItem:(AVPlayerItem *)item
@@ -151,6 +153,9 @@ - (void)disposeWithError:(FlutterError *_Nullable *_Nonnull)error {
151153
FVPRemoveKeyValueObservers(self, FVPGetPlayerItemObservations(), self.player.currentItem);
152154
FVPRemoveKeyValueObservers(self, FVPGetPlayerObservations(), self.player);
153155
}
156+
157+
// Clear cached audio selection options
158+
_cachedAudioSelectionOptions = nil;
154159

155160
[self.player replaceCurrentItemWithPlayerItem:nil];
156161

@@ -478,13 +483,14 @@ - (nullable FVPNativeAudioTrackData *)getAudioTracks:(FlutterError *_Nullable *_
478483
AVMediaSelectionGroup *audioGroup =
479484
[asset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
480485
if (audioGroup && audioGroup.options.count > 0) {
486+
// Cache the options array for later use in selectAudioTrack
487+
_cachedAudioSelectionOptions = audioGroup.options;
488+
481489
NSMutableArray<FVPMediaSelectionAudioTrackData *> *mediaSelectionTracks =
482490
[[NSMutableArray alloc] init];
483491
AVMediaSelectionOption *currentSelection = nil;
484492
if (@available(iOS 11.0, *)) {
485-
AVMediaSelection *currentMediaSelection = currentItem.currentMediaSelection;
486-
currentSelection =
487-
[currentMediaSelection selectedMediaOptionInMediaSelectionGroup:audioGroup];
493+
currentSelection = [currentItem selectedMediaOptionInMediaSelectionGroup:audioGroup];
488494
} else {
489495
#pragma clang diagnostic push
490496
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@@ -664,15 +670,16 @@ - (void)selectAudioTrack:(nonnull NSString *)trackId
664670

665671
// Check if this is a media selection track (for HLS streams)
666672
if ([trackId hasPrefix:@"media_selection_"]) {
667-
AVMediaSelectionGroup *audioGroup =
668-
[asset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
669-
if (audioGroup && audioGroup.options.count > 0) {
670-
// Parse the track ID to get the index
671-
NSString *indexString = [trackId substringFromIndex:[@"media_selection_" length]];
672-
NSInteger index = [indexString integerValue];
673-
674-
if (index >= 0 && index < audioGroup.options.count) {
675-
AVMediaSelectionOption *option = audioGroup.options[index];
673+
// Parse the track ID to get the index
674+
NSString *indexString = [trackId substringFromIndex:[@"media_selection_" length]];
675+
NSInteger index = [indexString integerValue];
676+
677+
// Validate that we have cached options and the index is valid
678+
if (_cachedAudioSelectionOptions && index >= 0 && index < _cachedAudioSelectionOptions.count) {
679+
AVMediaSelectionOption *option = _cachedAudioSelectionOptions[index];
680+
AVMediaSelectionGroup *audioGroup =
681+
[asset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
682+
if (audioGroup) {
676683
[currentItem selectMediaOption:option inMediaSelectionGroup:audioGroup];
677684
}
678685
}

0 commit comments

Comments
 (0)