Skip to content

Commit

Permalink
fix: audio track API for 1 track (#776)
Browse files Browse the repository at this point in the history
reverts a bug fix in #759

it was a good patch but it prevented the audio tracks API to be
populated if there was just 1 audio track.
it's still expected that with 1 audio track, the API returns an item for
video.audioTracks[0].

this change adds that back in.

the root issue in hls.js I believe was that we were trying to select an
audio track with a string ID and hls.js works with numeric ID's.
this change also adds a check if hls.js audioTracks has the ID to which
the player tries to switch.
  • Loading branch information
luwes committed Sep 22, 2023
1 parent bfbe4f0 commit 4222fd1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/playback-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ export const loadMedia = (
| 'subtitleTrack'
| 'userConfig'
| 'audioTrack'
| 'audioTracks'
| 'autoLevelEnabled'
| 'nextLevel'
| 'levels'
Expand Down
15 changes: 10 additions & 5 deletions packages/playback-core/src/media-tracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type { VideoRenditionList } from 'media-tracks';

export function setupMediaTracks(
customMediaEl: HTMLMediaElement,
hls: Pick<Hls, 'audioTrack' | 'autoLevelEnabled' | 'nextLevel' | 'levels' | 'on' | 'once' | 'off' | 'trigger'>
hls: Pick<
Hls,
'audioTrack' | 'audioTracks' | 'autoLevelEnabled' | 'nextLevel' | 'levels' | 'on' | 'once' | 'off' | 'trigger'
>
) {
if (!('videoTracks' in customMediaEl)) return;

Expand Down Expand Up @@ -36,9 +39,6 @@ export function setupMediaTracks(
hls.on(Hls.Events.AUDIO_TRACKS_UPDATED, function (event, data) {
removeAudioTracks();

// At least with ll-hls, there is a bug when enabling a single audio track (may be wider)
// for now, we can early bail if the `audioTracks` length is <=1. (CJP)
if (data.audioTracks.length <= 1) return;
for (const a of data.audioTracks) {
// hls.js doesn't return a `kind` property for audio tracks yet.
const kind = a.default ? 'main' : 'alternative';
Expand All @@ -52,7 +52,12 @@ export function setupMediaTracks(
});

customMediaEl.audioTracks.addEventListener('change', () => {
hls.audioTrack = [...customMediaEl.audioTracks].find((t) => t.enabled).id;
// Cast to number, hls.js uses numeric id's.
const audioTrackId = +[...customMediaEl.audioTracks].find((t) => t.enabled)?.id;
const availableIds = hls.audioTracks.map((t) => t.id);
if (audioTrackId != hls.audioTrack && availableIds.includes(audioTrackId)) {
hls.audioTrack = audioTrackId;
}
});

// Fired when a level is removed after calling `removeLevel()`
Expand Down

5 comments on commit 4222fd1

@vercel
Copy link

@vercel vercel bot commented on 4222fd1 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

elements-demo-vue – ./examples/vue-with-typescript

elements-demo-vue-mux.vercel.app
elements-demo-vue.vercel.app
elements-demo-vue-git-main-mux.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4222fd1 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

elements-demo-svelte-kit – ./examples/svelte-kit

elements-demo-svelte-kit-mux.vercel.app
elements-demo-svelte-kit-git-main-mux.vercel.app
elements-demo-svelte-kit.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4222fd1 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

elements-demo-create-react-app – ./examples/create-react-app-with-typescript

elements-demo-create-react-app-git-main-mux.vercel.app
elements-demo-create-react-app.vercel.app
elements-demo-create-react-app-mux.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4222fd1 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

elements-demo-nextjs – ./examples/nextjs-with-typescript

elements-demo-nextjs.vercel.app
elements-demo-nextjs-mux.vercel.app
elements-demo-nextjs-git-main-mux.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4222fd1 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

elements-demo-vanilla – ./examples/vanilla-ts-esm

elements-demo-vanilla-mux.vercel.app
elements-demo-vanilla.vercel.app
elements-demo-vanilla-git-main-mux.vercel.app

Please sign in to comment.