From ac55a3eee40f2c77b95f4a2fe2a56c5b9b059a87 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Fri, 14 Jul 2023 01:24:23 -0600 Subject: [PATCH] Change entire audio code to maybe make iOS work right --- scripts/fix_ios_audio.sh | 5 ---- src/ios/AppDelegate.m | 44 --------------------------- www/assets/js/audio.js | 64 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 53 deletions(-) delete mode 100755 scripts/fix_ios_audio.sh delete mode 100644 src/ios/AppDelegate.m diff --git a/scripts/fix_ios_audio.sh b/scripts/fix_ios_audio.sh deleted file mode 100755 index 0208a9b..0000000 --- a/scripts/fix_ios_audio.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -echo "Replacing AppDelegate.m to fix AVAudioSession.Category..." - -cp src/ios/AppDelegate.m platforms/ios/PackageHelper/Classes/AppDelegate.m \ No newline at end of file diff --git a/src/ios/AppDelegate.m b/src/ios/AppDelegate.m deleted file mode 100644 index 8d40971..0000000 --- a/src/ios/AppDelegate.m +++ /dev/null @@ -1,44 +0,0 @@ -/* - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. - */ - -// -// AppDelegate.m -// PackageHelper -// -// Created by ___FULLUSERNAME___ on ___DATE___. -// Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved. -// - -#import "AppDelegate.h" -#import "MainViewController.h" -#import - -@implementation AppDelegate - -- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions -{ - self.viewController = [[MainViewController alloc] init]; - //set audio category to stop app interrupting music - NSError *setCategoryError = nil; - BOOL success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &setCategoryError]; - - return [super application:application didFinishLaunchingWithOptions:launchOptions]; -} - -@end diff --git a/www/assets/js/audio.js b/www/assets/js/audio.js index b60b7cf..5542cbc 100644 --- a/www/assets/js/audio.js +++ b/www/assets/js/audio.js @@ -5,8 +5,18 @@ */ var sfx = {}; +var sfxBuffers = {}; +const AudioContext = window.AudioContext || window.webkitAudioContext; +var audioContext; +var sfxVolume = { + "alert": 100.0, + "ok": 100.0, + "error": 100.0, + "scan": 100.0 +}; function initSFX() { + audioContext = new AudioContext(); if (getStorage("alertsound") == null) { setStorage("alertsound", "sonar"); } @@ -29,6 +39,46 @@ function initSFX() { "error": new Audio("assets/audio/error.mp3"), "scan": new Audio("assets/audio/scan.mp3") }; + if (noalertsound == false) { + window.fetch("assets/audio/" + alertNoiseFile) + .then(response => response.arrayBuffer()) + .then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer, + audioBuffer => { + sfxBuffers.alert = audioBuffer; + }, + error => + console.error(error) + )); + } else { + sfxBuffers.alert = false; + } + window.fetch("assets/audio/ok.mp3") + .then(response => response.arrayBuffer()) + .then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer, + audioBuffer => { + sfxBuffers.ok = audioBuffer; + }, + error => + console.error(error) + )); + window.fetch("assets/audio/error.mp3") + .then(response => response.arrayBuffer()) + .then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer, + audioBuffer => { + sfxBuffers.error = audioBuffer; + }, + error => + console.error(error) + )); + window.fetch("assets/audio/scan.mp3") + .then(response => response.arrayBuffer()) + .then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer, + audioBuffer => { + sfxBuffers.scan = audioBuffer; + }, + error => + console.error(error) + )); setVolume("alert", alertVolume); } @@ -39,13 +89,18 @@ function initSFX() { * @returns {undefined} */ function playSound(sound) { - if (sfx[sound] == false) { + if (sfxBuffers[sound] == false) { return; } - if (sfx[sound].volume == 0) { + if (sfxVolume[sound] == 0) { return; } - sfx[sound].play(); + const source = audioContext.createBufferSource(); + source.buffer = sfxBuffers[sound]; + const gainNode = audioContext.createGain(); + gainNode.gain.value = sfxVolume[sound] / 100.0; + source.connect(gainNode).connect(audioContext.destination); + source.start(); } /** @@ -57,7 +112,8 @@ function setVolume(sound, volume) { if (sfx[sound] == false) { return; } - sfx[sound].volume = volume / 100.0; + //sfx[sound].volume = volume / 100.0; + sfxVolume[sound] = volume; } initSFX(); \ No newline at end of file