如何在chrome扩展中使用声音实现通知弹出窗口。
就像一样
发布于 2013-02-17 15:07:05
您可以使用以下代码作为在桌面通知中播放声音的参考,它结合使用了<audio>
标签和Desktop Notifications
。
游行示威
manifest.json
已注册的通知权限和包含清单文件的背景页。
{
"name": "Notification with Audio",
"description": "http://stackoverflow.com/questions/14917531/how-to-implement-a-notification-popup-with-sound-in-chrome-extension",
"manifest_version": 2,
"version": "1",
"permissions": [
"notifications"
],
"background": {
"scripts": [
"background.js"
]
}
}
background.js
已从后台应用程序创建通知页面。
// create a HTML notification:
var notification = webkitNotifications.createHTMLNotification(
'notification.html' // html url - can be relative
);
// Then show the notification.
notification.show();
notification.html
播放一些随机的歌曲
<html>
<body>
<p>Some Nice Text While Playing Song.. </p>
<audio autoplay>
<source src="http://www.html5rocks.com/en/tutorials/audio/quick/test.mp3" type="audio/mpeg" />
<source src="http://www.html5rocks.com/en/tutorials/audio/quick/test.ogg" type="audio/ogg" />
</audio>
</body>
</html>
参考文献
发布于 2014-01-05 11:41:47
我认为自从被接受的答案被写出来后,createHTMLNotification
已经被弃用了。对于现在这个线程上发生的任何人,这里有一个方法,假设你在清单中有notifications
权限,那么这个方法在2014年1月就可以工作:
background.js
createNotification();
audioNotification();
function audioNotification(){
var yourSound = new Audio('yourSound.mp3');
yourSound.play();
}
function createNotification(){
var opt = {type: "basic",title: "Your Title",message: "Your message",iconUrl: "your_icon.png"}
chrome.notifications.create("notificationName",opt,function(){});
//include this line if you want to clear the notification after 5 seconds
setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},5000);
}
这里的一般思想是,您将发出常规的通知,然后使用普通的JavaScript方法在通知创建后立即播放声音。当然,还有其他方法来完成和组织它,但我认为这在大多数情况下都是非常清晰和容易实现的。
https://stackoverflow.com/questions/14917531
复制相似问题