MediaSession.setPositionState()并寻求不工作

发布于 2025-01-30 02:45:29 字数 693 浏览 6 评论 0原文

MediaSession.setPositionState()未显示音频时间, 也不按预期显示Seekbar。

const audio= document.querySelector('audio');

function updatePositionState() {
  if ('setPositionState' in navigator.mediaSession) {
    navigator.mediaSession.setPositionState({
      duration: audio.duration,
      playbackRate: audio.playbackRate,
      position: audio.currentTime,
    });
  }
}

await audio.play();
updatePositionState();

navigator.mediaSession.setActionHandler('seekto', (details) => {
  updatePositionState();
});

MediaSession.setPositionState() not showing the audio time,
also seekbar not showing as expected.

const audio= document.querySelector('audio');

function updatePositionState() {
  if ('setPositionState' in navigator.mediaSession) {
    navigator.mediaSession.setPositionState({
      duration: audio.duration,
      playbackRate: audio.playbackRate,
      position: audio.currentTime,
    });
  }
}

await audio.play();
updatePositionState();

navigator.mediaSession.setActionHandler('seekto', (details) => {
  updatePositionState();
});

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍孤女 2025-02-06 02:45:29

这对我有用:

audio = document.querySelector("audio");
navigator.mediaSession.setActionHandler('seekto', (details) => {
        audio.currentTime = details.seekTime;
    });

This is what worked for me:

audio = document.querySelector("audio");
navigator.mediaSession.setActionHandler('seekto', (details) => {
        audio.currentTime = details.seekTime;
    });
长不大的小祸害 2025-02-06 02:45:29
function setup_media_session_action_handlers() {
    if (!("mediaSession" in navigator)) {
        write2log(`No mediaSession in navigator.`,2);
    }
    const audio_element = document.querySelector('audio');
    const default_skip_time = 60; /* Time to skip in seconds by default */

    const action_handlers = [
        ['play',            () => { audio_element.play(); }],
        ['pause',           () => { audio_element.pause(); }],
        ['previoustrack',   () => { audio_element.currentTime = 0; }],
        ['nexttrack',       () => { load_song_from_index(); }],
        ['stop',            () => {audio_element.pause(); }],
        ['seekbackward',    (details) => {  const skip_time = details.seekOffset || default_skip_time; audio_element.currentTime = Math.max(audio_element.currentTime - skip_time, 0); navigator.mediaSession.setPositionState({duration: audio_element.duration, playbackRate: audio_element.playbackRate, position: audio_element.currentTime});}],
        ['seekforward',     (details) => {  const skip_time = details.seekOffset || default_skip_time; audio_element.currentTime = Math.min(audio_element.currentTime + skip_time, 0); navigator.mediaSession.setPositionState({duration: audio_element.duration, playbackRate: audio_element.playbackRate, position: audio_element.currentTime});}],
        ['seekto',      (details) => {  if (details.fastSeek && 'fastSeek' in audio_element) { audio_element.fastSeek(details.seekTime); } else { audio_element.currentTime = details.seekTime; } navigator.mediaSession.setPositionState({duration: audio_element.duration, playbackRate: audio_element.playbackRate, position: audio_element.currentTime});}],

        // Not implemented: ['togglemicrophone', 'togglecamera', 'hangup', 'previousslide', and 'nextslide'];
        /* Video conferencing actions */
        //['togglemicrophone',  () => { await audio_element.pause(); /* On any microphone usage (on or off), just set the audio to pause. */ }],
        //['togglecamera',  () => { await audio_element.pause(); /* On any camera usage (on or off), just set the audio to pause. */ }],
        //['hangup',        () => { if (audio_element.currentTime > 0) { await audio_element.play(); } /* On hangup, simplified (not bulletproof) version of unmute if we were playing music before */}],
        /* Presenting slides actions */ // Not implemented at this time, placeholder for future? idk.
        //['previousslide', () => { /* ... */ }],
        //['nextslide',     () => { /* ... */ }],
    ];
    for (const [action, handler] of action_handlers) {
        try {
            navigator.mediaSession.setActionHandler(action, handler);
        } catch (error) {
            write2log(`The media session action "${action}" is not supported yet.`,2);
        }
    }
    try {
        // Set playback event listeners
        audio_element.addEventListener('play', () => { navigator.mediaSession.playbackState = 'playing'; });
        audio_element.addEventListener('pause', () => { navigator.mediaSession.playbackState = 'paused'; });
    }
    catch (err) {
        write2log(`Failed to set navigator.mediaSession.playbackState play/pause handlers`,2);
    }
}

参见 https:> https:/ https://noah-github.com/noah-jaffe/mjaffe/mmixcloud_play_play_play_playerer/blob /main/musicplayer.html 对于此精确函数的用例,所有音频媒体控件均已在set> setup_media_session_action_action_handlers()函数中实现。

在使用Android Chrome查看HTML页面上的Android设备上的动作中,如下

