Agora 在线 K 歌房支持歌唱评分功能,即把演唱者和歌曲原唱的音调进行对比,并实时将匹配度以动画和分数的方式呈现在界面上。
在实现评分功能前,请确保你已在项目中集成 Agora 音频 SDK v4.0.0 Beta,并实现在线 K 歌功能,详见如下教程:
enableAudioVolumeIndication
方法开启歌唱评分功能。示例代码如下:// Java
getRtcEngine().enableAudioVolumeIndication(30, 10, true);
// Swift
rtc.enableAudioVolumeIndication(20, smooth: 3, reportVad: true)
voicePitch
)。// Java
@Override
public void onAudioVolumeIndication(AudioVolumeInfo[] speakers, int totalVolume) {
for(AudioVolumeInfo info : speakers){
if(info.uid == 0 && info.voicePitch > 0){
mMainThreadDispatch.onLocalPitch(info.voicePitch);
}
}
}
// Swift
func rtcEngine(_: AgoraRtcEngineKit, reportAudioVolumeIndicationOfSpeakers speakers: [AgoraRtcAudioVolumeInfo], totalVolume _: Int) {
voicePitchRelay.accept(speakers.map { $0.voicePitch })
}
将获取到的 voicePitch
传递到歌词组件,示例代码如下:
如何使用歌词组件实现评分效果,请参考歌词组件使用教程 Android 端 或 歌词组件使用教程 iOS 端。
// Java
@Override
public void onLocalPitch(double pitch) {
super.onLocalPitch(pitch);
mBinding.lrcControlView.getPitchView().updateLocalPitch(pitch);
}
// Swift
RoomManager.shared().subscribeVoicePitch().subscribe { result in
guard let value = result.element else { return }
self.lrcScoreView.setVoicePitch(value)
}.disposed(by: disposeBag)