簡體   English   中英

使用Callkit時如何播放聲音,如響起的嗶嗶聲?

[英]How to play sounds, like the ringing beep, while using Callkit?

我正在嘗試使用webRTC和Callkit實現VoIP。 通話期間音頻工作正常,但是我想在用戶發起呼叫(去電)時向用戶播放聲音。

當用戶發起呼叫並等待接收者應答時,我想播放等待的嗶聲(長嗶聲)。 我可以在不使用Callkit的情況下設法播放聲音,但是當我向Callkit通知去電時,它將以某種方式取消音頻。 我的假設是這樣做,因為在通話時,IOS使音頻靜音。

所以我的問題是,當Callkit處於活動狀態時,如何播放mp3文件。 還是在Callkit或WebRTC中集成了這種等待聲音?

我在音頻會話中使用了不同的類別,但到目前為止還沒有運氣。 參見下面我當前代碼的摘要。

public var audioPlayer: AVAudioPlayer?

private init() {
    do {
        audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: "dialring", ofType: "mp3")!))
        audioPlayer!.prepareToPlay()
        audioPlayer!.numberOfLoops = -1 //loop
    } catch {
        print(error.localizedDescription)
    }
}

func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
    configureAudioSession()
    audioPlayer?.play()
}

func configureAudioSession() {
    print("Configuring audio session")
    let session = AVAudioSession.sharedInstance()
    do {
        try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.mixWithOthers])
        try session.setMode(AVAudioSession.Mode.voiceChat)
    } catch (let error) {
        print("Error while configuring audio session: \(error)")
    }
}

如果有人能指出正確的方向,我將不勝感激。

編輯:我有啟用音頻的背景模式。

圍繞結構稍加改動,現在可以使用了。 我不確定是什么改變了想法。 如果人們面臨同樣的問題。 -確保您對音頻播放器保持強烈的參考。 -確保模式為.playback或.playAndRecord

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM