我今天一直在寻找并找到了一些答案,例如:
<head>
<title>Audio test</title>
<script type="text/javascript">
// @param filename The name of the file WITHOUT ending
function playSound(filename){
document.getElementById("sound").innerHTML='<audio autoplay="autoplay"><source src="' + filename + '.mp3" type="audio/mpeg" /><source src="' + filename + '.ogg" type="audio/ogg" /><embed hidden="true" autostart="true" loop="false" src="' + filename +'.mp3" /></audio>';
}
</script>
</head>
<body>
<button onclick="playSound('bing');">Play</button>
<div id="sound"></div>
</body>
对于上面的答案,我认为我只需要输入正确的文件名(例如mydomain.com/correctfilename.mp3),其中'filename'在该脚本中。
但我正在寻找一些不同的东西,加快时间。我想知道我是否可以在网站打开时播放“默认”Android通知声音。我在这里找到了类似于我需要的东西:
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
但是当我把它放在HTML文件中时,它不起作用,我认为这是因为它不是javascript。
我知道有一些方法可以访问你的手机让它振动的任何东西,所以我想可以有一种方法来访问它预装的声音。我可以用脚本执行此操作吗?
答案 0 :(得分:0)
我想你问的是,你是否可以从网页上做某事,而不是从设备上的特定应用程序做某事 - 以这种方式使用WebView。
对于通过浏览器访问设备服务的任何网页,都有W3C Device API Working Group,其中包含Vibration API,甚至是works already on Android Chrome Beta 39,其他人也是如此:
navigator.vibrate(2000);
然而,我并不知道某种方式可以播放遵循设备环境的默认声音/振动,例如Windows API handles it。