Chrome拡張機能でサウンド付きの通知ポップアップを実装する方法。
20429 次
2 に答える
34
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 メソッドを使用して、通知の作成直後にサウンドを再生するということです。もちろん、それを実行して整理する方法は他にもありますが、ほとんどの場合、これは非常に明確で非常に簡単に実装できると思います。
于 2014-01-05T03:41:46.747 に答える
7
次のコードは、デスクトップ通知でサウンドを再生するためのリファレンスとして使用できます。<audio>
タグは と組み合わせて使用されDesktop Notifications
ます。
デモンストレーション
マニフェスト.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();
通知.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>
参考文献
于 2013-02-17T07:07:05.423 に答える