我正在使用 React 和 Electron 编写音乐播放器,并希望为音频添加元数据,以便获得 MPRIS 支持。所以我做了一些搜索,发现Media Session API正是我所需要的。为了测试它,我将上述链接包含的示例代码中的代码复制到我项目中的一个函数中。
navigator.mediaSession.metadata = new MediaMetadata({
title: 'Unforgettable',
artist: 'Nat King Cole',
album: 'The Ultimate Collection (Remastered)',
artwork: [
{ src: 'https://dummyimage.com/96x96', sizes: '96x96', type: 'image/png' },
{ src: 'https://dummyimage.com/128x128', sizes: '128x128', type: 'image/png' },
{ src: 'https://dummyimage.com/192x192', sizes: '192x192', type: 'image/png' },
{ src: 'https://dummyimage.com/256x256', sizes: '256x256', type: 'image/png' },
{ src: 'https://dummyimage.com/384x384', sizes: '384x384', type: 'image/png' },
{ src: 'https://dummyimage.com/512x512', sizes: '512x512', type: 'image/png' },
]
});
我把它放到一个函数中,当播放下一首歌曲时,组件会调用它。但是,我得到了这样的错误:
Failed to compile.
./src/components/FM/Cover/index.jsx
Line 112: 'MediaMetadata' is not defined no-undef
Search for the keywords to learn more about each error.
为什么?
您需要引用该window
对象。
window.MediaMetadata
^^^^^^
这是因为 babel/es-lint 不知道MediaMetadata
存在并且会抛出错误。由于 Chrome 将此对象作为全局变量注入,因此可以通过该window
对象访问。
(重新发布@AngelSalazar 在 OP 的评论中所说的内容以方便使用)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句