update callcenter
This commit is contained in:
122
Pods/DDAudioPlayerKit_Private/DDAudioPlayerKit_Private/Classes/DDAudioService.swift
generated
Normal file
122
Pods/DDAudioPlayerKit_Private/DDAudioPlayerKit_Private/Classes/DDAudioService.swift
generated
Normal 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)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user