@@ -6,6 +6,7 @@ function sepiaFW_build_audio(){
66 //Parameters and states
77 TTS . isSpeaking = false ; //state: is assistant speaking?
88 AudioPlayer . isPlaying = false ;
9+ AudioPlayer . isLoading = false ;
910
1011 var player ; //AudioPlayer for music and stuff
1112 var player2 ; //AudioPlayer for sound effects
@@ -35,6 +36,19 @@ function sepiaFW_build_audio(){
3536 AudioPlayer . getTtsPlayer = function ( ) {
3637 return speaker ;
3738 }
39+
40+ //Try to find out if any music player is active (playing or onHold and waiting to resume)
41+ AudioPlayer . isAnyAudioSourceActive = function ( ) {
42+ //Stop internal player
43+ var isInternalPlayerStreaming = AudioPlayer . isLoading || AudioPlayer . isMusicPlayerStreaming ( ) || AudioPlayer . isMainOnHold ( ) || TTS . isPlaying ;
44+ var isYouTubePlayerStreaming = SepiaFW . ui . cards . youTubePlayerGetState ( ) == 1 || SepiaFW . ui . cards . youTubePlayerIsOnHold ( ) ;
45+ var isAndroidPlayerStreaming = SepiaFW . ui . isAndroid && SepiaFW . android . lastReceivedMediaData && SepiaFW . android . lastReceivedMediaData . playing
46+ && ( ( new Date ( ) . getTime ( ) - SepiaFW . android . lastReceivedMediaAppTS ) < ( 1000 * 60 * 15 ) ) ; //This is pure guessing ...
47+ return isInternalPlayerStreaming || isYouTubePlayerStreaming || isAndroidPlayerStreaming ;
48+ }
49+ AudioPlayer . broadcastAudioEvent = function ( source , action , playerObject ) {
50+ console . error ( "audio event: " + source + " - " + action ) ;
51+ }
3852
3953 //AudioContext stuff:
4054 AudioPlayer . useAudioContext = false ; //experimental feature for things like gain control and music visualization (unfortunately fails for quite a few radio streams)
@@ -238,6 +252,9 @@ function sepiaFW_build_audio(){
238252 if ( ! audioPlayer ) audioPlayer = player ;
239253 if ( AudioPlayer . isPlaying ) {
240254 audioPlayer . pause ( ) ; //NOTE: possible race condition here if onPause callback triggers after fadeOutMain (then AudioPlayer.isPlaying will be true)
255+ } else {
256+ AudioPlayer . isLoading = false ;
257+ AudioPlayer . isPlaying = false ;
241258 }
242259 broadcastAudioFinished ( ) ;
243260 if ( ! audioPlayer . dataset . tts ) {
@@ -274,13 +291,15 @@ function sepiaFW_build_audio(){
274291 if ( ! mainAudioStopRequested ) { //(if forced ..) We try to prevent the race-condition with that (2)
275292 mainAudioIsOnHold = true ;
276293 }
294+ SepiaFW . audio . broadcastAudioEvent ( "stream" , "fadeOut" , player ) ;
277295 }
278296 }
279297 AudioPlayer . fadeInMainIfOnHold = function ( ) {
280298 if ( mainAudioIsOnHold ) {
281299 //fade to original volume
282300 AudioPlayer . playerFadeToOriginalVolume ( ) ;
283301 mainAudioIsOnHold = false ;
302+ SepiaFW . audio . broadcastAudioEvent ( "stream" , "fadeIn" , player ) ;
284303 } /*else{
285304 //just restore volume
286305 if (!gotPlayerAudioContext){
@@ -576,6 +595,8 @@ function sepiaFW_build_audio(){
576595 }
577596
578597 audioPlayer . preload = 'auto' ;
598+ AudioPlayer . isLoading = true ;
599+
579600 //console.log("Audio-URL: " + audioURL); //DEBUG
580601 audioPlayer . src = audioURL ;
581602 audioPlayer . oncanplay = function ( ) {
@@ -589,8 +610,18 @@ function sepiaFW_build_audio(){
589610 } else {
590611 TTS . isSpeaking = true ;
591612 }
613+ AudioPlayer . isLoading = false ;
592614 //callback
593615 if ( onStartCallback ) onStartCallback ( ) ;
616+ if ( audioPlayer == player ) {
617+ SepiaFW . audio . broadcastAudioEvent ( "stream" , "start" , audioPlayer ) ;
618+ } else if ( audioPlayer == player2 ) {
619+ SepiaFW . audio . broadcastAudioEvent ( "effects" , "start" , audioPlayer ) ;
620+ } else if ( audioPlayer == speaker ) {
621+ SepiaFW . audio . broadcastAudioEvent ( "tts-player" , "start" , audioPlayer ) ;
622+ } else {
623+ SepiaFW . audio . broadcastAudioEvent ( "unknown" , "start" , audioPlayer ) ;
624+ }
594625 } ;
595626 audioPlayer . onpause = function ( ) {
596627 if ( ! audioOnEndFired ) {
@@ -604,8 +635,18 @@ function sepiaFW_build_audio(){
604635 } else {
605636 TTS . isSpeaking = false ;
606637 }
638+ AudioPlayer . isLoading = false ;
607639 //callback
608640 if ( onEndCallback ) onEndCallback ( ) ;
641+ if ( audioPlayer == player ) {
642+ SepiaFW . audio . broadcastAudioEvent ( "stream" , "stop" , audioPlayer ) ;
643+ } else if ( audioPlayer == player2 ) {
644+ SepiaFW . audio . broadcastAudioEvent ( "effects" , "stop" , audioPlayer ) ;
645+ } else if ( audioPlayer == speaker ) {
646+ SepiaFW . audio . broadcastAudioEvent ( "tts-player" , "stop" , audioPlayer ) ;
647+ } else {
648+ SepiaFW . audio . broadcastAudioEvent ( "unknown" , "stop" , audioPlayer ) ;
649+ }
609650 }
610651 } ;
611652 audioPlayer . onended = function ( ) {
@@ -627,8 +668,18 @@ function sepiaFW_build_audio(){
627668 } else {
628669 TTS . isSpeaking = false ;
629670 }
671+ AudioPlayer . isLoading = false ;
630672 //callback
631673 if ( onErrorCallback ) onErrorCallback ( ) ;
674+ if ( audioPlayer == player ) {
675+ SepiaFW . audio . broadcastAudioEvent ( "stream" , "error" , audioPlayer ) ;
676+ } else if ( audioPlayer == player2 ) {
677+ SepiaFW . audio . broadcastAudioEvent ( "effects" , "error" , audioPlayer ) ;
678+ } else if ( audioPlayer == speaker ) {
679+ SepiaFW . audio . broadcastAudioEvent ( "tts-player" , "error" , audioPlayer ) ;
680+ } else {
681+ SepiaFW . audio . broadcastAudioEvent ( "unknown" , "error" , audioPlayer ) ;
682+ }
632683 } ;
633684 audioPlayer . play ( ) ;
634685 }
@@ -656,18 +707,23 @@ function sepiaFW_build_audio(){
656707
657708 audioPlayer . src = alarmSound ;
658709 audioPlayer . preload = 'auto' ;
710+ AudioPlayer . isLoading = true ;
711+
659712 audioPlayer . oncanplay = function ( ) {
660713 SepiaFW . debug . info ( "AUDIO: can be played now (oncanplay event)" ) ; //debug
661714 AudioPlayer . isPlaying = true ;
715+ AudioPlayer . isLoading = false ;
662716 broadcastAudioStarted ( ) ;
663717 //callback
664718 if ( onStartCallback ) onStartCallback ;
719+ SepiaFW . audio . broadcastAudioEvent ( "effects" , "start" , audioPlayer ) ;
665720 } ;
666721 audioPlayer . onpause = function ( ) {
667722 if ( ! audioOnEndFired ) {
668723 SepiaFW . debug . info ( "AUDIO: ended (onpause event)" ) ; //debug
669724 audioOnEndFired = true ;
670725 AudioPlayer . isPlaying = false ;
726+ AudioPlayer . isLoading = false ;
671727 broadcastAudioFinished ( ) ;
672728 //reset audio URL
673729 /*
@@ -676,6 +732,7 @@ function sepiaFW_build_audio(){
676732 */
677733 //callback
678734 if ( onEndCallback ) onEndCallback ( ) ;
735+ SepiaFW . audio . broadcastAudioEvent ( "effects" , "stop" , audioPlayer ) ;
679736 }
680737 } ;
681738 audioPlayer . onended = function ( ) {
@@ -688,13 +745,15 @@ function sepiaFW_build_audio(){
688745 SepiaFW . debug . info ( "AUDIO: error occured! - code: " + audioPlayer . error . code ) ; //debug
689746 broadcastAudioError ( ) ;
690747 AudioPlayer . isPlaying = false ;
748+ AudioPlayer . isLoading = false ;
691749 //reset audio URL
692750 /*
693751 audioPlayer.preload = 'none';
694752 audioPlayer.src = emptySound;
695753 */
696754 //callback
697755 if ( onErrorCallback ) onErrorCallback ( ) ;
756+ SepiaFW . audio . broadcastAudioEvent ( "effects" , "error" , audioPlayer ) ;
698757 } ;
699758 audioPlayer . play ( ) ;
700759 }
0 commit comments