【问题标题】:IOS 5: obtaining sample buffers from a file in a loopIOS 5:循环从文件中获取样本缓冲区
【发布时间】:2012-01-21 07:53:47
【问题描述】:

在阅读了 Apple 提供的文档数小时后,我仍然感到困惑。比如说,我可以找到一种通过 AVCaptureDevice 系列定期访问样本缓冲区的方法。但目前尚不清楚如何对 AVAssetReader 系列做同样的事情。比如说,我想定期获取音频样本缓冲区,而它们的源是 MP3 文件。怎么可能呢?

【问题讨论】:

  • 老实说,我可以想出一些原始的方法。问题是是否存在一些普遍推荐的方法和相关 API。

标签: iphone ios ios5 media avfoundation


【解决方案1】:

要解码 Apple 支持的任何音频文件:

NSString *songName = @"song.mp3";
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];

NSURL *path = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", resourcePath, songName]];

AVURLAsset *song = [AVURLAsset URLAssetWithURL:path options:nil];

AVAssetReader *reader = [AVAssetReader assetReaderWithAsset:song error:nil];

NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
    [NSNumber numberWithFloat:48000.0], AVSampleRateKey,
    [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
    [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
    [NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey,
    [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
    nil];

AVAssetReaderTrackOutput *output = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[song tracks] objectAtIndex:0] outputSettings:outputSettings];

[reader addOutput:output];

bool reading = [reader startReading];

CMSampleBufferRef sampleBuffer = [output copyNextSampleBuffer];

while (sampleBuffer != nil) {
    NSLog(@"Received PCM buffer with [TIMESTAMP:%.1fms]", CMTimeGetSeconds(CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer)) * 1000);
    NSLog(@"Buffer contains [SAMPLES:%ld]", CMSampleBufferGetNumSamples(sampleBuffer));
    NSLog(@"Buffer contains [DURATION:%.1fms] worth of audio", CMTimeGetSeconds(CMSampleBufferGetDuration(sampleBuffer)) * 1000);
    sampleBuffer = [output copyNextSampleBuffer];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多