Reading audio samples via AVAssetReader

后端 未结 3 1292
陌清茗
陌清茗 2020-11-27 13:05

How do you read audio samples via AVAssetReader? I\'ve found examples of duplicating or mixing using AVAssetReader, but those loops are always controlled by the AVAssetWrite

3条回答
  •  囚心锁ツ
    2020-11-27 13:31

    AVAssetReader *reader   = [[AVAssetReader alloc] initWithAsset:asset error:&error];
    AVAssetTrack  *track    = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    NSDictionary  *settings = @{ AVFormatIDKey : [NSNumber numberWithInt:kAudioFormatLinearPCM] };
    
    AVAssetReaderTrackOutput *readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track
                                                                                        outputSettings:settings];
    
    [reader addOutput:readerOutput]; 
    [reader startReading];
    
    CMSampleBufferRef sample = [readerOutput copyNextSampleBuffer];
    
    while ( sample )
    {
       sample = [readerOutput copyNextSampleBuffer];
    
        if ( ! sample )
        {
           continue;
        }
    
        CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sample);
    
        size_t  lengthAtOffset;
        size_t  totalLength;
        char   *data;
    
        if ( CMBlockBufferGetDataPointer( buffer, 0, &lengthAtOffset, &totalLength, &data ) != noErr )
        {
            NSLog(@"error!");
            break;
        }
    
        // do something with data...
    
        CFRelease(sample);
    }
    

提交回复
热议问题