Files
WerkoutIOS/iphone/Werkout_ios/AudioEngine.swift

76 lines
2.6 KiB
Swift

//
// AudioEngine.swift
// Werkout_ios
//
// Created by Trey Tartt on 6/21/24.
//
import AVKit
import AVFoundation
import SharedCore
class AudioEngine {
static let shared = AudioEngine()
private init() { }
private let runtimeReporter = RuntimeReporter.shared
var audioPlayer: AVAudioPlayer?
var avPlayer: AVPlayer?
func playRemoteAudio(fromURL url: URL) {
#if os(iOS)
let playerItem = AVPlayerItem(url: url)
do {
try AVAudioSession.sharedInstance().setCategory(.playback,
mode: .default,
options: [.mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
avPlayer?.pause()
avPlayer = AVPlayer(playerItem: playerItem)
avPlayer?.play()
} catch {
runtimeReporter.recordError("Failed playing remote audio", metadata: ["error": error.localizedDescription])
}
#endif
}
func playBeep() {
#if os(iOS)
if let path = Bundle.main.path(forResource: "short_beep", ofType: "m4a") {
do {
try AVAudioSession.sharedInstance().setCategory(.playback,
mode: .default,
options: [.mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer?.stop()
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
} catch {
runtimeReporter.recordError("Failed playing short beep", metadata: ["error": error.localizedDescription])
}
}
#endif
}
func playFinished() {
#if os(iOS)
if let path = Bundle.main.path(forResource: "long_beep", ofType: "m4a") {
do {
try AVAudioSession.sharedInstance().setCategory(.playback,
mode: .default,
options: [.mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer?.stop()
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
} catch {
runtimeReporter.recordError("Failed playing long beep", metadata: ["error": error.localizedDescription])
}
}
#endif
}
}