From 91e28f31e326f9527bef4c0e4925bfec77db7180 Mon Sep 17 00:00:00 2001 From: shiyiya Date: Thu, 27 Jun 2024 22:23:32 +0800 Subject: [PATCH] feat(hls): add defaultQuality --- examples/standalone/main.ts | 4 ++-- packages/hls/package.json | 2 +- packages/hls/src/index.ts | 20 ++++++++++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/examples/standalone/main.ts b/examples/standalone/main.ts index 33224232..e38ecacb 100644 --- a/examples/standalone/main.ts +++ b/examples/standalone/main.ts @@ -96,7 +96,7 @@ const player = Player.make('#player', { } }), torrent(), - hls({ forceHLS: true }), + hls({ forceHLS: true, defaultQuality: 1080 }), dash(), mpegts(), danmaku({ @@ -105,7 +105,7 @@ const player = Player.make('#player', { source: DANMAKU //SUPER_DANMAKU }), new Playlist({ - initialIndex: 7, + initialIndex: 0, sources: [ { title: 'HLS with SRT subtitle', diff --git a/packages/hls/package.json b/packages/hls/package.json index bfb33608..a13a132b 100644 --- a/packages/hls/package.json +++ b/packages/hls/package.json @@ -1,6 +1,6 @@ { "name": "@oplayer/hls", - "version": "1.2.26", + "version": "1.2.27-beta.0", "description": "Hls plugin for oplayer", "type": "module", "main": "./dist/index.es.js", diff --git a/packages/hls/src/index.ts b/packages/hls/src/index.ts index 9feb6820..6d93e054 100644 --- a/packages/hls/src/index.ts +++ b/packages/hls/src/index.ts @@ -11,6 +11,12 @@ export interface HlsPluginOptions { matcher?: Matcher + /** + * -1 : auto + * default auto + */ + defaultQuality?: number + /** * custom handle hls fatal error */ @@ -84,7 +90,8 @@ class HlsPlugin implements PlayerPlugin { qualityControl: true, withBitrate: false, qualitySwitch: 'immediate', - matcher: defaultMatcher + matcher: defaultMatcher, + defaultQuality: -1 } constructor(options?: HlsPluginOptions) { @@ -181,10 +188,12 @@ export default function create(options?: HlsPluginOptions): PlayerPlugin { return new HlsPlugin(options) } -const generateSetting = (player: Player, instance: Hls, options: HlsPluginOptions = {}) => { +const generateSetting = (player: Player, instance: Hls, options: HlsPlugin['options']) => { const ui = player.context.ui if (options.qualityControl) { instance.once(HlsPlugin.library.Events.LEVEL_LOADED, () => { + let defaultSelect = options.defaultQuality + injectSetting({ icon: ui.icons.quality, name: 'Quality', @@ -200,6 +209,11 @@ const generateSetting = (player: Player, instance: Hls, options: HlsPluginOption const number = useMb ? (kb / 1000).toFixed(2) : Math.floor(kb) name += ` (${number}${useMb ? 'm' : 'k'}bps)` } + + if (level.height <= options.defaultQuality) { + defaultSelect = i + } + pre.push({ name, default: instance.currentLevel == i, value: i }) return pre }, @@ -218,6 +232,8 @@ const generateSetting = (player: Player, instance: Hls, options: HlsPluginOption } } }) + + if (defaultSelect != -1) instance.currentLevel = defaultSelect }) instance.on(