Skip to content

Commit caa1872

Browse files
committed
Web app: quick MP3 hack for Safari
1 parent ccf2386 commit caa1872

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

.devcontainer/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ENV GL4ES_DIR=/opt/gl4es
66
RUN \
77
export DEBIAN_FRONTEND=noninteractive; \
88
apt-get update -y; \
9-
apt-get install less
9+
apt-get install -y less ffmpeg
1010

1111
COPY ./install-gl4es.sh /tmp/install-gl4es
1212
RUN \
@@ -20,4 +20,4 @@ RUN \
2020
RUN echo emscripten ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/emscripten && \
2121
chmod 0440 /etc/sudoers.d/emscripten
2222

23-
USER emscripten
23+
USER emscripten

emscripten/ball.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ $(DATA_ZIP):
193193

194194
.PHONY: packages
195195
packages: clean-packages
196-
$(MAKE) -f mk/packages.mk OUTPUT_DIR=$$(pwd)/js/packages
196+
$(MAKE) -f mk/packages.mk OUTPUT_DIR=$$(pwd)/js/packages all mp3s
197197

198198
.PHONY: clean-packages
199199
clean-packages:

js/index.html

+23-10
Original file line numberDiff line numberDiff line change
@@ -1012,16 +1012,24 @@ <h2><span class="neverball-text">About the game</span></h2>
10121012
this.audioMusicFile = null;
10131013
this.audioMusicSourceNode = null;
10141014
this.audioMusicGainNode = null;
1015+
1016+
this.audioCanPlayOgg = (new Audio()).canPlayType('audio/ogg') !== '';
10151017
},
10161018

10171019
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+
}
10201028
}
10211029

1022-
const audioBuffer = await this.audioContext.decodeAudioData(fileData);
1030+
const audioBuffer = await this.audioContext?.decodeAudioData(fileData);
10231031

1024-
if (!this.audioContext) {
1032+
if (!audioBuffer) {
10251033
return;
10261034
}
10271035

@@ -1039,14 +1047,11 @@ <h2><span class="neverball-text">About the game</span></h2>
10391047

10401048
sourceNode.connect(gainNode);
10411049
gainNode.connect(this.audioSoundSink);
1050+
10421051
sourceNode.start();
10431052
},
10441053

10451054
audioMusicFadeTo: async function (fileName, fileData, fadeTime) {
1046-
if (!this.audioContext) {
1047-
return;
1048-
}
1049-
10501055
if (this.audioMusicFile && fileName === this.audioMusicFile) {
10511056
// If we're already playing this BGM, just force a fade-in.
10521057

@@ -1058,8 +1063,16 @@ <h2><span class="neverball-text">About the game</span></h2>
10581063

10591064
// Set up and fade in the new BGM.
10601065

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+
10631076
const audioElem = new Audio(url);
10641077

10651078
audioElem.loop = true;

0 commit comments

Comments
 (0)