首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在chrome扩展中实现带声音的通知弹出窗口

如何在chrome扩展中实现带声音的通知弹出窗口
EN

Stack Overflow用户
提问于 2013-02-17 10:56:55
回答 2查看 20.2K关注 0票数 20

如何在chrome扩展中使用声音实现通知弹出窗口。

就像一样

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-17 15:07:05

您可以使用以下代码作为在桌面通知中播放声音的参考,它结合使用了<audio>标签和Desktop Notifications

游行示威

manifest.json

已注册的通知权限和包含清单文件的背景页。

代码语言:javascript
运行
复制
{
    "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

已从后台应用程序创建通知页面。

代码语言:javascript
运行
复制
// create a HTML notification:
var notification = webkitNotifications.createHTMLNotification(
    'notification.html' // html url - can be relative
);

// Then show the notification.
notification.show();

notification.html

播放一些随机的歌曲

代码语言:javascript
运行
复制
<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>

参考文献

票数 7
EN

Stack Overflow用户

发布于 2014-01-05 11:41:47

我认为自从被接受的答案被写出来后,createHTMLNotification已经被弃用了。对于现在这个线程上发生的任何人,这里有一个方法,假设你在清单中有notifications权限,那么这个方法在2014年1月就可以工作:

background.js

代码语言:javascript
运行
复制
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方法在通知创建后立即播放声音。当然,还有其他方法来完成和组织它,但我认为这在大多数情况下都是非常清晰和容易实现的。

票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14917531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档