メディアプレーヤー
ネイティブの audio 要素や video 要素の上に重ねる、ページ独自のコントロール。
このページの内容
メディアプレーヤーは、ネイティブの <audio> または <video> に、ページ独自のコントロールを重ねます。再生を担うのは引き続きメディア要素です。メディア要素はそのセマンティクス、ブラウザーのメディアコントロールにおける位置付け、そしてフォールバックとなる controls 属性を保持します。player のビヘイビアは、適用時にその属性を取り除き、以降はメディアの状態をコントロールの行に反映します。再生ボタンは再生中に押された状態になります。シークトラックはスライダーと同じ範囲入力で、再生済みの部分の下に読み込み済みの部分が描かれます。時間は、桁が変わっても幅が変わらない小さな等幅数字で表示されます。
メディアプレーヤー
<div class="player w-full max-w(480)" role="group" aria-label="A short recording"> <audio class="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <svg class="icon player-off" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M7.633 4.7883L14.865 8.9606A1.2 1.2 0 0 1 14.865 11.0394L7.633 15.2117A1.2 1.2 0 0 1 5.8333 14.1723L5.8333 5.8277A1.2 1.2 0 0 1 7.633 4.7883Z" /> </svg> <svg class="icon player-on" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="5" y="3.75" width="3.3333" height="12.5" rx="0.8" /><rect x="11.6667" y="3.75" width="3.3333" height="12.5" rx="0.8" /> </svg> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> <button type="button" class="player-button player-mute" aria-label="Mute" aria-pressed="false"> <svg class="icon player-off" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z" /><path d="M10.3333 6C11.3333 7 11.3333 9 10.3333 10" /><path d="M12.3333 3.6667C14.6667 6 14.6667 10 12.3333 12.3333" /> </svg> <svg class="icon player-on" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z" /><path d="M11 6.3333L14.3333 9.6667" /><path d="M14.3333 6.3333L11 9.6667" /> </svg> </button> <input type="range" class="slider player-volume" min="0" max="1" step="0.05" value="1" aria-label="Volume"> <button type="button" class="player-button player-speed" popovertarget="recording-speed" aria-label="Playback speed"> 1× </button> <div id="recording-speed" popover class="menu menu-top menu-end"> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="0.75">0.75× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="1" checked>1× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="1.25">1.25× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="1.5">1.5× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="2">2× </label> </div> </div></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import PlayIcon from '@master/icons-react/native/20/soft/play'import PauseIcon from '@master/icons-react/native/20/soft/pause'import VolumeIcon from '@master/icons-react/native/16/soft/volume'import VolumeOffIcon from '@master/icons-react/native/16/soft/volume-off'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="player w-full max-w(480)" role="group" aria-label="A short recording"> <audio className="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div className="player-controls"> <button type="button" className="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon className="icon player-off" /> <PauseIcon className="icon player-on" /> </button> <span className="player-elapsed">0:00</span> <input type="range" className="slider player-seek" min="0" max="0" step="any" defaultValue="0" aria-label="Seek" /> <span className="player-duration">0:00</span> <button type="button" className="player-button player-mute" aria-label="Mute" aria-pressed="false"> <VolumeIcon className="icon player-off" /> <VolumeOffIcon className="icon player-on" /> </button> <input type="range" className="slider player-volume" min="0" max="1" step="0.05" defaultValue="1" aria-label="Volume" /> <button type="button" className="player-button player-speed" popoverTarget="recording-speed" aria-label="Playback speed"> 1× </button> <div id="recording-speed" popover="" className="menu menu-top menu-end"> <label className="menu-item"> <input className="menu-input" type="radio" name="recording-speed" value="0.75" />0.75× </label> <label className="menu-item"> <input className="menu-input" type="radio" name="recording-speed" value="1" defaultChecked />1× </label> <label className="menu-item"> <input className="menu-input" type="radio" name="recording-speed" value="1.25" />1.25× </label> <label className="menu-item"> <input className="menu-input" type="radio" name="recording-speed" value="1.5" />1.5× </label> <label className="menu-item"> <input className="menu-input" type="radio" name="recording-speed" value="2" />2× </label> </div> </div> </div> )}<script> import { attach } from '@master/ui/behaviors' import PlayIcon from '@master/icons-svelte/native/20/soft/play' import PauseIcon from '@master/icons-svelte/native/20/soft/pause' import VolumeIcon from '@master/icons-svelte/native/16/soft/volume' import VolumeOffIcon from '@master/icons-svelte/native/16/soft/volume-off'</script><div class="player w-full max-w(480)" role="group" aria-label="A short recording" {@attach attach}> <audio class="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon class="icon player-off" /> <PauseIcon class="icon player-on" /> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> <button type="button" class="player-button player-mute" aria-label="Mute" aria-pressed="false"> <VolumeIcon class="icon player-off" /> <VolumeOffIcon class="icon player-on" /> </button> <input type="range" class="slider player-volume" min="0" max="1" step="0.05" value="1" aria-label="Volume"> <button type="button" class="player-button player-speed" popovertarget="recording-speed" aria-label="Playback speed"> 1× </button> <div id="recording-speed" popover class="menu menu-top menu-end"> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="0.75">0.75× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="1" checked>1× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="1.25">1.25× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="1.5">1.5× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="2">2× </label> </div> </div></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import PlayIcon from '@master/icons-vue/native/20/soft/play'import PauseIcon from '@master/icons-vue/native/20/soft/pause'import VolumeIcon from '@master/icons-vue/native/16/soft/volume'import VolumeOffIcon from '@master/icons-vue/native/16/soft/volume-off'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="player w-full max-w(480)" role="group" aria-label="A short recording"> <audio class="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon class="icon player-off" /> <PauseIcon class="icon player-on" /> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> <button type="button" class="player-button player-mute" aria-label="Mute" aria-pressed="false"> <VolumeIcon class="icon player-off" /> <VolumeOffIcon class="icon player-on" /> </button> <input type="range" class="slider player-volume" min="0" max="1" step="0.05" value="1" aria-label="Volume"> <button type="button" class="player-button player-speed" popovertarget="recording-speed" aria-label="Playback speed"> 1× </button> <div id="recording-speed" popover class="menu menu-top menu-end"> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="0.75">0.75× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="1" checked>1× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="1.25">1.25× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="1.5">1.5× </label> <label class="menu-item"> <input class="menu-input" type="radio" name="recording-speed" value="2">2× </label> </div> </div> </div></template>#動画
player-video を指定すると、メディアプレーヤーが動画の枠になります。映像はサーフェスと同じ角丸の暗い領域を埋め、同じコントロールの行が、下端から立ち上がる影の上で映像の下端に重なります。画面は暗いものなので、コントロールの行はどちらのテーマでもダークスキームで描画されます。動画では 3 つのコントロールが加わり、それぞれ動作できる場合にのみ表示されます。字幕ボタンは動画に captions または subtitles のトラックがある場合、ピクチャーインピクチャーはブラウザーが対応している場合、全画面表示はブラウザーが許可している場合に表示されます。映像をクリックすると再生と一時停止を切り替え、ダブルクリックすると全画面表示になります。
動画の再生中にポインターが 2.5 秒間動かないか、ポインターが枠の外に出ると、コントロールの行は映像からフェードアウトし、ポインターも消えます。ポインターを少しでも動かすと元に戻ります。コントロールにキーボードフォーカスがある間や速度メニューが開いている間は、フェードアウトしません。動きを抑える設定をしている読者に対しても、フェードアウトしません。タッチスクリーンでは、コントロールがフェードアウトした映像をタップすると、コントロールが元に戻るだけです。
動画
<div class="player player-video w-full max-w(560)" role="group" aria-label="Moonrise" style="--player-ratio: 16 / 9"> <video class="player-media" src="/demo/moonrise.mp4" poster="/demo/moonrise.jpg" controls preload="metadata" playsinline> <track kind="captions" src="/demo/recording.vtt" srclang="en" label="English"> </video> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <svg class="icon player-off" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M7.633 4.7883L14.865 8.9606A1.2 1.2 0 0 1 14.865 11.0394L7.633 15.2117A1.2 1.2 0 0 1 5.8333 14.1723L5.8333 5.8277A1.2 1.2 0 0 1 7.633 4.7883Z" /> </svg> <svg class="icon player-on" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="5" y="3.75" width="3.3333" height="12.5" rx="0.8" /><rect x="11.6667" y="3.75" width="3.3333" height="12.5" rx="0.8" /> </svg> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> <button type="button" class="player-button player-mute" aria-label="Mute" aria-pressed="false"> <svg class="icon player-off" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z" /><path d="M10.3333 6C11.3333 7 11.3333 9 10.3333 10" /><path d="M12.3333 3.6667C14.6667 6 14.6667 10 12.3333 12.3333" /> </svg> <svg class="icon player-on" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M2.6 6L4.4365 6A0.6 0.6 0 0 0 4.8378 5.846L6.9986 3.9013A0.6 0.6 0 0 1 8 4.3472L8 11.6528A0.6 0.6 0 0 1 6.9986 12.0987L4.8378 10.154A0.6 0.6 0 0 0 4.4365 10L2.6 10A0.6 0.6 0 0 1 2 9.4L2 6.6A0.6 0.6 0 0 1 2.6 6Z" /><path d="M11 6.3333L14.3333 9.6667" /><path d="M14.3333 6.3333L11 9.6667" /> </svg> </button> <button type="button" class="player-button player-captions" aria-label="Captions" aria-pressed="false"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="2" y="3" width="12" height="10" rx="1" /><path d="M4.6667 6.6667L6 6.6667" /><path d="M8.6667 6.6667L11.3333 6.6667" /><path d="M4.6667 9.6667L7.3333 9.6667" /><path d="M10 9.6667L11.3333 9.6667" /> </svg> </button> <button type="button" class="player-button player-pip" aria-label="Picture in picture" aria-pressed="false"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M5.5 12L2.3333 12A1 1 0 0 1 1.3333 11L1.3333 3.6667A1 1 0 0 1 2.3333 2.6667L13 2.6667A1 1 0 0 1 14 3.6667L14 6.1667" /><rect x="8" y="8.6667" width="6.6667" height="4.6667" rx="1" /> </svg> </button> <button type="button" class="player-button player-fullscreen" aria-label="Full screen" aria-pressed="false"> <svg class="icon player-off" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M2.6667 6L2.6667 3.6667A1 1 0 0 1 3.6667 2.6667L6 2.6667" /><path d="M10 2.6667L12.3333 2.6667A1 1 0 0 1 13.3333 3.6667L13.3333 6" /><path d="M13.3333 10L13.3333 12.3333A1 1 0 0 1 12.3333 13.3333L10 13.3333" /><path d="M6 13.3333L3.6667 13.3333A1 1 0 0 1 2.6667 12.3333L2.6667 10" /> </svg> <svg class="icon player-on" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M2.6667 6L5 6A1 1 0 0 0 6 5L6 2.6667" /><path d="M10 2.6667L10 5A1 1 0 0 0 11 6L13.3333 6" /><path d="M13.3333 10L11 10A1 1 0 0 0 10 11L10 13.3333" /><path d="M6 13.3333L6 11A1 1 0 0 0 5 10L2.6667 10" /> </svg> </button> </div></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import PlayIcon from '@master/icons-react/native/20/soft/play'import PauseIcon from '@master/icons-react/native/20/soft/pause'import VolumeIcon from '@master/icons-react/native/16/soft/volume'import VolumeOffIcon from '@master/icons-react/native/16/soft/volume-off'import SubtitlesIcon from '@master/icons-react/native/16/soft/subtitles'import PictureInPictureIcon from '@master/icons-react/native/16/soft/picture-in-picture'import MaximizeIcon from '@master/icons-react/native/16/soft/maximize'import MinimizeIcon from '@master/icons-react/native/16/soft/minimize'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="player player-video w-full max-w(560)" role="group" aria-label="Moonrise" style={{ '--player-ratio': '16 / 9' } as React.CSSProperties}> <video className="player-media" src="/demo/moonrise.mp4" poster="/demo/moonrise.jpg" controls preload="metadata" playsInline> <track kind="captions" src="/demo/recording.vtt" srcLang="en" label="English" /> </video> <div className="player-controls"> <button type="button" className="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon className="icon player-off" /> <PauseIcon className="icon player-on" /> </button> <span className="player-elapsed">0:00</span> <input type="range" className="slider player-seek" min="0" max="0" step="any" defaultValue="0" aria-label="Seek" /> <span className="player-duration">0:00</span> <button type="button" className="player-button player-mute" aria-label="Mute" aria-pressed="false"> <VolumeIcon className="icon player-off" /> <VolumeOffIcon className="icon player-on" /> </button> <button type="button" className="player-button player-captions" aria-label="Captions" aria-pressed="false"> <SubtitlesIcon className="icon" /> </button> <button type="button" className="player-button player-pip" aria-label="Picture in picture" aria-pressed="false"> <PictureInPictureIcon className="icon" /> </button> <button type="button" className="player-button player-fullscreen" aria-label="Full screen" aria-pressed="false"> <MaximizeIcon className="icon player-off" /> <MinimizeIcon className="icon player-on" /> </button> </div> </div> )}<script> import { attach } from '@master/ui/behaviors' import PlayIcon from '@master/icons-svelte/native/20/soft/play' import PauseIcon from '@master/icons-svelte/native/20/soft/pause' import VolumeIcon from '@master/icons-svelte/native/16/soft/volume' import VolumeOffIcon from '@master/icons-svelte/native/16/soft/volume-off' import SubtitlesIcon from '@master/icons-svelte/native/16/soft/subtitles' import PictureInPictureIcon from '@master/icons-svelte/native/16/soft/picture-in-picture' import MaximizeIcon from '@master/icons-svelte/native/16/soft/maximize' import MinimizeIcon from '@master/icons-svelte/native/16/soft/minimize'</script><div class="player player-video w-full max-w(560)" role="group" aria-label="Moonrise" style="--player-ratio: 16 / 9" {@attach attach}> <video class="player-media" src="/demo/moonrise.mp4" poster="/demo/moonrise.jpg" controls preload="metadata" playsinline> <track kind="captions" src="/demo/recording.vtt" srclang="en" label="English"> </video> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon class="icon player-off" /> <PauseIcon class="icon player-on" /> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> <button type="button" class="player-button player-mute" aria-label="Mute" aria-pressed="false"> <VolumeIcon class="icon player-off" /> <VolumeOffIcon class="icon player-on" /> </button> <button type="button" class="player-button player-captions" aria-label="Captions" aria-pressed="false"> <SubtitlesIcon class="icon" /> </button> <button type="button" class="player-button player-pip" aria-label="Picture in picture" aria-pressed="false"> <PictureInPictureIcon class="icon" /> </button> <button type="button" class="player-button player-fullscreen" aria-label="Full screen" aria-pressed="false"> <MaximizeIcon class="icon player-off" /> <MinimizeIcon class="icon player-on" /> </button> </div></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import PlayIcon from '@master/icons-vue/native/20/soft/play'import PauseIcon from '@master/icons-vue/native/20/soft/pause'import VolumeIcon from '@master/icons-vue/native/16/soft/volume'import VolumeOffIcon from '@master/icons-vue/native/16/soft/volume-off'import SubtitlesIcon from '@master/icons-vue/native/16/soft/subtitles'import PictureInPictureIcon from '@master/icons-vue/native/16/soft/picture-in-picture'import MaximizeIcon from '@master/icons-vue/native/16/soft/maximize'import MinimizeIcon from '@master/icons-vue/native/16/soft/minimize'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="player player-video w-full max-w(560)" role="group" aria-label="Moonrise" style="--player-ratio: 16 / 9"> <video class="player-media" src="/demo/moonrise.mp4" poster="/demo/moonrise.jpg" controls preload="metadata" playsinline> <track kind="captions" src="/demo/recording.vtt" srclang="en" label="English"> </video> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon class="icon player-off" /> <PauseIcon class="icon player-on" /> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> <button type="button" class="player-button player-mute" aria-label="Mute" aria-pressed="false"> <VolumeIcon class="icon player-off" /> <VolumeOffIcon class="icon player-on" /> </button> <button type="button" class="player-button player-captions" aria-label="Captions" aria-pressed="false"> <SubtitlesIcon class="icon" /> </button> <button type="button" class="player-button player-pip" aria-label="Picture in picture" aria-pressed="false"> <PictureInPictureIcon class="icon" /> </button> <button type="button" class="player-button player-fullscreen" aria-label="Full screen" aria-pressed="false"> <MaximizeIcon class="icon player-off" /> <MinimizeIcon class="icon player-on" /> </button> </div> </div></template>動画の読み込み前から枠がその形を保つように、--player-ratio に動画の縦横比を設定します。コントロールの行は、メディアプレーヤーに与えられた幅に応じて変わります。幅が 26rem より狭いと音量スライダーが行から外れ、ミュートボタンは残ります。24rem より狭いとピクチャーインピクチャーと経過時間も外れます。そのため、スマートフォンでもすべてのコントロールが、指での操作に必要な 44px の操作領域を保ちます。
#スクリプトなしの場合
ビヘイビアが適用されるまで、メディア要素は controls 属性を保持し、スタイルシートはメディアプレーヤーのコントロールの行を非表示にします。そのため、メディアはブラウザー標準のコントロールで再生できます。これは最初の例と同じマークアップで、スクリプトはありません。
スクリプトなしの場合
<div class="player w-full max-w(480)" role="group" aria-label="A short recording"> <audio class="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <svg class="icon player-off" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M7.633 4.7883L14.865 8.9606A1.2 1.2 0 0 1 14.865 11.0394L7.633 15.2117A1.2 1.2 0 0 1 5.8333 14.1723L5.8333 5.8277A1.2 1.2 0 0 1 7.633 4.7883Z" /> </svg> <svg class="icon player-on" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="5" y="3.75" width="3.3333" height="12.5" rx="0.8" /><rect x="11.6667" y="3.75" width="3.3333" height="12.5" rx="0.8" /> </svg> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> </div></div>import PlayIcon from '@master/icons-react/native/20/soft/play'import PauseIcon from '@master/icons-react/native/20/soft/pause'<div className="player w-full max-w(480)" role="group" aria-label="A short recording"> <audio className="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div className="player-controls"> <button type="button" className="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon className="icon player-off" /> <PauseIcon className="icon player-on" /> </button> <span className="player-elapsed">0:00</span> <input type="range" className="slider player-seek" min="0" max="0" step="any" defaultValue="0" aria-label="Seek" /> <span className="player-duration">0:00</span> </div></div><script> import PlayIcon from '@master/icons-svelte/native/20/soft/play' import PauseIcon from '@master/icons-svelte/native/20/soft/pause'</script><div class="player w-full max-w(480)" role="group" aria-label="A short recording"> <audio class="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon class="icon player-off" /> <PauseIcon class="icon player-on" /> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> </div></div><script setup>import PlayIcon from '@master/icons-vue/native/20/soft/play'import PauseIcon from '@master/icons-vue/native/20/soft/pause'</script><template> <div class="player w-full max-w(480)" role="group" aria-label="A short recording"> <audio class="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon class="icon player-off" /> <PauseIcon class="icon player-on" /> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> </div> </div></template>#ビヘイビア
player は、player クラスを持つ要素であるルートを受け取り、関数を返します。この関数はビヘイビアを解除し、メディア要素にコントロールを戻します。
import { player } from '@master/ui/behaviors/player.js'const detach = player(document.querySelector('.player'))信頼できる唯一の情報源はメディア要素です。ビヘイビアはメディア要素を監視し、その状態をスタイルシートが読み取る属性に反映します。各コントロールは自身の状態を変えるのではなく、メディア要素に変更を依頼します。そのため、システムのメディアキーやほかのスクリプトなど、プレーヤーの外部から再生した場合も、再生状態は同じように表示されます。
- トグル 再生ボタンはメディアの再生中、ミュートボタンはミュート中、字幕ボタンは最初の captions または subtitles のトラックが表示されている間、ピクチャーインピクチャーボタンと全画面表示ボタンは動画がそれぞれの状態にある間、
aria-pressed="true"になります。トグルは「再生」「ミュート」のように 1 つの名前を保ち、状態はaria-pressedが示します。player-offとplayer-onの 2 つの表示を持つトグルは、状態に応じた方を表示します。表示が 1 つのトグルは、オンの間くぼんだ表示になります。 - 時間 シークトラックの
maxは再生時間の長さ、valueは現在の再生位置で、文章によるaria-valuetextを持ちます。経過時間と再生時間の長さはそれぞれの部分に書き込まれ、メディアの総再生時間が 1 時間以上の場合は、時の単位も表示されます。読者がトラックをドラッグしている間は、メディア要素が指の下のつまみを動かすことはありません。 - 読み込み済み 現在の再生位置の前後で読み込み済みの範囲は、0 から 1 までの値の
--player-bufferedとしてルートに書き込まれ、トラック上の再生済みの部分の下に描かれます。 - 音量と速度 音量スライダーは音量を保持し、ミュート中も値を保ったまま、一段控えめな表示になります。音量を本体のボタンでしか設定できないデバイスでは非表示になります。速度ボタンは、
popovertargetが指定するメニューを開きます。メニュー内の各menu-inputラジオボタンが再生速度を表し、ビヘイビアは選んだ速度をボタンに書き込み、対応するラジオボタンをチェックします。 - 字幕 オフにしたトラックは
disabledではなくhiddenになります。そのため、同じトラックを読み取るトランスクリプトはキューを保持できます。 - 全画面表示は動画ではなくメディアプレーヤー全体に適用されるため、コントロールの行は映像の上に残ります。動画しか全画面表示できない iPhone では、システム標準のプレーヤーが使われます。
- 値 返される関数は、現在の再生位置を秒で表す
valueを持ちます。設定するとシークします。
| オプション | 既定値 | |
|---|---|---|
step | 5 | 矢印キーでシークする秒数 |
jump | 10 | J と L でシークする秒数 |
volumeStep | 0.1 | ArrowUp と ArrowDown で音量が変わる量 |
idle | 2500 | 再生中の動画のコントロールがフェードアウトするまでの、ポインターが静止している時間(ミリ秒) |
valueText | 英語 | 再生位置と再生時間の長さから作る、シークトラックの aria-valuetext。ページの言語で指定します |
rateText | 1.5× | 速度ボタンに表示する、再生速度の文言 |
#キーボード操作
キー操作は、メディアプレーヤー内のどこにフォーカスがあっても、また動画の映像をクリックした後でも機能します。コントロールが必要とするキーは、そのコントロールが優先して処理します。Space はフォーカスのあるボタンを押し、矢印キーは音量スライダーを動かし、速度メニューではメニューのキー操作が使えます。
| キー | 操作 |
|---|---|
| Space, K | 再生または一時停止します。ボタン上で Space を押すと、代わりにそのボタンを押します。 |
| ArrowLeft, ArrowRight | 5 秒戻る、または 5 秒進みます。シークトラック上では ArrowDown と ArrowUp も同じ動作です。右から左のプレーヤーでは左右が入れ替わります。音量スライダー上では、ネイティブの入力と同様に矢印キーで音量を設定します。 |
| J, L | 10 秒戻る、または 10 秒進みます |
| ArrowUp, ArrowDown | シークトラックと音量スライダー以外で、音量を 10 分の 1 ずつ上げ下げします。 |
| M | ミュート、またはミュートを解除します |
| C | 動画に captions または subtitles トラックがある場合に、字幕のオンとオフを切り替えます。 |
| F | ブラウザーが許可する場合に、動画の全画面表示に入る、または全画面表示から出ます。 |
| Home, End | 先頭または末尾に移動します。音量スライダー上では最小または最大の音量に設定します。 |
#アクセシビリティ
- ルートに
role="group"とメディアの名前を示すaria-labelを指定し、どのメディアのコントロールかが読み上げで伝わるようにします。 - すべてのボタンに、その働きを表す名前をページの言語で付けます。状態が変わっても名前は変えず、状態は
aria-pressedで示します。 - 発話を含む動画には字幕が必要です。
<track kind="captions">を追加します。映像のない録音にはトランスクリプトが必要です。 - 音のあるメディアを自動再生しないでください。メディアプレーヤーは読者が求めたときに再生します。
- ボタンはどのポインターでも 32px です。タッチスクリーンでは、各ボタンの上下に操作領域が広がり、高さ 44px になります。時間の表示は桁が変わっても幅が変わらないため、再生中にトラックがずれません。
#React、Svelte、Vue
player コンポーネントは、src、video、poster、tracks からメディア要素とコントロールの行を描画してビヘイビアを適用し、現在の再生位置と再生中かどうかの受け渡しを行います。React では time と playing に onTimeChange と onPlayingChange、Svelte ではバインド可能なプロパティ、Vue では v-model:time と v-model:playing を使います。外部から設定した時間でシークし、playing で再生と一時停止を切り替えます。npx master-ui add player でプロジェクトにコピーできます。コンポーネントの構成と使い方は React、Svelte、Vue を参照してください。
#クラス
| クラス | 説明 |
|---|---|
player | 音声録音のプレーヤーです。コントロールの行を 6px 内側に収める、浮き上がったシートです。role="group" と、録音の名前を示す aria-label を付けます。 |
player-video | 動画のプレーヤーです。枠自体がサーフェスの角の丸みを持つ暗い領域になり、下端にダークスキームでコントロールを重ねます。動画の再生中にポインターが止まっていると、コントロールはフェードアウトします。player と併用します |
player-media | controls 属性を持つネイティブの audio 要素または video 要素です。標準コントロールがフォールバックとして機能し、ビヘイビアが適用されると controls 属性が取り除かれます。 |
player-controls | ボタン、時間、シークトラックを並べた行です。メディアに controls 属性がある間は非表示です。 |
player-button | アイコン 1 つを持つ控えめな 32px のコントロールで、タッチ画面では高さ 44px の操作領域を持ちます。トグルには aria-pressed と固定の名前を付けます。player-off と player-on の 2 つの表示面がある場合は状態に応じた方を表示し、表示面が 1 つの場合はオンの間くぼみに沈みます。 |
player-play | 再生と一時停止で、アイコンは一段大きくなります。メディアの再生中は押下状態です。名前は「再生」にします。player-button と併用します |
player-mute | 音声のオンとオフで、メディアのミュート中は押下状態です。名前は「ミュート」にします。player-button と併用します |
player-speed | 再生速度を数字で表示し、popovertarget で速度のメニューを開きます。メニューは label 項目で構成し、各項目は value が再生速度のラジオ menu-input を囲みます。player-button と併用します |
player-captions | 字幕のオンとオフを切り替えます。最初の captions または subtitles トラックが表示されている間は押下状態になります。動画に captions または subtitles トラックが一つもない場合は非表示になります。player-button と併用します |
player-pip | ピクチャーインピクチャーで、動画が独立したウィンドウで再生されている間は押下状態です。ブラウザーが対応していない場合や、幅 24rem 未満のプレーヤーでは非表示です。player-button と併用します |
player-fullscreen | 全画面表示で、プレーヤーが画面全体に表示されている間は押下状態です。ブラウザーが許可しない場合は非表示です。player-button と併用します |
player-off | トグルがオフの間の表示面です。再生、音声、全画面表示に入るアイコンを表示します。 |
player-on | トグルがオンの間の表示面です。一時停止、ミュート、全画面表示から出るアイコンを表示します。 |
player-elapsed | 再生済みの時間を小さな等幅数字で表示します。幅 24rem 未満のプレーヤーでは省略されます。ビヘイビアが書き込みます。 |
player-duration | メディアの総再生時間を小さな等幅数字で表示します。総再生時間が分かると、ビヘイビアが値を書き込みます。 |
player-seek | シークトラックです。行に収まる限りの長さのスライダーで、再生済みの部分の下に読み込み済みの部分を描きます。ビヘイビアが max、value、言葉で表した aria-valuetext を設定します。 |
player-volume | 音量です。ミュートボタンの横に置く 0 から 1 の短いスライダーで、幅 26rem 未満のプレーヤーでは省略され、ページから音量を設定できないデバイスでは非表示になります。 |
シークトラックと音量は slider の入力です。速度メニューは、menu-input ラジオボタンを含むラベル項目で構成した menu です。
#カスタムプロパティ
| プロパティ | 既定値 | 説明 |
|---|---|---|
--player-buffered | 0 | メディアの読み込み済みの割合を 0 から 1 の値で表し、シークトラックに描画します。値はビヘイビアが設定します。指定先:player |
--player-idle | 0 | 再生中の動画でしばらくポインターが動かず、コントロールがフェードアウトしている間は 1、それ以外は 0 です。ビヘイビアが設定します。指定先:player |
--player-ratio | 読み込み前に確保する動画の縦横比です。指定しない場合、枠は動画自体の形になります。指定先:player-video | |
--player-inset | 6px | 音声プレーヤーのシートとコントロールの間の余白指定先:player |
--player-radius | calc(var(--radius-control-sm) + var(--player-inset) / var(--corner-scale)) | プレーヤーの角の丸みです。小さいコントロールの角の丸みに inset を加えた値で、角にある 32px のボタンと同心になります。動画ではサーフェスの角の丸みになります。指定先:player |
#関連コンポーネント
- トランスクリプト録音の内容を、再生に合わせて追従するキューで表示します。
- 波形録音をバーの列で表示し、再生済みの範囲を濃く示します。
- スライダー細くくぼんだトラックに、真珠調のつまみを備えたネイティブの範囲入力。
- 図版縁を整えた、キャプション付きの画像や動画。