Skip to content

Commit 22c1bb0

Browse files
committed
refactor(video_player): replace track ID with group/track indices
- Changed VideoAudioTrack to use groupIndex and trackIndex instead of a single string ID - Updated selectAudioTrack to accept VideoAudioTrack object instead of string trackId - Modified UI to display track indices in format "Track {groupIndex}_{trackIndex}"
1 parent c63b050 commit 22c1bb0

File tree

29 files changed

+388
-237
lines changed

29 files changed

+388
-237
lines changed

packages/video_player/video_player/example/lib/audio_tracks_demo.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,28 @@ class _AudioTracksDemoState extends State<AudioTracksDemo> {
106106
}
107107
}
108108

109-
Future<void> _selectAudioTrack(String trackId) async {
109+
Future<void> _selectAudioTrack(VideoAudioTrack track) async {
110110
final VideoPlayerController? controller = _controller;
111111
if (controller == null) {
112112
return;
113113
}
114114

115115
try {
116-
await controller.selectAudioTrack(trackId);
116+
await controller.selectAudioTrack(track);
117117

118118
// Reload tracks to update selection status
119119
await _loadAudioTracks();
120120

121121
if (!mounted) {
122122
return;
123123
}
124-
ScaffoldMessenger.of(
125-
context,
126-
).showSnackBar(SnackBar(content: Text('Selected audio track: $trackId')));
124+
ScaffoldMessenger.of(context).showSnackBar(
125+
SnackBar(
126+
content: Text(
127+
'Selected audio track: ${track.label.isNotEmpty ? track.label : "Track ${track.groupIndex}_${track.trackIndex}"}',
128+
),
129+
),
130+
);
127131
} catch (e) {
128132
if (!mounted) {
129133
return;
@@ -348,15 +352,17 @@ class _AudioTracksDemoState extends State<AudioTracksDemo> {
348352
),
349353
),
350354
title: Text(
351-
track.label.isNotEmpty ? track.label : 'Track ${track.id}',
355+
track.label.isNotEmpty
356+
? track.label
357+
: 'Track ${track.groupIndex}_${track.trackIndex}',
352358
style: TextStyle(
353359
fontWeight: track.isSelected ? FontWeight.bold : FontWeight.normal,
354360
),
355361
),
356362
subtitle: Column(
357363
crossAxisAlignment: CrossAxisAlignment.start,
358364
children: <Widget>[
359-
Text('ID: ${track.id}'),
365+
Text('Group: ${track.groupIndex}, Track: ${track.trackIndex}'),
360366
Text('Language: ${track.language}'),
361367
if (track.codec != null) Text('Codec: ${track.codec}'),
362368
if (track.bitrate != null) Text('Bitrate: ${track.bitrate} bps'),
@@ -370,7 +376,7 @@ class _AudioTracksDemoState extends State<AudioTracksDemo> {
370376
track.isSelected
371377
? const Icon(Icons.radio_button_checked, color: Colors.green)
372378
: const Icon(Icons.radio_button_unchecked),
373-
onTap: track.isSelected ? null : () => _selectAudioTrack(track.id),
379+
onTap: track.isSelected ? null : () => _selectAudioTrack(track),
374380
),
375381
);
376382
}

packages/video_player/video_player/example/pubspec.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ flutter:
3838
# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
3939
# See https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changing-federated-plugins
4040
dependency_overrides:
41-
video_player_android:
42-
{ path: ../../../../packages/video_player/video_player_android }
43-
video_player_avfoundation:
44-
{ path: ../../../../packages/video_player/video_player_avfoundation }
45-
video_player_web: { path: ../../../../packages/video_player/video_player_web }
41+
video_player_android: {path: ../../../../packages/video_player/video_player_android}
42+
video_player_avfoundation: {path: ../../../../packages/video_player/video_player_avfoundation}
43+
video_player_platform_interface: {path: ../../../../packages/video_player/video_player_platform_interface}
44+
video_player_web: {path: ../../../../packages/video_player/video_player_web}

packages/video_player/video_player/lib/video_player.dart

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export 'src/closed_caption_file.dart';
3030
class VideoAudioTrack {
3131
/// Constructs an instance of [VideoAudioTrack].
3232
const VideoAudioTrack({
33-
required this.id,
33+
required this.groupIndex,
34+
required this.trackIndex,
3435
required this.label,
3536
required this.language,
3637
required this.isSelected,
@@ -40,8 +41,11 @@ class VideoAudioTrack {
4041
this.codec,
4142
});
4243

43-
/// Unique identifier for the audio track.
44-
final String id;
44+
/// The group index of the audio track.
45+
final int groupIndex;
46+
47+
/// The track index within the group.
48+
final int trackIndex;
4549

4650
/// Human-readable label for the track.
4751
final String label;
@@ -73,7 +77,8 @@ class VideoAudioTrack {
7377
return identical(this, other) ||
7478
other is VideoAudioTrack &&
7579
runtimeType == other.runtimeType &&
76-
id == other.id &&
80+
groupIndex == other.groupIndex &&
81+
trackIndex == other.trackIndex &&
7782
label == other.label &&
7883
language == other.language &&
7984
isSelected == other.isSelected &&
@@ -85,7 +90,8 @@ class VideoAudioTrack {
8590

8691
@override
8792
int get hashCode => Object.hash(
88-
id,
93+
groupIndex,
94+
trackIndex,
8995
label,
9096
language,
9197
isSelected,
@@ -98,7 +104,8 @@ class VideoAudioTrack {
98104
@override
99105
String toString() =>
100106
'VideoAudioTrack('
101-
'id: $id, '
107+
'groupIndex: $groupIndex, '
108+
'trackIndex: $trackIndex, '
102109
'label: $label, '
103110
'language: $language, '
104111
'isSelected: $isSelected, '
@@ -120,7 +127,8 @@ VideoAudioTrack _convertPlatformAudioTrack(
120127
platform_interface.VideoAudioTrack platformTrack,
121128
) {
122129
return VideoAudioTrack(
123-
id: platformTrack.id,
130+
groupIndex: platformTrack.groupIndex,
131+
trackIndex: platformTrack.trackIndex,
124132
label: platformTrack.label ?? 'Unknown',
125133
language: platformTrack.language ?? 'und',
126134
isSelected: platformTrack.isSelected,
@@ -945,20 +953,27 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
945953
return platformTracks.map(_convertPlatformAudioTrack).toList();
946954
}
947955

948-
/// Selects which audio track is chosen for playback from its [trackId]
949-
///
950-
/// The [trackId] should match the ID of one of the tracks returned by
951-
/// [getAudioTracks]. If the track ID is not found or invalid, the
952-
/// platform may ignore the request or throw an exception.
956+
/// Selects which audio track is chosen for playback.
953957
///
954-
/// Throws an exception if the video player is disposed or not initialized.
955-
Future<void> selectAudioTrack(String trackId) async {
958+
/// The [track] parameter should be one of the tracks returned by [getAudioTracks].
959+
Future<void> selectAudioTrack(VideoAudioTrack track) async {
956960
if (_isDisposedOrNotInitialized) {
957961
throw Exception('VideoPlayerController is disposed or not initialized');
958962
}
959-
// The platform implementation (e.g., Android) will wait for the track
960-
// selection to complete by listening to platform-specific events
961-
await _videoPlayerPlatform.selectAudioTrack(_playerId, trackId);
963+
// Convert the public VideoAudioTrack to platform interface VideoAudioTrack
964+
final platform_interface.VideoAudioTrack platformTrack =
965+
platform_interface.VideoAudioTrack(
966+
groupIndex: track.groupIndex,
967+
trackIndex: track.trackIndex,
968+
label: track.label,
969+
language: track.language,
970+
isSelected: track.isSelected,
971+
bitrate: track.bitrate,
972+
sampleRate: track.sampleRate,
973+
channelCount: track.channelCount,
974+
codec: track.codec,
975+
);
976+
await _videoPlayerPlatform.selectAudioTrack(_playerId, platformTrack);
962977
}
963978

964979
/// Returns whether audio track selection is supported on this platform.

packages/video_player/video_player/pubspec.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ topics:
4141
# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
4242
# See https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changing-federated-plugins
4343
dependency_overrides:
44-
video_player_android:
45-
{ path: ../../../packages/video_player/video_player_android }
46-
video_player_avfoundation:
47-
{ path: ../../../packages/video_player/video_player_avfoundation }
48-
video_player_web: { path: ../../../packages/video_player/video_player_web }
44+
video_player_android: {path: ../../../packages/video_player/video_player_android}
45+
video_player_avfoundation: {path: ../../../packages/video_player/video_player_avfoundation}
46+
video_player_platform_interface: {path: ../../../packages/video_player/video_player_platform_interface}
47+
video_player_web: {path: ../../../packages/video_player/video_player_web}

0 commit comments

Comments
 (0)