自定义麦位
指定用户麦位
语聊房 UIKit(Live Audio Room Kit) 支持为语聊房中的角色设置指定麦位,主要接口如下:
takeSeatIndexWhenJoining
:当加入语聊房时(对于主持人和发言人),使用此选项自动设置用户所坐的麦位。hostSeatIndexes
:仅用于设置 主持人 的特殊麦位(不允许发言人和观众就坐)。
自定义麦位布局
语聊房 UIKit 的布局方式,与行和对齐方式相关,如需自定义麦位布局,请参考以下说明:
ZegoLiveAudioRoomLayoutConfig
:
rowConfigs
([ZegoLiveAudioRoomLayoutRowConfig]): 有多少行,以及每行的配置方式。
rowSpacing
(int): 每行的间距,必须 ≥ 0。
ZegoLiveAudioRoomLayoutRowConfig
:count
(int): 每行的麦位数,范围 [1 - 4]。seatSpacing
(int): 每个麦位的水平间距,必须 ≥ 0(仅当对齐方式为FLEX_START
、FLEX_END
和CENTER
时生效)。alignment
(ZegoLiveAudioRoomLayoutAlignment): 为列设置的对齐方式。
start
: 将麦位尽可能靠近起始位置。end
: 将麦位尽可能靠近结束位置。center
: 将麦位尽可能靠中间位置。spaceBetween
: 在麦位之间均匀分布剩余空间。spaceAround
: 在麦位之间均匀分布剩余空间,并在第一个和最后一个麦位之前和之后分布一半的空间。spaceEvenly
: 在麦位之间均匀分布剩余空间,并在第一个和最后一个麦位之前和之后分布相同的空间。
六种 alignment
效果如下:


以如下麦位设置为例,举例说明自定义设置的实现方式:
- 第一行的唯一麦位,设置为主持人的特殊麦位。
- 第二行和第三行的麦位数为 4,对齐方式为
spaceAround
。

class ViewController: UIViewController {
let selfUserID: String = "userID"
let selfUserName: String = "userName"
let yourAppID: UInt32 = YourAppID
let yourAppSign: String = "YourAppSign"
let roomID: String = "YourRoomID"
@IBAction func startLiveAudio(_ sender: Any) {
// Modify your custom configurations here.
let config: ZegoUIKitPrebuiltLiveAudioRoomConfig = ZegoUIKitPrebuiltLiveAudioRoomConfig.host()
let layoutConfig: ZegoLiveAudioRoomLayoutConfig = ZegoLiveAudioRoomLayoutConfig()
layoutConfig.rowSpecing = 5
let rowConfig0: ZegoLiveAudioRoomLayoutRowConfig = ZegoLiveAudioRoomLayoutRowConfig()
rowConfig0.count = 1
rowConfig0.alignment = .center
let rowConfig1: ZegoLiveAudioRoomLayoutRowConfig = ZegoLiveAudioRoomLayoutRowConfig()
rowConfig1.count = 4
rowConfig1.alignment = .spaceAround
let rowConfig2: ZegoLiveAudioRoomLayoutRowConfig = ZegoLiveAudioRoomLayoutRowConfig()
rowConfig2.count = 4
rowConfig2.alignment = .spaceAround
layoutConfig.rowConfigs = [rowConfig0,rowConfig1, rowConfig2]
layoutConfig.rowSpecing = 0
config.layoutConfig = layoutConfig
let liveAudioVC = ZegoUIKitPrebuiltLiveAudioRoomVC.init(yourAppID, appSign: yourAppSign, userID: selfUserID, userName: selfUserName, roomID: roomID, config: config)
liveAudioVC.modalPresentationStyle = .fullScreen
self.present(liveAudioVC, animated: true, completion: nil)
}
}
自定义麦位的 UI
默认情况下,麦位的用户界面会显示围绕用户头像的声浪。
如果您对声浪样式不满意,或者想自定义前景和背景视图,请相应地使用seatConfig
中的配置。
隐藏声浪
默认情况下,声浪会显示出来,如果要隐藏它,请使用showSoundWaveInAudioMode
配置。
showSoundWaveInAudioMode
: 使用此选项来决定是否显示用户头像周围的声浪。默认情况下显示。
以下是参考代码:
class ViewController: UIViewController {
let selfUserID: String = "userID"
let selfUserName: String = "userName"
let yourAppID: UInt32 = YourAppID
let yourAppSign: String = "YourAppSign"
let roomID: String = "YourRoomID"
@IBAction func startLiveAudio(_ sender: Any) {
// Modify your custom configurations here.
let config: ZegoUIKitPrebuiltLiveAudioRoomConfig = ZegoUIKitPrebuiltLiveAudioRoomConfig.host()
config.seatConfig.showSoundWaveInAudioMode = false
let liveAudioVC = ZegoUIKitPrebuiltLiveAudioRoomVC.init(yourAppID, appSign: yourAppSign, userID: selfUserID, userName: selfUserName, roomID: roomID, config: config)
liveAudioVC.modalPresentationStyle = .fullScreen
self.present(liveAudioVC, animated: true, completion: nil)
}
}
自定义麦位的前景或背景视图
要自定义麦位的背景视图,请使用以下属性:
backgroundColor
:使用此属性自定义背景颜色。backgroundImage
:使用此属性自定义背景图片。
要自定义用户麦位的前景视图,您需要声明 delegate 并实现 getSeatForegroundView
方法。
以下是效果和参考代码的示例:

class ViewController: UIViewController {
let selfUserID: String = "userID"
let selfUserName: String = "userName"
let yourAppID: UInt32 = YourAppID
let yourAppSign: String = "YourAppSign"
let roomID: String = "YourRoomID"
@IBAction func startLiveAudio(_ sender: Any) {
// Modify your custom configurations here.
let config: ZegoUIKitPrebuiltLiveAudioRoomConfig = ZegoUIKitPrebuiltLiveAudioRoomConfig.host()
config.seatConfig.backgroudColor = <#Your backgroudColor#>
config.seatConfig.backgroundImage = <#Your image#>
let liveAudioVC = ZegoUIKitPrebuiltLiveAudioRoomVC.init(yourAppID, appSign: yourAppSign, userID: selfUserID, userName: selfUserName, roomID: roomID, config: config)
liveAudioVC.delegate = self
liveAudioVC.modalPresentationStyle = .fullScreen
self.present(liveAudioVC, animated: true, completion: nil)
}
}
extension ViewController : ZegoUIKitPrebuiltLiveAudioRoomVCDelegate {
func getSeatForegroundView(_ userInfo: ZegoUIKitUser?, seatIndex: Int) -> UIView? {
return YourCustomVideoForegroundView
}
}