Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Звуковое уведомление на js в чате (https://javascript.ru/forum/misc/77608-zvukovoe-uvedomlenie-na-js-v-chate.html)

nikeyb 26.05.2019 19:23

Звуковое уведомление на js в чате
 
Добрый вечер! Нужна помощь со звуковым уведомление в чате.
ЭТО НЕ РАБОТАЮЩИЙ ФРАГМЕНТ КОДА, в который нужно добавить на js возможность проигрование звука после отправки сообщения в чат:
$(document).ready(function() {
  $("#exit").click(function() { //Если пользователь хочет выйти
   if (confirm("Выйти из чата?")==true) { window.location = 'index.php?logout=true'; }
  });
  $('#message').submit(function(e) { //Если пользователь отправил сообщение
   var clientmsg = $("#usermsg").val();
   $.post("post.php", {text: clientmsg});
   $("#usermsg").attr("value", "");
   return false;
  });
  function loadLog() { //Загрузить лог чата
   var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
   $.ajax ({
    url: "log.html",
    cache: false,
    success: function(html) {
     $("#chatbox").html(html); //Автопрокрутка
     var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
     if (newscrollHeight > oldscrollHeight) {
      $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal');
     }
    }
   });
  }
  //Обновление каждые 2.5с
  setInterval (loadLog, 1500);
 });


А ЭТО РАБОТАЮЩИЙ ФРАГМЕНТ КОДА где приходит звуковое оповещение
$(function(){  
	$("#usermsg").focus(); 
  $('<audio id="chatAudio"><source src="notify.ogg" type="audio/ogg"><source src="notify.mp3" type="audio/mpeg"><source src="notify.wav" type="audio/wav"></audio>').appendTo('body');
  $("#submitmsg").on("click",function(){
    var a = $("#usermsg").val().trim();
    if(a.length > 0){
	$("#usermsg").val('');   
	$("#usermsg").focus(); 
     $("<li></li>").html('<img src="small.jpg"/><span>'+a+'</span>').appendTo("#chatMessages");
	  $("#chat").animate({"scrollTop": $('#chat')[0].scrollHeight}, "slow");
      $('#chatAudio')[0].play();
    }
  });
});

Vlasenko Fedor 28.05.2019 00:28

speechSynthesis.speak(new SpeechSynthesisUtterance('Глянь чат, письмо пришло'));

Вариант, прочитать текст, сообщение

Dilettante_Pro 28.05.2019 12:25

Poznakomlus,
В консоли сообщение:
Цитата:

VM14:1 [Deprecation] speechSynthesis.speak() without user activation is no longer allowed since M71, around December 2018. See https://www.chromestatus.com/feature/5687444770914304 for more details


Часовой пояс GMT +3, время: 21:55.