| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- //
- // LNIMAudioCallBellPlayer.swift
- // Gami
- //
- // Created by OneeChan on 2026/2/9.
- //
- import Foundation
- import AVFAudio
- class LNIMAudioCallBellPlayer: NSObject {
- private var player: AVAudioPlayer?
- private var playing = false
-
- func startPlay(isInCome: Bool) {
- if player != nil {
- stop()
- }
- let path = if isInCome {
- Bundle.main.path(forResource: "phone_bell", ofType: "mp3")
- } else {
- Bundle.main.path(forResource: "phone_bell", ofType: "mp3")
- }
- guard let path else { return }
-
- do {
- let session = AVAudioSession.sharedInstance()
- try session.setCategory(.playback, mode: .default)
- try session.setActive(true)
- player = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
- } catch let error as NSError {
- print("Error: \(error.localizedDescription)")
- return
- }
-
- guard let prepare = player?.prepareToPlay(), prepare else {
- return
- }
-
- playing = true
- player?.delegate = self
- player?.play()
- }
-
- func stop() {
- playing = false
- player?.stop()
- player = nil
- }
- }
- extension LNIMAudioCallBellPlayer: AVAudioPlayerDelegate {
- func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
- if playing {
- player.play()
- }
- }
- }
|