Posts: 300
Threads: 81
Joined: May 2017
Reputation:
0
(24.12.2020, 08:46) admin Wrote: This should do what you want. You can change the interval (it is 2000 milliseconds now) if needed.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
var interval ;
//
Function to play audio by URL
function Play_Audio (
url ){
//
console.log (
OS_Version )
if (
OS_Version ===
'Android' ||
OS_Version ===
'U.Motion' ){
//
alert (
OS_Version );
snd.src =
url ;
snd.pause ();
sndcurrentTime =
0 ;
//
snd.load ();
snd.play ();
}
else {
request.open (
"GET" ,
url ,
true );
request.responseType =
"arraybuffer" ;
request.onload =
function (){
ctx.decodeAudioData (
request.response ,
onDecoded );
}
function onDecoded (
buffer ){
var bufferSource =
ctx.createBufferSource ();
bufferSource.buffer =
buffer ;
bufferSource.connect (
ctx.destination );
bufferSource.start ();
}
request.send ();
}
};
//
Function to create event listeners
function CreateListeners (
groupaddr ,
url ){
//
Make event listener to object
if (
typeof grp !=
'undefined' ) {
grp.listen (
groupaddr ,
function (
object ,
state ) {
var value =
object.value ;
var enabled =
grp.getvalue (
'32/2/1' );
clearInterval (
interval );
if (
state ==
'value' &&
value ==
1 &&
enabled ) {
Play_Audio (
url );
interval =
setInterval (
function () {
Play_Audio (
url );
},
2000 );
}
},
true );
}
}
//
Start creating listeners
for (
var i in AudioTable ) {
CreateListeners (
AudioTable [
i ].
address ,
AudioTable [
i ].
url );
}
});
Great! Thank you so much. And best wishes!
Peppe
Posts: 300
Threads: 81
Joined: May 2017
Reputation:
0
(21.12.2020, 07:32) admin Wrote: You can use grp.taglisten(tag_name, callback_function, all_events) instead of grp.listen(group_address, callback_function, all_events) for this
I tried to use grp.taglisten instead of grp.listen... but evidently I made some mistake:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
//
allarme sonoro
//
Closing current script section to add elements
</
script >
<
audio id =
"SE_Audio" style =
"display: none;" ></
audio >
<
script type =
"text/javascript" > //
Starting new script section after adding elements
$(
function (){
//
Table with parameters
var AudioTable = [
{
tag_name :
"beep" ,
url :
"/scada/resources/img/beep.mp3" },
// {
address :
"32/2/1" ,
url :
"/user/audio2.mp3" },
//{
address :
"32/2/2" ,
url :
"/user/audio3.mp3" },
];
var ctx =
new (
window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext ),
request =
new XMLHttpRequest ();
var snd =
document.getElementById (
"SE_Audio" );
function getMobileOperatingSystem () {
var userAgent =
navigator.userAgent ||
navigator.vendor ||
window.opera ;
//
console.log (
userAgent )
if (
userAgent.match (/
iPad /
i ) ||
userAgent.match (/
iPhone /
i ) ||
userAgent.match (/
iPod /
i )) {
return 'iOS' ;
}
else if (
userAgent.match (/
Android /
i )) {
return 'Android' ;
}
else if (
userAgent.match (/
iKonWebTouch /
i )) {
return 'U.Motion' ;
}
else {
return 'PC' ;
}
}
var OS_Version =
getMobileOperatingSystem ();
//
Fix iOS Audio Context
(
function () {
var fixAudioContext =
function (
e ) {
if (
ctx ) {
//
Create empty buffer
var buffer =
ctx.createBuffer (
1 ,
1 ,
22050 );
var source =
ctx.createBufferSource ();
source.buffer =
buffer ;
//
Connect to output
source.connect (
ctx.destination );
//
Play sound
if (
source.start ) {
source.start (
0 );
}
else if (
source.play ) {
source.play (
0 );
}
else if (
source.noteOn ) {
source.noteOn (
0 );
}
}
//
Check if document is loaded in iframe
if (
window.frameElement ){
//
Remove event listeners from parent
var thisparent =
window.parent ;
thisparent.document.removeEventListener (
'touchstart' ,
fixAudioContext );
thisparent.document.removeEventListener (
'touchend' ,
fixAudioContext );
}
//
Remove events
document.removeEventListener (
'touchstart' ,
fixAudioContext );
document.removeEventListener (
'touchend' ,
fixAudioContext );
};
//
Check if document is loaded in iframe
if (
window.frameElement ){
//
Add event listeners to parent
var thisparent =
window.parent ;
//
Event listener for iOS 6 -
8 (
was previous touchstart event )
thisparent.document.addEventListener (
'touchstart' ,
fixAudioContext );
//
Event listener for iOS 9 + (
is now touchend event )
thisparent.document.addEventListener (
'touchend' ,
fixAudioContext );
}
//
Event listener for iOS 6 -
8 (
was previous touchstart event )
document.addEventListener (
'touchstart' ,
fixAudioContext );
//
Event listener for iOS 9 + (
is now touchend event )
document.addEventListener (
'touchend' ,
fixAudioContext );
})();
var interval ;
//
Function to play audio by URL
function Play_Audio (
url ){
//
console.log (
OS_Version )
if (
OS_Version ===
'Android' ||
OS_Version ===
'U.Motion' ){
//
alert (
OS_Version );
snd.src =
url ;
snd.pause ();
sndcurrentTime =
0 ;
//
snd.load ();
snd.play ();
}
else {
request.open (
"GET" ,
url ,
true );
request.responseType =
"arraybuffer" ;
request.onload =
function (){
ctx.decodeAudioData (
request.response ,
onDecoded );
}
function onDecoded (
buffer ){
var bufferSource =
ctx.createBufferSource ();
bufferSource.buffer =
buffer ;
bufferSource.connect (
ctx.destination );
bufferSource.start ();
}
request.send ();
}
};
//
Function to create event listeners
function CreateListeners (
tag_name ,
url ){
//
Make event listener to object
if (
typeof grp !=
'undefined' ) {
grp.taglisten (
tag_name ,
function (
object ,
state ) {
var value =
object.value ;
var enabled =
grp.getvalue (
'32/1/21' );
clearInterval (
interval );
if (
state ==
'value' &&
value ==
1 &&
enabled ) {
Play_Audio (
url );
interval =
setInterval (
function () {
Play_Audio (
url );
},
2000 );
}
},
true );
}
}
//
Start creating listeners
for (
var i in AudioTable ) {
CreateListeners (
AudioTable [
i ].
tag_name ,
AudioTable [
i ].
url );
}
});
Best Wishes
Peppe
Posts: 1806
Threads: 7
Joined: Jul 2015
Reputation:
121
This should work:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//
Closing current script section to add elements
</
script >
<
audio id =
"SE_Audio" style =
"display: none;" ></
audio >
<
script type =
"text/javascript" > //
Starting new script section after adding elements
$(
function (){
var ctx =
new (
window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext ),
request =
new XMLHttpRequest ();
var snd =
document.getElementById (
"SE_Audio" );
function getMobileOperatingSystem () {
var userAgent =
navigator.userAgent ||
navigator.vendor ||
window.opera ;
//
console.log (
userAgent )
if (
userAgent.match (/
iPad /
i ) ||
userAgent.match (/
iPhone /
i ) ||
userAgent.match (/
iPod /
i )) {
return 'iOS' ;
}
else if (
userAgent.match (/
Android /
i )) {
return 'Android' ;
}
else if (
userAgent.match (/
iKonWebTouch /
i )) {
return 'U.Motion' ;
}
else {
return 'PC' ;
}
}
var OS_Version =
getMobileOperatingSystem ();
//
Fix iOS Audio Context
(
function () {
var fixAudioContext =
function (
e ) {
if (
ctx ) {
//
Create empty buffer
var buffer =
ctx.createBuffer (
1 ,
1 ,
22050 );
var source =
ctx.createBufferSource ();
source.buffer =
buffer ;
//
Connect to output
source.connect (
ctx.destination );
//
Play sound
if (
source.start ) {
source.start (
0 );
}
else if (
source.play ) {
source.play (
0 );
}
else if (
source.noteOn ) {
source.noteOn (
0 );
}
}
//
Check if document is loaded in iframe
if (
window.frameElement ){
//
Remove event listeners from parent
var thisparent =
window.parent ;
thisparent.document.removeEventListener (
'touchstart' ,
fixAudioContext );
thisparent.document.removeEventListener (
'touchend' ,
fixAudioContext );
}
//
Remove events
document.removeEventListener (
'touchstart' ,
fixAudioContext );
document.removeEventListener (
'touchend' ,
fixAudioContext );
};
//
Check if document is loaded in iframe
if (
window.frameElement ){
//
Add event listeners to parent
var thisparent =
window.parent ;
//
Event listener for iOS 6 -
8 (
was previous touchstart event )
thisparent.document.addEventListener (
'touchstart' ,
fixAudioContext );
//
Event listener for iOS 9 + (
is now touchend event )
thisparent.document.addEventListener (
'touchend' ,
fixAudioContext );
}
//
Event listener for iOS 6 -
8 (
was previous touchstart event )
document.addEventListener (
'touchstart' ,
fixAudioContext );
//
Event listener for iOS 9 + (
is now touchend event )
document.addEventListener (
'touchend' ,
fixAudioContext );
})();
var interval ;
//
Function to play audio by URL
function Play_Audio (
url ){
//
console.log (
'play activated' )
if (
OS_Version ===
'Android' ||
OS_Version ===
'U.Motion' ){
snd.src =
url ;
snd.pause ();
sndcurrentTime =
0 ;
snd.play ();
}
else {
request.open (
"GET" ,
url ,
true );
request.responseType =
"arraybuffer" ;
request.onload =
function (){
ctx.decodeAudioData (
request.response ,
onDecoded );
}
function onDecoded (
buffer ){
var bufferSource =
ctx.createBufferSource ();
bufferSource.buffer =
buffer ;
bufferSource.connect (
ctx.destination );
bufferSource.start ();
}
request.send ();
}
};
//
Make event listener to object
if (
typeof grp !=
'undefined' ) {
grp.taglisten (
"beep" ,
function (
object ,
state ) {
var value =
object.value ;
var enabled =
grp.getvalue (
'32/1/21' );
//
Enable for testing
//
enabled =
true ;
clearInterval (
interval );
if (
state ==
'value' &&
value ==
1 &&
enabled ) {
Play_Audio (
"/scada/resources/img/beep.mp3" );
interval =
setInterval (
function () {
Play_Audio (
"/scada/resources/img/beep.mp3" );
},
2000 );
}
},
true );
}
});
Posts: 300
Threads: 81
Joined: May 2017
Reputation:
0
(27.12.2020, 12:14) Erwin van der Zwart Wrote: This should work:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//
Closing current script section to add elements
</
script >
<
audio id =
"SE_Audio" style =
"display: none;" ></
audio >
<
script type =
"text/javascript" > //
Starting new script section after adding elements
$(
function (){
var ctx =
new (
window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext ),
request =
new XMLHttpRequest ();
var snd =
document.getElementById (
"SE_Audio" );
function getMobileOperatingSystem () {
var userAgent =
navigator.userAgent ||
navigator.vendor ||
window.opera ;
//
console.log (
userAgent )
if (
userAgent.match (/
iPad /
i ) ||
userAgent.match (/
iPhone /
i ) ||
userAgent.match (/
iPod /
i )) {
return 'iOS' ;
}
else if (
userAgent.match (/
Android /
i )) {
return 'Android' ;
}
else if (
userAgent.match (/
iKonWebTouch /
i )) {
return 'U.Motion' ;
}
else {
return 'PC' ;
}
}
var OS_Version =
getMobileOperatingSystem ();
//
Fix iOS Audio Context
(
function () {
var fixAudioContext =
function (
e ) {
if (
ctx ) {
//
Create empty buffer
var buffer =
ctx.createBuffer (
1 ,
1 ,
22050 );
var source =
ctx.createBufferSource ();
source.buffer =
buffer ;
//
Connect to output
source.connect (
ctx.destination );
//
Play sound
if (
source.start ) {
source.start (
0 );
}
else if (
source.play ) {
source.play (
0 );
}
else if (
source.noteOn ) {
source.noteOn (
0 );
}
}
//
Check if document is loaded in iframe
if (
window.frameElement ){
//
Remove event listeners from parent
var thisparent =
window.parent ;
thisparent.document.removeEventListener (
'touchstart' ,
fixAudioContext );
thisparent.document.removeEventListener (
'touchend' ,
fixAudioContext );
}
//
Remove events
document.removeEventListener (
'touchstart' ,
fixAudioContext );
document.removeEventListener (
'touchend' ,
fixAudioContext );
};
//
Check if document is loaded in iframe
if (
window.frameElement ){
//
Add event listeners to parent
var thisparent =
window.parent ;
//
Event listener for iOS 6 -
8 (
was previous touchstart event )
thisparent.document.addEventListener (
'touchstart' ,
fixAudioContext );
//
Event listener for iOS 9 + (
is now touchend event )
thisparent.document.addEventListener (
'touchend' ,
fixAudioContext );
}
//
Event listener for iOS 6 -
8 (
was previous touchstart event )
document.addEventListener (
'touchstart' ,
fixAudioContext );
//
Event listener for iOS 9 + (
is now touchend event )
document.addEventListener (
'touchend' ,
fixAudioContext );
})();
var interval ;
//
Function to play audio by URL
function Play_Audio (
url ){
//
console.log (
'play activated' )
if (
OS_Version ===
'Android' ||
OS_Version ===
'U.Motion' ){
snd.src =
url ;
snd.pause ();
sndcurrentTime =
0 ;
snd.play ();
}
else {
request.open (
"GET" ,
url ,
true );
request.responseType =
"arraybuffer" ;
request.onload =
function (){
ctx.decodeAudioData (
request.response ,
onDecoded );
}
function onDecoded (
buffer ){
var bufferSource =
ctx.createBufferSource ();
bufferSource.buffer =
buffer ;
bufferSource.connect (
ctx.destination );
bufferSource.start ();
}
request.send ();
}
};
//
Make event listener to object
if (
typeof grp !=
'undefined' ) {
grp.taglisten (
"beep" ,
function (
object ,
state ) {
var value =
object.value ;
var enabled =
grp.getvalue (
'32/1/21' );
//
Enable for testing
//
enabled =
true ;
clearInterval (
interval );
if (
state ==
'value' &&
value ==
1 &&
enabled ) {
Play_Audio (
"/scada/resources/img/beep.mp3" );
interval =
setInterval (
function () {
Play_Audio (
"/scada/resources/img/beep.mp3" );
},
2000 );
}
},
true );
}
});
Ok, sorry... I re-tested it.. now it's working fine!
Thank you so much!
Peppe
Posts: 19
Threads: 3
Joined: Jun 2022
Reputation:
0
HI:
I'm trying to use this function but no sound is reproduced.
If I try to execute the file in the explorer edge iI see only text not sound. The same file in my PC I do.
"ID3#TSSELavf58.76.100ÿó`Ĉ±ðIH2ᶈ ‚‚dé…A!pL7!@¡0@11Z=Q;t£º08àAØÒïƒáöâppŸXñþƒù@AØ!ð@p 8>} ‡L‡åÃõƒà̸>ƒàø …*h¤*„D†1l‹¢Ööa!qòždX#åFäk „ÿóbÄ 'Z Î_h;TÀ(:wƒO‚BH Ðd*A¸C£,¦É›‚-`¬†!#ôÏ¢K’ã€Ol@„Ü SDÜú&é‰QØ–$L‡2TÊY7CðRÖ0¥æ šÍWÿÿ/š h¤Èº¹‚Ó¹’*uÿÿöDù¢‚Ïê. 4Oÿÿôâ•Ä€$“»×" "“‚ÿób..... it is like this
What is the reason?
thanks in advance.