切换频道未显示任何内容Agora Swift

时间:2020-09-02 18:53:26

标签: swift agora.io

我正在尝试在侦听器从Firebase接收新的频道值之后切换频道。即使两个用户的频道值相同,远程视频在调用切换频道时也不会显示任何内容。加入频道最初可以正常工作,但是如果我尝试离开频道并加入频道而不是切换频道,那么它也将不起作用。

import UIKit
import AgoraRtcKit
import FirebaseDatabase

class LiveViewController: UIViewController {

@IBOutlet weak var remoteView: UIView!
@IBOutlet weak var localView: UIView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var startButton: UIButton!

var agoraKit: AgoraRtcEngineKit!
var channel:String?
var started = false

func initializeAgoraEngine() {
    agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: AppID, delegate: self)
}

func setupVideo() {
    agoraKit.enableVideo()  // Enables video mode.
    agoraKit.setVideoEncoderConfiguration(
        AgoraVideoEncoderConfiguration(size: AgoraVideoDimension640x360,
                                  frameRate: .fps15,
                                    bitrate: AgoraVideoBitrateStandard,
                            orientationMode: .adaptative)
    ) // Default video profile is 360P
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    localView.layer.cornerRadius = 10.0
    localView.layer.masksToBounds = true
    
    initializeAgoraEngine()
    setupVideo()
    setupLocalVideo()
    setUpChannel()
}

func setupLocalVideo() {
    let videoCanvas = AgoraRtcVideoCanvas()
    videoCanvas.uid = 0
    videoCanvas.view = localView
    videoCanvas.renderMode = .hidden
    agoraKit.setupLocalVideo(videoCanvas)
}

func setUpChannel() {
    agoraKit.setDefaultAudioRouteToSpeakerphone(true)
    agoraKit.joinChannel(byToken: nil, channelId: User.current.uid, info:nil, uid:0) {(sid, uid, elapsed) -> Void in
    }
    UIApplication.shared.isIdleTimerDisabled = true
}

func switchChannel(channel: String) {
    agoraKit.switchChannel(byToken: nil, channelId: channel, joinSuccess: nil)
}

func leaveChannel() {
    agoraKit.leaveChannel(nil)
    UIApplication.shared.isIdleTimerDisabled = false
    remoteView.removeFromSuperview()
    localView.removeFromSuperview()
}

@IBAction func clickStart(_ sender: Any) {
    if !started {
        startButton.setImage(UIImage(named: "next"), for: .normal)
        started = true
        addLiveUserAndListener()
    }
}

@IBAction func didClickSwitchCameraButton(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
    agoraKit.switchCamera()
}

func addLiveUserAndListener() {
    //listener for channel
    Database.database().reference().child("liveUsers").child(User.current.uid).observe(.value, with: {(snapshot) in
         guard let live = Live(snapshot: snapshot) else {
            return
         }
        if !(live.channel.elementsEqual("")) {
            self.switchChannel(channel: live.channel)
        }
    })
}
}

extension LiveViewController: AgoraRtcEngineDelegate {
func rtcEngine(_ engine: AgoraRtcEngineKit, firstRemoteVideoDecodedOfUid uid:UInt, size:CGSize, elapsed:Int) {
    if (remoteView.isHidden) {
        remoteView.isHidden = false
    }
    let videoCanvas = AgoraRtcVideoCanvas()
    videoCanvas.uid = uid
    videoCanvas.view = remoteView
    videoCanvas.renderMode = .hidden
    agoraKit.setupRemoteVideo(videoCanvas)
}

internal func rtcEngine(_ engine: AgoraRtcEngineKit, didOfflineOfUid uid:UInt, reason:AgoraUserOfflineReason) {
    self.remoteView.isHidden = true
}

func rtcEngine(_ engine: AgoraRtcEngineKit, didVideoMuted muted:Bool, byUid:UInt) {
    remoteView.isHidden = muted
}
}

1 个答案:

答案 0 :(得分:1)

如果您查看switchChannel函数的docs,您可能会注意到:

注意:此方法仅适用于实时交互式流媒体频道中的受众角色。

对于您的用例,我建议您自己调用LeaveChannel和joinChannel。

不建议使用firstRemoteVideoDecodedOfUid,而应使用didJoinedOfUidremoteVideoStateChangedOfUid