1.创建
extern OSStatus
AudioConverterNewSpecific( const AudioStreamBasicDescription * inSourceFormat,
const AudioStreamBasicDescription * inDestinationFormat,
UInt32 inNumberClassDescriptions,
const AudioClassDescription * inClassDescriptions,
AudioConverterRef __nullable * __nonnull outAudioConverter)
1.创建
extern OSStatus
AudioConverterNewSpecific( const AudioStreamBasicDescription * inSourceFormat,
const AudioStreamBasicDescription * inDestinationFormat,
UInt32 inNumberClassDescriptions,
const AudioClassDescription * inClassDescriptions,
AudioConverterRef __nullable * __nonnull outAudioConverter)
inSourceFormat 和 inDestinationFormat 是传入的源音频格式和目标音频格式
inNumberClassDescriptions 和 inClassDescriptions是传入的音频编码器描述类和它的个数
outAudioConverter是传出的AudioConverter引用
例子:
AudioStreamBasicDescription inf = {0};
inf.mSampleRate = 44100;
inf.mChannelsPerFrame = 1; //这里只能是1,否则报错
inf.mBitsPerChannel = 16;
inf.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
inf.mFormatID = kAudioFormatLinearPCM;
inf.mFramesPerPacket = 1;
inf.mBytesPerFrame = inf.mBitsPerChannel * inf.mChannelsPerFrame / 8;
inf.mBytesPerPacket = inf.mFramesPerPacket*inf.mBytesPerFrame;
AudioStreamBasicDescription outf = { 0 };
outf.mFormatID = kAudioFormatMPEG4AAC;
outf.mFormatFlags = 0;
outf.mFramesPerPacket = 1024;
outf.mSampleRate = 44100;
outf.mChannelsPerFrame = 2;
<pre name="code" class="html">const OSType subtype = kAudioFormatMPEG4AAC; // kAudioEncoderComponentType的时候使用目标类型
// kAudioDecoderComponentType的时候使用源类型
AudioClassDescription requestedCodecs[2] = {
{
kAudioEncoderComponentType,
subtype,
kAppleSoftwareAudioCodecManufacturer
},
{
kAudioEncoderComponentType,
subtype,
kAppleHardwareAudioCodecManufacturer
}
};
AudioConverterRef audioConverter;
OSStatus result = AudioConverterNewSpecific(inputFormat, outputFormat,2, requestedCodecs, &audioConverter);
2.释放
extern OSStatus
AudioConverterDispose( AudioConverterRef inAudioConverter)
inAudioConverter是AudioConverter的引用
例子:
if(audioConverter != NULL){
<span style="white-space:pre"> </span>AudioConverterDispose(audioConverter);
}
3.编码
extern OSStatus
AudioConverterFillComplexBuffer( AudioConverterRef inAudioConverter,
AudioConverterComplexInputDataProc inInputDataProc,
void * __nullable inInputDataProcUserData,
UInt32 * ioOutputDataPacketSize,
AudioBufferList * outOutputData,
AudioStreamPacketDescription * __nullable outPacketDescription)
inAudioConverter是传入的AudioConverter的引用
inInputDataProc是用户自己实现的获取编码数据的Callback函数(
inInputDataProcUserData是获取编码数据的Callback函数中使用的用户数据(一般传输编码session对象)
ioOutputDataPacketSize输出数据长度(通过获取AudioConverter的kAudioConverterPropertyMaximumOutputPacketSize属性获取或自定义足够的长度)
outOutputData输出数据
outPacketDescription输出数据描述
例子:
static OSStatus FillAudioDataCallback(AudioConverterRef inAudioConverter, UInt32 *ioNumberDataPackets,AudioBufferList *bufferList, AudioStreamPacketDescription **outDataPacketDescription, void *inUserData)
void *data = malloc(length);
UInt32 outputDataPackets = 1;
AudioBufferList outputData;
outputData.mNumberBuffers = 1;
outputData.mBuffers[0].mNumberChannels = 2;// 输出类型声道数
outputData.mBuffers[0].mDataByteSize = (UInt32)length;
outputData.mBuffers[0].mData = data;
AudioStreamPacketDescription outputPacketDescription;
OSStatus status = AudioConverterFillComplexBuffer(audioConverter, FillAudioDataCallback(__bridge void *)self, &outputDataPackets, &outputData, &outputPacketDescription);