检测< audio>是否用户交互或缓冲区不足触发了"pause"事件? [英] Detect if <audio> `pause` event fired by user interaction or buffer underrun?

查看:47
本文介绍了检测< audio>是否用户交互或缓冲区不足触发了"pause"事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当通过 < audio> 控制中心

  • Android:浏览器的通知抽屉(至少是Chrome,Opera,Firefox)
  • 桌面:媒体会话API 控件,但未初始化,而没有显式的 setActionHandler (到目前为止可能已被隐藏在标志后面)
  • 各种网络状况导致的缓冲区不足
  • 是否可以区分1/2和3?

    • 理想情况下,会有一个事件属性,例如 isTrusted ,我想念
    • 我想猜,尤其是.在 readyState networkState ,但两者都是不确定的,尤其是在浏览器中(例如 HAVE_FUTURE_DATA HAVE_ENOUGH_DATA 的解释/语义)
    • 我回避制作正在衰减的状态机",而是在处理其他事件.缓冲区欠载通常是 ,前是 stalled 事件,有时是 ,后是 end 事件.跨浏览器的实现似乎很复杂,而且误报的风险非常高.

    我是否很幸运,直到媒体会议到处都是?

    注意:此问题看起来像是一种解决方案,但不幸的是,它不是-浏览器以不同且不一致的方式处理实时流的结束".

    解决方案

    如果您要在用户暂停音频时启动事件,则此代码段将完成此工作.我没有在通知抽屉中的移动设备上对其进行测试,但我认为它可以工作.

      const video = document.querySelector('video');video.addEventListener('pause',(event)=> {console.log('布尔值暂停属性现在为true.或者是+"pause()方法已被调用或自动播放属性已切换.");}); 

    资源:音频元素事件

    资源:暂停事件

    >

    我也找到了一个有用的答案,可以帮助您 2 (至少从我的理解)以及为什么这是一种不好的技术.链接到问题

    3 事件:已暂停/正在等待检查事件资源

    When playing a live audio stream, like web radio, through <audio> or Audio(), the pause event can fire in (at least) three ways:

    1. user clicks on the pause button (with <audio controls>)
    2. user clicks the browsers global audio controls
      • iOS: Control Center
      • Android: browser's notification drawer (at least Chrome, Opera, Firefox)
      • Desktop: Media Session API controls, but uninitialized, without explicit setActionHandler (might be hidden behind a flag as of now)
    3. a buffer underrun caused by various network conditions

    Is it possible to distinguish between 1/2 and 3?

    • Ideally, there would be an event property like isTrusted, which I am missing
    • I have tried to guess, looking esp. at readyState and networkState, but both are very inconclusive, especially across browsers (e.g. the interpretation/semantics of HAVE_FUTURE_DATA vs HAVE_ENOUGH_DATA)
    • I have shied away from making a "decaying state machine", juggling other events. A buffer underrun is often preceded by stalled events, and sometimes followed by ended events. A cross-browser implementation seems crazy complex and the danger of false positives very high.

    Am I out of luck until Media Session lands everywhere?

    Note: this question looks like a solution, but unfortunately isn't -- browsers handle live streams' "ends" differently and inconsistently.

    解决方案

    If you want to start an event when the user pauses the audio then this snippet will do the job. I didn't test it on mobile in the notification drawer but I think it'll work.

    const video = document.querySelector('video');
    
    video.addEventListener('pause', (event) => {
      console.log('The Boolean paused property is now true. Either the ' + 
      'pause() method was called or the autoplay attribute was toggled.');
    });
    

    resource: audio element events

    resource: pause event

    I also found a helpful answer to what you are trying to do 2 (at least from what I understand) and why it's a bad technique. Link to question

    3 Events: stalled / waiting check the events resource

    这篇关于检测&lt; audio&gt;是否用户交互或缓冲区不足触发了"pause"事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