function setup_media_session_action_handlers() {
    if (!("mediaSession" in navigator)) {
        write2log(`No mediaSession in navigator.`,2);
    }
    const audio_element = document.querySelector('audio');
    const default_skip_time = 60; /* Time to skip in seconds by default */

    const action_handlers = [
        ['play',            () => { audio_element.play(); }],
        ['pause',           () => { audio_element.pause(); }],
        ['previoustrack',   () => { audio_element.currentTime = 0; }],
        ['nexttrack',       () => { load_song_from_index(); }],
        ['stop',            () => {audio_element.pause(); }],
        ['seekbackward',    (details) => {  const skip_time = details.seekOffset || default_skip_time; audio_element.currentTime = Math.max(audio_element.currentTime - skip_time, 0); navigator.mediaSession.setPositionState({duration: audio_element.duration, playbackRate: audio_element.playbackRate, position: audio_element.currentTime});}],
        ['seekforward',     (details) => {  const skip_time = details.seekOffset || default_skip_time; audio_element.currentTime = Math.min(audio_element.currentTime + skip_time, 0); navigator.mediaSession.setPositionState({duration: audio_element.duration, playbackRate: audio_element.playbackRate, position: audio_element.currentTime});}],
        ['seekto',      (details) => {  if (details.fastSeek && 'fastSeek' in audio_element) { audio_element.fastSeek(details.seekTime); } else { audio_element.currentTime = details.seekTime; } navigator.mediaSession.setPositionState({duration: audio_element.duration, playbackRate: audio_element.playbackRate, position: audio_element.currentTime});}],

        // Not implemented: ['togglemicrophone', 'togglecamera', 'hangup', 'previousslide', and 'nextslide'];
        /* Video conferencing actions */
        //['togglemicrophone',  () => { await audio_element.pause(); /* On any microphone usage (on or off), just set the audio to pause. */ }],
        //['togglecamera',  () => { await audio_element.pause(); /* On any camera usage (on or off), just set the audio to pause. */ }],
        //['hangup',        () => { if (audio_element.currentTime > 0) { await audio_element.play(); } /* On hangup, simplified (not bulletproof) version of unmute if we were playing music before */}],
        /* Presenting slides actions */ // Not implemented at this time, placeholder for future? idk.
        //['previousslide', () => { /* ... */ }],
        //['nextslide',     () => { /* ... */ }],
    ];
    for (const [action, handler] of action_handlers) {
        try {
            navigator.mediaSession.setActionHandler(action, handler);
        } catch (error) {
            write2log(`The media session action "${action}" is not supported yet.`,2);
        }
    }
    try {
        // Set playback event listeners
        audio_element.addEventListener('play', () => { navigator.mediaSession.playbackState = 'playing'; });
        audio_element.addEventListener('pause', () => { navigator.mediaSession.playbackState = 'paused'; });
    }
    catch (err) {
        write2log(`Failed to set navigator.mediaSession.playbackState play/pause handlers`,2);
    }
}

See https://github.com/Noah-Jaffe/Mixcloud_player/blob/main/MUSICPLAYER.html for a use case of this exact function, all of the audio media controls are implemented inside the setup_media_session_action_handlers() function.

in action on android device viewing html page with Android Chrome it looks like the following
in action on android device viewing html page with Android Chrome

じ违心 2025-02-06 02:45:29

我遇到了同样的事情。

然后,我正在进行一些清理,并决定目前不需要以下内容:

navigator.mediaSession.setActionHandler("seekbackward", (data) => {
        console.log('seekBackward: data: ', data);
        // seekBackward: data: { action: 'seekbackward' }
      });
      navigator.mediaSession.setActionHandler("seekforward", (data) => {
        console.log('seekForward: data: ', data);
        // seekForward: data:  {action: 'seekforward'}
      });

一旦我对此进行了评论,现在在我的移动设备上可以看到Seek Bar。在桌面上仍然看不到。我怀疑MediaSession API的工作方式可能是您向前和向后的按钮跳过,那么Seekto将被禁用。

现在,Seekto bar是可见的,可以触发,但这是不正确的。当轨道启动时,已经走了四分之一。

我似乎找不到在菜谱上在菜谱中设置曲目的运行时的方法。

我在某个地方读到,甚至不可能在溪流上有搜索栏。

I encountered the same thing.

Then I was doing some cleaning up and decided that the below was not required for now:

navigator.mediaSession.setActionHandler("seekbackward", (data) => {
        console.log('seekBackward: data: ', data);
        // seekBackward: data: { action: 'seekbackward' }
      });
      navigator.mediaSession.setActionHandler("seekforward", (data) => {
        console.log('seekForward: data: ', data);
        // seekForward: data:  {action: 'seekforward'}
      });

Once I had commented this out, the seek bar was now visible on my Mobile Device. On the desktop it was still not visible. I suspect that maybe the way the MediaSession API works is if you have the skip forward and backward buttons, then the seekTo is disabled.

Now, the seekTo bar is visible and can be triggered but it is incorrect. When a track starts, it's already a quarter of the way through.

I can't seem to find a way to set the runtime of the track in the MedaSession API on a stream.

I had read somewhere that it was not possible to even have a seek bar on a stream. ????‍♀️

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文