获取实时Webrtc音频轨道级别-Swift

时间:2019-07-16 09:55:11

标签: swift webrtc

我有一个使用Webrtc的流应用程序。 为了检测远程流的实时音频水平(用于可视化UI),我使用的计时器将像波纹管一样重复运行0.1秒:

client!.peerConnection!.stats(for: receive.track!, statsOutputLevel: .standard, completionHandler: { reports in
    for report in reports {
        if report.reportId.contains("_recv") {
            if report.values["mediaType"] != nil && report.values["mediaType"] == "audio" {
                if report.values["audioOutputLevel"] != nil {
                    let audioOutputLevel = Int32(report.values["audioOutputLevel"]!)!
                    var audioLevel: Float = 0.0
                    if audioOutputLevel < 0 {
                        audioLevel = 0.0
                    } else if audioOutputLevel > 32768 {
                        audioLevel = 1.0
                    } else {
                        audioLevel = Float(Float(audioOutputLevel) / 32768.0)
                    }                                                
                    self._delegate?.onReceiveAudioLevel(audioLevel: audioLevel)
                } else {
                    self._delegate?.onReceiveAudioLevel(audioLevel: 0.0)
                }
            }
        }
    }
})

此代码效果很好,但我认为它对应用程序的效果不好,因为计时器为0.1s

我还有一个变量remoteAudioTrackRTCAudioTrack)。当我检查remoteAudioTrack.source.volume的音频电平时,它总是返回0。

您是否想获得远程音轨的实时音频电平?

0 个答案:

没有答案