【问题标题】:How to get the name of audio playing in a Chrome tab如何获取在 Chrome 标签中播放的音频名称
【发布时间】:2021-04-21 14:46:57
【问题描述】:

我们正在开发一个 Chrome 扩展程序,并试图弄清楚如何获取当前在 Chrome 中播放的音频的名称。我们知道,在 Chrome 中播放音频时,会有一个图标显示正在播放的音频的名称。下面的截图展示了一个例子,其中播放了一个名为“Chrome and the web in 2020”的视频。

有什么方法可以通过我们的 Chrome 扩展程序获取正在播放的视频的标题(本例中为“Chrome and the web in 2020”)?我们希望我们的扩展能够知道用户正在听什么视频或歌曲。谢谢!

【问题讨论】:

    标签: javascript google-chrome-extension google-chrome-devtools youtube-data-api web-audio-api


    【解决方案1】:

    根据Video resource 的官方文档,可以找到给定YouTube 视频的标题作为属性snippet.title 的值。

    可以通过其Videos.list 端点从 API 查询此属性(属于视频资源的任何其他属性)。

    例如,在以下 URL 上发出 curl:

    https://www.googleapis.com/youtube/v3/videos?key=$APP_KEY&id=14pb8t1lHws&part=snippet&fields=items/snippet/title&maxResults=1

    将调用将生成仅包含该视频标题的JSON response 文本的端点:

    {
      "items": [
        {
          "snippet": {
            "title": "Chrome and the web in 2020"
          }
        }
      ]
    }
    

    对于 Javascript GAPI(即 Google 的浏览器端 JavaScript 客户端库)实现,代码如下所示:

    var app_key = ...;
    var videoTitle;
    
    function loadAPIClientInterfaces() {
      gapi.client.load('youtube', 'v3', function() {
        getVideoTitle('14pb8t1lHws');
      });
    }
    
    function getVideoTitle(videoId) {
      var request = gapi.client.youtube.videos.list({
        id: videoId,
        part: 'snippet',
        fields: 'items/snippet/title',
        key: app_key
      });
      request.execute(function(response) {
        if ('error' in response) {
          displayMessage(response.error.message);
        } else {
          videoTitle = response.items[0].snippet.title;
        }
      });
    }
    

    请注意,上面的代码使用fields 请求参数从Videos.list 端点仅获取视频的标题(最好从API 仅询问实际使用的信息)。

    另请注意,$APP_KEY 和 app_key 是(shell 和,分别是 Javascript)变量,设置为包含与通过 Google's developers console 创建的项目关联的有效 API 密钥。

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2021-08-19
      • 2014-11-16
      • 1970-01-01
      相关资源
      最近更新 更多