currently I am playing ringtone using AVPlayer at the time of incoming call. But in background default ringtone is playing from the system. How can I stop background ringtone of the system.
var player: AVPlayer?
func playSound() {
let url = Bundle.main.url(forResource: "Hold Music", withExtension: "mp3")!
do {
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setActive(false, options: [])
try audioSession.setCategory(.playback,mode: .default, options: .mixWithOthers)
try audioSession.setActive(true)
let playerItem:AVPlayerItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem
player?.volume = 1
player.play()
} catch let error {
print("lool \(error.localizedDescription)")
}
}
Call observer for getting call status.
extension LandingViewController: CXCallObserverDelegate{
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
if call.hasEnded == true {
player?.pause()
print("call Disconnected")
}
if call.isOutgoing == true && call.hasConnected == false {
print("call Dialing")
player?.pause()
}
if call.isOutgoing == false && call.hasConnected == false && call.hasEnded == false {
print("call Incoming")
self.playSound()
}
}
if call.hasConnected == true && call.hasEnded == false {
print("call Connected")
player?.pause()
}
}
You need to set
CallKit.CXProviderConfigurationringtone property to your mp3 file and make sure that the sound file includes the app you want to build in it's target membership.The
config.ringtoneSoundproperty will automatically override the default ringtone.It is used to create an instance of the configuration provider that sets the custom ringtone for the incoming call screen.
Using AVPlayer to play sound by detecting incoming call is not the right way.