Controller+playSound.swift (3727B)
1 /* 2 * This file is part of GNU Taler, ©2022-25 Taler Systems S.A. 3 * See LICENSE.md 4 */ 5 /** 6 * @author Marc Stibane 7 */ 8 import Foundation 9 import AVFoundation 10 import UIKit 11 12 extension Controller { 13 14 @MainActor 15 func hapticNotification(_ feedbackType: UINotificationFeedbackGenerator.FeedbackType) { 16 if useHaptics { 17 /// call when a notification is displayed, passing the corresponding type 18 UINotificationFeedbackGenerator().notificationOccurred(feedbackType) 19 } 20 } 21 22 @MainActor 23 func hapticFeedback(_ feedbackStyle: UIImpactFeedbackGenerator.FeedbackStyle) { 24 /// call when your UI element impacts something else 25 UIImpactFeedbackGenerator(style: feedbackStyle).impactOccurred() 26 } 27 28 @MainActor 29 func hapticFeedback(intensity: CGFloat) { 30 /// call when your UI element impacts something else with a specific intensity [0.0, 1.0] 31 UIImpactFeedbackGenerator().impactOccurred(intensity: intensity) 32 } 33 34 @MainActor 35 func hapticFeedback() { 36 /// call when the selection changes (not on initial selection) 37 UISelectionFeedbackGenerator().selectionChanged() 38 } 39 40 /// 0 = failure, 1 = received, 2 = sent 41 @MainActor func playSound(_ number: Int) { 42 let sysVolume = AVAudioSession.sharedInstance().outputVolume 43 let volume = sysVolume < 0.16 ? 2.5 // too bad we cannot make it louder 44 : sysVolume < 0.21 ? 1.5 // nope, doesn't work 45 : sysVolume < 0.26 ? 1.0 // this is full volume... 46 : sysVolume < 0.31 ? 0.9 // and 47 // : sysVolume < 0.36 ? 48 : sysVolume < 0.41 ? 0.8 // we 49 // : sysVolume < 0.46 ? 50 : sysVolume < 0.51 ? 0.7 // must 51 : sysVolume < 0.56 ? 0.6 // reduce 52 // : sysVolume < 0.61 ? 53 : sysVolume < 0.66 ? 0.5 // it, 54 : sysVolume < 0.71 ? 0.4 // or 55 : sysVolume < 0.76 ? 0.3 // it'll 56 : sysVolume < 0.81 ? 0.2 // play 57 : sysVolume < 0.86 ? 0.15 // way 58 : sysVolume < 0.91 ? 0.10 // too 59 : 0.07 // loud! 60 // logger.log("❗️sys:\(sysVolume) vol:\(volume)") 61 62 var soundID: SystemSoundID = 0 63 if number > 9 { 64 soundID = UInt32(number) 65 } else { 66 let sound = (number == 0) ? "payment_failure" : 67 (number == 1) ? "payment_success" 68 : "PaymentReceived" 69 let fileURL = URL(fileURLWithPath: "/System/Library/Audio/UISounds/" 70 + sound + ".caf") 71 AudioServicesCreateSystemSoundID(fileURL as CFURL, &soundID) 72 logger.trace("\(sound, privacy: .public) \(soundID)") 73 } 74 if number == 0 || number > 9 || playSoundsI < 0 { 75 AudioServicesPlaySystemSound(soundID); 76 } else if playSoundsI > 0 && playSoundsB { 77 if let url = Bundle.main.url(forResource: (number == 1) ? "payment_sent" 78 : "payment_received", 79 withExtension: "m4a") { 80 player.removeAllItems() 81 player.insert(AVPlayerItem(url: url), after: nil) 82 logger.log("\(url, privacy: .public)") 83 player.play() 84 player.volume = Float(volume) 85 } else { 86 AudioServicesPlaySystemSound(soundID); 87 } 88 } 89 hapticNotification(number == 0 ? .error : .success) 90 } 91 }