Воспроизвести аудио из данных PCM и обрабатывать задержку PCM iOS SwiftIOS

Программируем под IOS
Ответить
Anonymous
 Воспроизвести аудио из данных PCM и обрабатывать задержку PCM iOS Swift

Сообщение Anonymous »

Я интегрировал массив Byte Byte Byte Websocket и Socket, и я использовал приведенный ниже код, чтобы воспроизводить звук из байта, который получает от сокета, и он работает нормально, но когда есть задержка из сокета, звук останавливается и не запускается снова. < /p>
Канал: mono
septore: 16000 < /em> < /p> : Получите размер байта со временем < /p>
Received binary data of size: 640 at time: 10:50:31.022
Received binary data of size: 640 at time: 10:50:31.059
Received binary data of size: 640 at time: 10:50:31.067
Received binary data of size: 640 at time: 10:50:31.086
Received binary data of size: 640 at time: 10:50:31.104
Received binary data of size: 640 at time: 10:50:31.124
Received binary data of size: 640 at time: 10:50:31.152 --- see the difference on this time only
Received binary data of size: 640 at time: 10:50:31.414
Received binary data of size: 640 at time: 10:50:31.415
Received binary data of size: 640 at time: 10:50:31.415
Received binary data of size: 640 at time: 10:50:31.415
Received binary data of size: 640 at time: 10:50:31.416
Received binary data of size: 640 at time: 10:50:31.416
< /code>
import Foundation
import AVFoundation
import Starscream

class AudioUnitPlayer {

var audioQueue: AudioQueueRef?
var audioFormat: AudioStreamBasicDescription
var isPlaying = false

init() {
audioFormat = AudioStreamBasicDescription(
mSampleRate: 16000,
mFormatID: kAudioFormatLinearPCM,
mFormatFlags: kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
mBytesPerPacket: 2, // 2 bytes per sample (16 bits)
mFramesPerPacket: 1, // 1 frame per packet
mBytesPerFrame: 2, // 2 bytes per frame (mono)
mChannelsPerFrame: 1, // Mono
mBitsPerChannel: 16, // 16 bits
mReserved: 0
)
}

func start() {
AudioQueueNewOutput(
&audioFormat,
audioQueueCallback,
Unmanaged.passUnretained(self).toOpaque(),
nil,
nil,
0,
&audioQueue
)

let numBuffers = 3
var buffers = [AudioQueueBufferRef?](repeating: nil, count: numBuffers)

for i in 0.. Int {
let audioData = buffer.pointee.mAudioData
let bytesToCopy = min(data.count, Int(buffer.pointee.mAudioDataBytesCapacity))
data.copyBytes(to: audioData.assumingMemoryBound(to: UInt8.self), count: bytesToCopy)
return bytesToCopy
}

func enqueuePCMData(_ data: Data) {
let chunkSize = 640
var offset = 0

while offset < data.count {
let end = min(offset + chunkSize, data.count)
let chunk = data.subdata(in: offset.. AudioQueueBufferRef? {
var buffer: AudioQueueBufferRef? = nil
let status = AudioQueueAllocateBuffer(audioQueue!, 640, &buffer)
guard status == noErr, let validBuffer = buffer else {
print("Failed to allocate audio buffer")
return nil
}
return validBuffer
}
}

func audioQueueCallback(
inUserData: UnsafeMutableRawPointer?,
inAQ: AudioQueueRef,
inBuffer: AudioQueueBufferRef
) {
print("Audio callback triggered")
let player = Unmanaged.fromOpaque(inUserData!).takeUnretainedValue()
inBuffer.pointee.mAudioDataByteSize = 0
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, nil)
}


Подробнее здесь: https://stackoverflow.com/questions/795 ... -ios-swift
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «IOS»