Web Audio Session API 用于触发应用程序中的方法调用而不是播放媒体
我很难让 Web Audio API 来做我想做的事情。我的目标是最终在移动设备上的 PWA 中触发语音识别,但首先我决定做一个测试用例,在按下播放或暂停时递增计数器。我正在使用Vue框架。
这是我的代码的缩写版本以及在屏幕上创建的 GUI。
<template>
<v-app>
<v-btn @click="activateMediaButton">Start</v-btn>
<div>{{counter}}</div>
<v-btn @click="increment">Increment</v-btn>
</v-app>
</template>
<script>
export default {
name: 'App',
data(){
return {
counter: 0
};
},
methods: {
increment(){
console.log('ssedfr');
this.counter++;
},
activateMediaButton(){
navigator.mediaSession.metadata = new window.MediaMetadata({})
navigator.mediaSession.playbackState = 'playing';
try{
navigator.mediaSession.setActionHandler('play', this.increment)
navigator.mediaSession.setActionHandler('pause',this.increment)
}
catch(error){console.log(error);}
}
}
}
</script>
如您所见,当我单击 GUI 上的开始按钮时,就会注册操作处理程序,并且每当我按下媒体按钮时计数器值就会增加。我已经使用 GUI 上的增量按钮元素测试了该计数器,它工作正常,但是当按下媒体键(如 console.log 语句所确认)时,对增量的调用甚至不会被触发。执行 try 块来注册操作时不会引发错误。
我正在通过使用笔记本电脑上的媒体键来测试此功能,否则这些键会播放和暂停任何打开的选项卡中可用的媒体。我已经关闭了所有其他媒体选项卡,以便没有任何东西拦截击键。我确实注意到,每当我在注册操作后按下媒体按钮时,焦点都会转到触发注册的“开始”按钮元素,就像在单击它一样。 我还在我的手机上通过 Netlify 托管的 PWA 和蓝牙耳机对此进行了测试,但它也不起作用。
I'm having a hard time getting the Web Audio API to do what I want. My goal is to eventually trigger voice recognition in a PWA on mobile, but first I decided to do a test case of incrementing a counter whenever play or pause was pressed. I am using the Vue framework.
Here is an abbreviated version of my code along with the GUI is creates on the screen.
<template>
<v-app>
<v-btn @click="activateMediaButton">Start</v-btn>
<div>{{counter}}</div>
<v-btn @click="increment">Increment</v-btn>
</v-app>
</template>
<script>
export default {
name: 'App',
data(){
return {
counter: 0
};
},
methods: {
increment(){
console.log('ssedfr');
this.counter++;
},
activateMediaButton(){
navigator.mediaSession.metadata = new window.MediaMetadata({})
navigator.mediaSession.playbackState = 'playing';
try{
navigator.mediaSession.setActionHandler('play', this.increment)
navigator.mediaSession.setActionHandler('pause',this.increment)
}
catch(error){console.log(error);}
}
}
}
</script>
As you can see, the action handlers are registered when I click the start button on the GUI, and the counter value is supposed to go up whenever I press the media buttons. I have tested this counter using the Increment button element on the GUI and it works fine, but the calls to increment aren't even being fired when the media keys are pressed as confirmed by console.log statements. No errors are thrown when the try block is executed to register the actions.
I am testing this functionality by using the Media keys on my laptop, which otherwise do play and pause media available in any open tabs. I have closed all other media tabs so that nothing is intercepting the keystrokes. I do notice that whenever I press the media button after the actions are registered, the focus goes to the Start button element that triggered the registration in such a way that it looks like a click is being held on it.
I have also tested this on my phone through a Netlify hosted PWA and bluetooth earphones and it doesn't work there either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用 Web Media Session API,页面必须播放媒体文件(视频或音频)。
这是一个工作示例:
要了解其工作原理,请尝试从页面手动暂停音频,然后尝试使用媒体键。
To work with Web Media Session API, the page must have to be playing a media file (video or audio).
Here is a working example:
To understand how it works try pausing the audio manually from the page and then try using the media keys.