-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(android): add audio track selection for ExoPlayer #10312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nateshmbhat
wants to merge
14
commits into
flutter:main
Choose a base branch
from
nateshmbhat:28-oct-platform-android
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b544b45
feat(video_player_android): implement audio track selection API
nateshmbhat 1fdb5d5
Merge branch 'main' into 28-oct-platform-android
nateshmbhat a0a9a0d
Update packages/video_player/video_player_android/android/src/main/ja…
nateshmbhat 061388d
refactor(video_player_android): improve audio track selection validat…
nateshmbhat 8c776aa
Merge branch '28-oct-platform-android' of github.com:nateshmbhat/flut…
nateshmbhat 40b1b3f
test(video_player_android): add audio track support availability test…
nateshmbhat f9d72fe
Merge branch 'main' into 28-oct-platform-android
nateshmbhat f0a3f38
Update packages/video_player/video_player_android/android/src/main/ja…
nateshmbhat 25fa206
test(video_player_android): change audio track validation from warnin…
nateshmbhat 308a341
Merge branch 'main' into 28-oct-platform-android
nateshmbhat 2bcef5f
Merge branch '28-oct-platform-android' of github.com:nateshmbhat/flut…
nateshmbhat fc51b80
Merge branch 'main' into 28-oct-platform-android
nateshmbhat 15db47e
style(video_player_android): apply dart format and omit obvious local…
nateshmbhat ffdd3ac
Merge branch '28-oct-platform-android' of github.com:nateshmbhat/flut…
nateshmbhat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,14 +7,23 @@ | |
| import static androidx.media3.common.Player.REPEAT_MODE_ALL; | ||
| import static androidx.media3.common.Player.REPEAT_MODE_OFF; | ||
|
|
||
| import android.util.Log; | ||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.media3.common.AudioAttributes; | ||
| import androidx.media3.common.C; | ||
| import androidx.media3.common.Format; | ||
| import androidx.media3.common.MediaItem; | ||
| import androidx.media3.common.PlaybackParameters; | ||
| import androidx.media3.common.TrackGroup; | ||
| import androidx.media3.common.TrackSelectionOverride; | ||
| import androidx.media3.common.Tracks; | ||
| import androidx.media3.common.util.UnstableApi; | ||
| import androidx.media3.exoplayer.ExoPlayer; | ||
| import androidx.media3.exoplayer.trackselection.DefaultTrackSelector; | ||
| import io.flutter.view.TextureRegistry.SurfaceProducer; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A class responsible for managing video playback using {@link ExoPlayer}. | ||
|
|
@@ -26,6 +35,7 @@ public abstract class VideoPlayer implements VideoPlayerInstanceApi { | |
| @Nullable protected final SurfaceProducer surfaceProducer; | ||
| @Nullable private DisposeHandler disposeHandler; | ||
| @NonNull protected ExoPlayer exoPlayer; | ||
| @UnstableApi @Nullable protected DefaultTrackSelector trackSelector; | ||
|
|
||
| /** A closure-compatible signature since {@link java.util.function.Supplier} is API level 24. */ | ||
| public interface ExoPlayerProvider { | ||
|
|
@@ -43,6 +53,7 @@ public interface DisposeHandler { | |
| void onDispose(); | ||
| } | ||
|
|
||
| @UnstableApi | ||
| // Error thrown for this-escape warning on JDK 21+ due to https://bugs.openjdk.org/browse/JDK-8015831. | ||
| // Keeping behavior as-is and addressing the warning could cause a regression: https://github.com/flutter/packages/pull/10193 | ||
| @SuppressWarnings("this-escape") | ||
|
|
@@ -55,6 +66,12 @@ public VideoPlayer( | |
| this.videoPlayerEvents = events; | ||
| this.surfaceProducer = surfaceProducer; | ||
| exoPlayer = exoPlayerProvider.get(); | ||
|
|
||
| // Try to get the track selector from the ExoPlayer if it was built with one | ||
| if (exoPlayer.getTrackSelector() instanceof DefaultTrackSelector) { | ||
| trackSelector = (DefaultTrackSelector) exoPlayer.getTrackSelector(); | ||
| } | ||
|
|
||
| exoPlayer.setMediaItem(mediaItem); | ||
| exoPlayer.prepare(); | ||
| exoPlayer.addListener(createExoPlayerEventListener(exoPlayer, surfaceProducer)); | ||
|
|
@@ -125,6 +142,101 @@ public ExoPlayer getExoPlayer() { | |
| return exoPlayer; | ||
| } | ||
|
|
||
| @UnstableApi | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the TODOs to all the UnstableApi annotations for clarity? Sorry it's tedious :/ |
||
| @Override | ||
| public @NonNull NativeAudioTrackData getAudioTracks() { | ||
| List<ExoPlayerAudioTrackData> audioTracks = new ArrayList<>(); | ||
|
|
||
| // Get the current tracks from ExoPlayer | ||
| Tracks tracks = exoPlayer.getCurrentTracks(); | ||
|
|
||
| // Iterate through all track groups | ||
| for (int groupIndex = 0; groupIndex < tracks.getGroups().size(); groupIndex++) { | ||
| Tracks.Group group = tracks.getGroups().get(groupIndex); | ||
|
|
||
| // Only process audio tracks | ||
| if (group.getType() == C.TRACK_TYPE_AUDIO) { | ||
| for (int trackIndex = 0; trackIndex < group.length; trackIndex++) { | ||
| Format format = group.getTrackFormat(trackIndex); | ||
| boolean isSelected = group.isTrackSelected(trackIndex); | ||
|
|
||
| // Create audio track data with metadata | ||
| ExoPlayerAudioTrackData audioTrack = | ||
| new ExoPlayerAudioTrackData( | ||
| (long) groupIndex, | ||
| (long) trackIndex, | ||
| format.label, | ||
| format.language, | ||
| isSelected, | ||
| format.bitrate != Format.NO_VALUE ? (long) format.bitrate : null, | ||
| format.sampleRate != Format.NO_VALUE ? (long) format.sampleRate : null, | ||
| format.channelCount != Format.NO_VALUE ? (long) format.channelCount : null, | ||
| format.codecs != null ? format.codecs : null); | ||
|
|
||
| audioTracks.add(audioTrack); | ||
| } | ||
| } | ||
| } | ||
| return new NativeAudioTrackData(audioTracks); | ||
| } | ||
|
|
||
| @UnstableApi | ||
| @Override | ||
| public void selectAudioTrack(long groupIndex, long trackIndex) { | ||
| if (trackSelector == null) { | ||
| Log.w("VideoPlayer", "Cannot select audio track: track selector is null"); | ||
| return; | ||
| } | ||
|
|
||
| // Get current tracks | ||
| Tracks tracks = exoPlayer.getCurrentTracks(); | ||
|
|
||
| if (groupIndex < 0 || groupIndex >= tracks.getGroups().size()) { | ||
| Log.w( | ||
nateshmbhat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "VideoPlayer", | ||
| "Cannot select audio track: groupIndex " | ||
| + groupIndex | ||
| + " is out of bounds (available groups: " | ||
| + tracks.getGroups().size() | ||
| + ")"); | ||
| return; | ||
| } | ||
|
|
||
| Tracks.Group group = tracks.getGroups().get((int) groupIndex); | ||
|
|
||
| // Verify it's an audio track | ||
| if (group.getType() != C.TRACK_TYPE_AUDIO) { | ||
| Log.w( | ||
| "VideoPlayer", | ||
| "Cannot select audio track: group at index " | ||
| + groupIndex | ||
| + " is not an audio track (type: " | ||
| + group.getType() | ||
| + ")"); | ||
| return; | ||
| } | ||
|
|
||
| // Verify the track index is valid | ||
| if (trackIndex < 0 || (int) trackIndex >= group.length) { | ||
| Log.w( | ||
| "VideoPlayer", | ||
| "Cannot select audio track: trackIndex " | ||
| + trackIndex | ||
| + " is out of bounds (available tracks in group: " | ||
| + group.length | ||
| + ")"); | ||
| return; | ||
| } | ||
|
|
||
| // Get the track group and create a selection override | ||
| TrackGroup trackGroup = group.getMediaTrackGroup(); | ||
| TrackSelectionOverride override = new TrackSelectionOverride(trackGroup, (int) trackIndex); | ||
|
|
||
| // Apply the track selection override | ||
| trackSelector.setParameters( | ||
| trackSelector.buildUponParameters().setOverrideForType(override).build()); | ||
| } | ||
|
|
||
| public void dispose() { | ||
| if (disposeHandler != null) { | ||
| disposeHandler.onDispose(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.