如何设置AudioStreamBasicDescription属性?

时间:2016-10-25 12:16:45

标签: ios pcm audioqueue

我正在尝试使用AudioQueue从服务器播放PCM流数据。

PCM数据格式:

Sample rate = 48000, num of channel = 2, Bit per sample = 16

并且,服务器不会将固定字节流式传输到客户端。 (可变字节。) (例如:30848,128,2764,......字节)

如何设置ASBD? 我不知道如何设置mFramesPerPacketmBytesPerFramemBytesPerPacket。 我已阅读Apple参考文档,但没有详细说明。

请给我任何想法。

新添加:这里,我已经设置了ASBD结构。 (语言:斯威夫特)

// Create ASBD structure & set properties.
var streamFormat = AudioStreamBasicDescription()

streamFormat.mSampleRate = 48000
streamFormat.mFormatID = kAudioFormatLinearPCM
streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked
streamFormat.mFramesPerPacket = 1
streamFormat.mChannelsPerFrame = 2
streamFormat.mBitsPerChannel = 16

streamFormat.mBytesPerFrame = (streamFormat.mBitsPerChannel / 8) * streamFormat.mChannelsPerFrame
streamFormat.mBytesPerPacket = streamFormat.mBytesPerFrame
streamFormat.mReserved = 0

// Create AudioQueue for playing PCM streaming data.
var err = AudioQueueNewOutput(&streamFormat, self.queueCallbackProc, nil, nil, nil, 0, &aq)

...

我已经像上面那样设置了ASBD结构。

AudioQueue播放PCM数据非常好几秒钟,

但很快就会停止播放。我该怎么办?

(仍在流式传输,排队AudioQueue)

请给我任何想法。

1 个答案:

答案 0 :(得分:1)

ASBD只是下面定义的结构如下:

struct AudioStreamBasicDescription
{
    Float64             mSampleRate;
    AudioFormatID       mFormatID;
    AudioFormatFlags    mFormatFlags;
    UInt32              mBytesPerPacket;
    UInt32              mFramesPerPacket;
    UInt32              mBytesPerFrame;
    UInt32              mChannelsPerFrame;
    UInt32              mBitsPerChannel;
    UInt32              mReserved;
};
typedef struct AudioStreamBasicDescription  AudioStreamBasicDescription;

您可以像这样设置结构的变量:

AudioStreamBasicDescription streamFormat;

streamFormat.mFormatID = kAudioFormatLinearPCM;
streamFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;

streamFormat.mSampleRate = sampleRate;
streamFormat.mBitsPerChannel = bitsPerChannel;
streamFormat.mChannelsPerFrame = channelsPerFrame;
streamFormat.mFramesPerPacket = 1;

int bytes = (bitsPerChannel / 8) * channelsPerFrame;
streamFormat.mBytesPerFrame = bytes;
streamFormat.mBytesPerPacket = bytes;