update callcenter

This commit is contained in:
DDIsFriend
2023-09-05 14:51:24 +08:00
parent 1db91e1e14
commit 177b77c044
26 changed files with 8425 additions and 7824 deletions

View File

@@ -0,0 +1,122 @@
//
// SystemSoundPlayer.swift
// DDAudioPlayerKit_Private
//
// Created by on 2023/8/31.
//
import Foundation
import AVFoundation
public let DDAS = DDAudioService.default
open class DDAudioService {
public static let `default` = DDAudioService()
private var audioSoundID : SystemSoundID = 0
private var vibrateSoundID : SystemSoundID = 0
private var audioNumberOfLoops : Int = 0
private var vibrateNumberOfLoops : Int = 0
private var endSound : Bool = false
private var endVibrate : Bool = false
public func playSoundWithVibrate(audioUrl:URL,numberOfLoops:Int = 1) {
playVibrate(numberOfLoops: -1)
playSound(audioUrl: audioUrl,numberOfLoops: numberOfLoops) {[weak self] in
self?.stopSound()
self?.stopVibrate()
}
}
public func stopSoundWithVibrate() {
stopSound()
stopVibrate()
}
public func playSound(audioUrl:URL,numberOfLoops:Int = 1, endCompletionBlock: (() -> Void)? = nil) {
self.audioNumberOfLoops = numberOfLoops
var audioSoundID:SystemSoundID = 0
AudioServicesCreateSystemSoundID(audioUrl as CFURL, &audioSoundID)
self.audioSoundID = audioSoundID
DispatchQueue.global().async {[weak self] in
self?.playSound(audioSoundID: audioSoundID,leftNumberOfLoops: numberOfLoops,endCompletionBlock: {
if endCompletionBlock != nil {
endCompletionBlock!()
}
})
}
}
public func playVibrate(numberOfLoops:Int = 1, endCompletionBlock: (() -> Void)? = nil) {
self.audioNumberOfLoops = numberOfLoops
var vibrateSoundID : SystemSoundID = 0
vibrateSoundID = kSystemSoundID_Vibrate
self.vibrateSoundID = vibrateSoundID
DispatchQueue.global().async {[weak self] in
self?.playVibrate(vibrateSoundID: vibrateSoundID, leftNumberOfLoops: numberOfLoops,endCompletionBlock: {
if endCompletionBlock != nil {
endCompletionBlock!()
}
})
}
}
public func stopSound() {
endSound = true
AudioServicesDisposeSystemSoundID(audioSoundID)
}
public func stopVibrate() {
endVibrate = true
AudioServicesDisposeSystemSoundID(vibrateSoundID)
}
private func playSound(audioSoundID:SystemSoundID,leftNumberOfLoops:Int,timeInterval:TimeInterval = 0,eachLoopCompletionBlock: (() -> Void)? = nil, endCompletionBlock: (() -> Void)? = nil) {
var changeLeftNumberOfLoops = leftNumberOfLoops
AudioServicesPlaySystemSoundWithCompletion(audioSoundID) {[weak self] in
if eachLoopCompletionBlock != nil {
eachLoopCompletionBlock!()
}
// 0
changeLeftNumberOfLoops-=1
if changeLeftNumberOfLoops == 0 || self?.endSound == true {
self?.stopSound()
if endCompletionBlock != nil {
endCompletionBlock!()
}
print("sound播放结束")
}else{
DispatchQueue.global().asyncAfter(deadline: .now() + timeInterval, execute: {
self?.playSound(audioSoundID: audioSoundID, leftNumberOfLoops: changeLeftNumberOfLoops,timeInterval: timeInterval)
})
}
}
}
private func playVibrate(vibrateSoundID:SystemSoundID,leftNumberOfLoops:Int,timeInterval:TimeInterval = 0,eachLoopCompletionBlock: (() -> Void)? = nil, endCompletionBlock: (() -> Void)? = nil) {
var changeLeftNumberOfLoops = leftNumberOfLoops
AudioServicesPlayAlertSoundWithCompletion(vibrateSoundID) {[weak self] in
if eachLoopCompletionBlock != nil {
eachLoopCompletionBlock!()
}
// 0
changeLeftNumberOfLoops-=1
if changeLeftNumberOfLoops == 0 || self?.endVibrate == true {
self?.stopVibrate()
if endCompletionBlock != nil {
endCompletionBlock!()
}
print("vibrate播放结束")
}else{
DispatchQueue.global().asyncAfter(deadline: .now() + timeInterval, execute: {
self?.playVibrate(vibrateSoundID: vibrateSoundID, leftNumberOfLoops: changeLeftNumberOfLoops,timeInterval: timeInterval)
})
}
}
}
}