Sinch视频通话声音来自前扬声器



我已经在iOS Swift项目中实现了SINCH视频通话,我遵循了SINCH实施文档中给出的所有过程https://www.sinch.com/docs/docs/video/ios/#calling。我已经成功实施了,但是我会从前扬声器中发出视频声音。我怎么解决这个问题??在我的代码下方:

var client: SINClient?
var sinCall : SINCall?

配置Sinch

//MARK: Configuring Sinch Delegate
func configuringSinch(){
    //Configuring Client Key
    client = Sinch.client(withApplicationKey: Constants.SINCH_APP_KEY, applicationSecret: Constants.SINCH_PRIVATE_KEY, environmentHost: Constants.SANDBOX_ENVIRONMENT, userId: Utility().getUserId())
    client?.call().delegate = self
    client?.setSupportCalling(true)
    client?.enableManagedPushNotifications()
    client?.start()
    client?.startListeningOnActiveConnection()
    let vcCont = client?.videoController()
    self.vwLocalView.addSubview((vcCont?.localView())!)
    self.sinCall?.delegate = self
}
//MARK: Sinch Video Call Delegate
func clientDidStart(_ client: SINClient!) {
    print("Client Did Start")
}
func clientDidFail(_ client: SINClient!, error: Error!) {
    print("Client failed : (error)")
    player?.stop()
}
func clientDidStop(_ client: SINClient!) {
    print("Client Did Stop")
    player?.stop()
}
    //MARK: Video Call Did Recieve
func client(_ client: SINCallClient!, didReceiveIncomingCall call: SINCall!) {
    print("Did Recieve Incoming Call")
    playRingtoneSound() // Playing Audio
    call.delegate = self;
    self.sinCall = call
}
 //MARK: Call Did Add Video Track
func callDidAddVideoTrack(_ call: SINCall!) {
    let videoCont = client?.videoController()
    vwRemoteView.addSubview((videoCont?.remoteView())!)
}
func callDidEnd(_ call: SINCall!) {
    sinCall?.hangup()
}

这就是您可以管理 sinaudiocontroller 来管理音频输出的方式。

func audioController() -> SINAudioController {
        return (client?.audioController())!
    }
//MARK: Video Call Did Recieve
func client(_ client: SINCallClient!, didReceiveIncomingCall call: SINCall!) {
    audioController().enableSpeaker()
    playRingtoneSound() // Playing Audio
    call.delegate = self;
    self.sinCall = call
}
// In SINCallDelegate
func callDidEstablish(_ call: SINCall!) {
    //to disableSpeaker
    audioController().disableSpeaker()
}

尝试以此操作来管理AudioOutput会话手动

// MARK: AudioOutput Session
// to enable front speaker manually
func setSessionPlayerOn()
{
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().setActive(true)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.none)
    } catch _ {
    }
}
// to enable speaker manually
func setSessionPlayerSpeaker()
{
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().setActive(true)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
    } catch _ {
    }
}
// to turnoff AudioOutput Session manually
func setSessionPlayerOff()
{
    do {
        try AVAudioSession.sharedInstance().setActive(false)
    } catch _ {
    }
}

使用此功能

 func callDidEstablish(_ call: SINCall!) {
    let audio = APPDELEGATE.client?.audioController()
    audio?.disableSpeaker()
   // self.startCallDurationTimerWithSelector()
    appDelegate.client?.audioController().stopPlayingSoundFile()
}

这对我来说是有效的。

class MainVC: UIViewController,SINCallDelegate {
   // var client: SINClient?
    private let videoController = SinchManager.sharedInstance.client!.videoController()
    private let audioController = SinchManager.sharedInstance.client!.audioController()

    private let callClient: SINCallClient
    private var call: SINCall!
    let username: String
    @IBOutlet weak var otherView: UIView!
   // private var mainView: SinchView { return view as! SinchView }
    @IBAction func call_btn(_ sender: UIButton) {
        answer()
    }
    @IBAction func end_btn(_ sender: UIButton) {
        decline()
    }

    override func loadView() {
       // view = SinchView()
        view = otherView
    }
    init(username: String) {
        self.username = username
        self.callClient = SinchManager.sharedInstance.client!.call()
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder aDecoder: NSCoder) {
        print("init(coder:) has not been implemented " + String(describing: aDecoder))
        fatalError("init(coder:) has not been implemented " + String(describing: aDecoder))
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        call.delegate = self
        //self.mainView.videoView.addSubview(self.videoController.localView())
        otherView.addSubview((self.videoController?.localView())!)
        self.videoController?.localView().contentMode = .scaleToFill
        if self.call.direction == SINCallDirection.incoming {
            self.audioController?.startPlayingSoundFile(self.pathForSound(string: "incoming.wav") as String, loop: true)
        }
        if self.call.details.isVideoOffered {
            print("video offered")
            //self.mainView.videoView.addSubview(self.videoController.localView())
            otherView.addSubview((self.videoController?.localView())!)
            self.videoController?.localView().contentMode = .scaleToFill
        }
        otherView.addSubview((self.videoController?.localView())!)
       // mainView.answerButton.addTarget(self, action: #selector(answer), forControlEvents: .TouchUpInside)
       // mainView.declineButton.addTarget(self, action: #selector(decline), forControlEvents: .TouchUpInside)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.audioController?.enableSpeaker()
    }
    func pathForSound(string: String) -> NSString {
        let nsSt = Bundle.main.resourcePath! as NSString
        return nsSt.appendingPathComponent(string) as NSString
    }
    func answer() {
        call.answer()
    }
    func decline() {
        call.hangup()
    }
    func callDidEstablish(call: SINCall!) {
        print("callDidEstablish")
        let audio = SinchManager.sharedInstance.client!.audioController()
        audio?.disableSpeaker()
        // self.startCallDurationTimerWithSelector()
       SinchManager.sharedInstance.client!.audioController().stopPlayingSoundFile()
    }
    func callDidEnd(call: SINCall!) {
        print("callDidEnd")
    }
    func callDidProgress(call: SINCall!) {
        print("callDidProgress")
        self.audioController?.startPlayingSoundFile(self.pathForSound(string: "ringback.wav") as String, loop: true)
    }
    func callDidAddVideoTrack(call: SINCall!) {
        print("callDidAddVideoTrack")
        otherView.addSubview((self.videoController?.localView())!)
    }

最新更新