播放本地pcm数据音频


#define QUEUE_BUFFER_SIZE 4            //队列缓冲个数
#define EVERY_READ_LENGTH 1000         //每次从文件读取的长度
#define MIN_SIZE_PER_FRAME 2000        //每侦最小数据长度

@interface OLMyTapesCell ()
{
    AudioStreamBasicDescription audioDescription;             //音频参数
    AudioQueueRef audioQueue;                                 //音频播放队列
    AudioQueueBufferRef audioQueueBuffers[QUEUE_BUFFER_SIZE]; //音频缓存
    NSLock *synlock ;                                         //同步控制
    Byte *pcmDataBuffer;                                      //pcm的读文件数据区
    FILE *file;                                               //pcm源文件
    BOOL isStaring;
    
}


// 初始化播放器
- (void)player{
    NSFileManager *fileManger = [NSFileManager defaultManager];
    NSString *filePath = @“文件路径”;
    
    [[fileManger attributesOfItemAtPath:filePath error:nil] fileSize];
    
    file = fopen([filePath UTF8String],"r");
    if (file) {
        fseek(file, 0, SEEK_SET);
        pcmDataBuffer = malloc(EVERY_READ_LENGTH);
    }else{
        NSLog(@"!!!!!!!!!!!!!!!!");
    }
    synlock = [[NSLock alloc] init];
    
    [self initAudio];
    
    AudioQueueStart(audioQueue, NULL);
    for(int i=0;imAudioDataByteSize = readLength;
    Byte *audiodata = (Byte *)outQB->mAudioData;
    for(int i=0;imAudioDataByteSize用来指示数据区大小,AudioQueueBufferRef->mAudioData用来保存数据区
     */
    AudioQueueEnqueueBuffer(outQ, outQB, 0, NULL);
    [synlock unlock];
}

-(void)checkUsedQueueBuffer:(AudioQueueBufferRef) qbuf
{
    if(qbuf == audioQueueBuffers[0])
    {
        NSLog(@"AudioPlayerAQInputCallback,bufferindex = 0");
    }
    if(qbuf == audioQueueBuffers[1])
    {
        NSLog(@"AudioPlayerAQInputCallback,bufferindex = 1");
    }
    if(qbuf == audioQueueBuffers[2])
    {
        NSLog(@"AudioPlayerAQInputCallback,bufferindex = 2");
    }
    if(qbuf == audioQueueBuffers[3])
    {
        NSLog(@"AudioPlayerAQInputCallback,bufferindex = 3");
    }
}

你可能感兴趣的:(播放本地pcm数据音频)