@@ -1012,16 +1012,24 @@ <h2><span class="neverball-text">About the game</span></h2>
1012
1012
this . audioMusicFile = null ;
1013
1013
this . audioMusicSourceNode = null ;
1014
1014
this . audioMusicGainNode = null ;
1015
+
1016
+ this . audioCanPlayOgg = ( new Audio ( ) ) . canPlayType ( 'audio/ogg' ) !== '' ;
1015
1017
} ,
1016
1018
1017
1019
audioPlay : async function ( fileName , fileData , a ) {
1018
- if ( ! this . audioContext ) {
1019
- return ;
1020
+ if ( fileName . endsWith ( '.ogg' ) && ! this . audioCanPlayOgg ) {
1021
+ // MP3 fallback for Safari.
1022
+
1023
+ const response = await fetch ( 'packages/mp3/' + fileName . replace ( '.ogg' , '.mp3' ) ) ;
1024
+
1025
+ if ( response . ok ) {
1026
+ fileData = await response . arrayBuffer ( ) ;
1027
+ }
1020
1028
}
1021
1029
1022
- const audioBuffer = await this . audioContext . decodeAudioData ( fileData ) ;
1030
+ const audioBuffer = await this . audioContext ? .decodeAudioData ( fileData ) ;
1023
1031
1024
- if ( ! this . audioContext ) {
1032
+ if ( ! audioBuffer ) {
1025
1033
return ;
1026
1034
}
1027
1035
@@ -1039,14 +1047,11 @@ <h2><span class="neverball-text">About the game</span></h2>
1039
1047
1040
1048
sourceNode . connect ( gainNode ) ;
1041
1049
gainNode . connect ( this . audioSoundSink ) ;
1050
+
1042
1051
sourceNode . start ( ) ;
1043
1052
} ,
1044
1053
1045
1054
audioMusicFadeTo : async function ( fileName , fileData , fadeTime ) {
1046
- if ( ! this . audioContext ) {
1047
- return ;
1048
- }
1049
-
1050
1055
if ( this . audioMusicFile && fileName === this . audioMusicFile ) {
1051
1056
// If we're already playing this BGM, just force a fade-in.
1052
1057
@@ -1058,8 +1063,16 @@ <h2><span class="neverball-text">About the game</span></h2>
1058
1063
1059
1064
// Set up and fade in the new BGM.
1060
1065
1061
- const blob = new Blob ( [ fileData ] , { type : 'audio/ogg' } ) ;
1062
- const url = URL . createObjectURL ( blob ) ;
1066
+ let url ;
1067
+
1068
+ if ( this . audioCanPlayOgg ) {
1069
+ const blob = new Blob ( [ fileData ] , { type : 'audio/ogg' } ) ;
1070
+
1071
+ url = URL . createObjectURL ( blob ) ;
1072
+ } else {
1073
+ url = 'packages/mp3/' + fileName . replace ( '.ogg' , '.mp3' ) ;
1074
+ }
1075
+
1063
1076
const audioElem = new Audio ( url ) ;
1064
1077
1065
1078
audioElem . loop = true ;
0 commit comments