Skip to content

Commit 397e91f

Browse files
author
zhaoyongqiang
committed
fix pcm播放卡顿的问题
1 parent 4245b84 commit 397e91f

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

iOS/APIExample/Common/ExternalAudio/AgoraPcmSourcePush.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ class AgoraPcmSourcePush: NSObject {
6060
let sleepTime = TimeInterval(self.samples) / TimeInterval(self.sampleRate)
6161
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
6262
while input.hasBytesAvailable && self.state == .Play {
63+
let beforePush = CFAbsoluteTimeGetCurrent()
6364
input.read(buffer, maxLength: bufferSize)
6465
// self.delegate?.onAudioFrame(data: Data(bytes: buffer, count: bufferSize))
6566
self.delegate?.onAudioFrame(data: buffer)
66-
Thread.sleep(forTimeInterval: sleepTime)
67+
let afterPush = CFAbsoluteTimeGetCurrent()
68+
let sleep = sleepTime - (afterPush - beforePush)
69+
Thread.sleep(forTimeInterval: sleep)
6770
}
6871
buffer.deallocate()
6972
input.close()

iOS/APIExample/Examples/Advanced/CustomPcmAudioSource/CustomPcmAudioSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CustomPcmAudioSourceMain: BaseViewController {
5252
}
5353
}
5454

55-
let sampleRate:UInt = 44100, channel:UInt = 2, bitPerSample = 16, samples = 441 * 10
55+
let sampleRate:UInt = 44100, channel:UInt = 1, bitPerSample = 16, samples = 441 * 10
5656
override func viewDidLoad(){
5757
super.viewDidLoad()
5858

0 commit comments

Comments
 (0)