我试图在用户提交聊天时发出声音,对方也会听到。这是我的代码:
Enter Chat and press enter
<div><input id=input placeholder=you-chat-here /></div>
<code>Chat Output</code>
<div id=box></div>
<div id=pubnub pub-key=demo sub-key=demo></div>
<script src=http://cdn.pubnub.com/pubnub-3.1.min.js></script>
<script>(function(){
var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chatlllll';
PUBNUB.subscribe({
channel : channel,
callback : function(text) {
box.innerHTML =
(''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML;
}
});
PUBNUB.bind( 'keyup', input, function(e) {
(e.keyCode || e.charCode) === 13 && PUBNUB.publish({
playsound('http://www.aphpsite.comuv.com/sound/chat.wav')
channel : channel,
message : input.value,
x : (input.value='')
});
});
})();</script>
这是我的。我在添加声音时遇到问题。这个脚本坏了。所以这些都不起作用。我希望有人能修复它。
谢谢。
最佳答案
您询问的是 PubNub 和一个聊天消息到达/发送时具有音效的示例聊天应用程序。我已经更新了示例并提供了一个额外的 sound.js JavaScript HTML5 库,这将有助于播放音效。请注意,我将您的声音 WAV 文件转换为 OGG 和 MP3 文件格式,以便提供跨浏览器兼容性。接下来,我将粘贴接收消息时带有音效聊天的完整有效源代码。在源代码之后,我粘贴了您需要的 URL 资源,例如 sound.js 和 audio 文件。
尝试LIVE! - http://pubnub-demo.s3.amazonaws.com/chat-with-sounds/chat.html
查看源代码:
<div><input id=input placeholder=chat-here></div>
<code>Chat Output</code>
<div id=box></div>
<div id=pubnub pub-key=demo sub-key=demo></div>
<script src=http://cdn.pubnub.com/pubnub-3.1.min.js></script>
<script src=sound.js></script>
<script>(function(){
var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chatlllll';
PUBNUB.subscribe({
channel : channel,
callback : function(text) {
// PLAY SOUND HERE
sounds.play('chat');
// UPDATE TEXT OUTPUT HERE
box.innerHTML =
(''+text).replace( /[<>]/g, '' ) +
'<br>' +
box.innerHTML;
}
});
PUBNUB.bind( 'keyup', input, function(e) {
(e.keyCode || e.charCode) === 13 && PUBNUB.publish({
channel : channel,
message : input.value,
x : (input.value='')
});
});
})();</script>
在 GitHub 上下载源代码
https://github.com/pubnub/pubnub-api/tree/master/app-showcase/chat-with-sounds - 单击链接访问 PubNub GitHub 存储库,其中包含与声音聊天演示的源代码。
关于Javascript + Pubnub 聊天通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532364/