首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAssetReader查找

AVAssetReader查找
EN

Stack Overflow用户
提问于 2011-05-22 00:56:24
回答 2查看 8.8K关注 0票数 6

我需要寻找周围的音频文件和拉出块。我正在尝试使用AVAssetReader。我看到的错误是,如果我在一段时间内从不同的偏移量读取音频,我得到的平均值(块)是不同的。

例如,如果我读的是0.1s到0.5s的音频,我得到的是不同的,如果我读的是0.2s到0.5s,我收到的区块是不同的

以下是演示它的代码示例

代码语言:javascript
复制
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

+ (void) test
{
    NSURL* path = [[NSBundle mainBundle] URLForResource:@"music" withExtension:@"mp3"];

    [self test:path sample:1 showChunks:5];
    [self test:path sample:2 showChunks:4];
    [self test:path sample:3 showChunks:3];
}

+(void) test:(NSURL*) url sample:(NSInteger) sample showChunks:(NSInteger) chunkCount
{
#define CHUNK 800
#define SAMPLE_RATE 8000
    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:url options:nil];
    NSError *assetError = nil;
    AVAssetReader* assetReader = [AVAssetReader assetReaderWithAsset:asset error:&assetError];

    CMTime startTime = CMTimeMake(sample*CHUNK, SAMPLE_RATE);
    CMTimeShow(startTime);

    CMTimeRange timeRange = CMTimeRangeMake(startTime, kCMTimePositiveInfinity);
    assetReader.timeRange = timeRange;

    NSDictionary* dict = nil;
    dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:SAMPLE_RATE], AVSampleRateKey, [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, nil];


    AVAssetReaderAudioMixOutput* assetReaderOutput = [AVAssetReaderAudioMixOutput assetReaderAudioMixOutputWithAudioTracks:asset.tracks audioSettings: dict];
    if (! [assetReader canAddOutput: assetReaderOutput]) {
        NSLog (@"error: Cannot add output reader");
        assetReader = nil;
        return;
    }

    [assetReader addOutput: assetReaderOutput];

    [assetReader startReading];

    CMSampleBufferRef nextBuffer;

    if (!(nextBuffer = [assetReaderOutput copyNextSampleBuffer]))
    {
        return;
    }
    CMSampleBufferGetTotalSampleSize (nextBuffer);
    // Extract bytes from buffer
    CMBlockBufferRef dataBuffer = CMSampleBufferGetDataBuffer(nextBuffer);

    NSInteger len = CMBlockBufferGetDataLength(dataBuffer);
    if (len < chunkCount*CHUNK)
    {
        printf("CHUNK is to big");
        return;
    }
    UInt8* buf = malloc(len);
    CMBlockBufferCopyDataBytes(dataBuffer, 0, len, buf);

    for (int ii = 0; ii < chunkCount*CHUNK; ii+=CHUNK)
    {
        CGFloat av = 0;
        for (int jj = 0; jj < CHUNK; jj++)
        {
            av += (CGFloat) buf[jj+ii];
        }
        printf("Time: %f av: %f\n", (CGFloat)(ii+CHUNK*sample)/(CGFloat)SAMPLE_RATE,  av/(CGFloat)CHUNK);
    }
    printf("\n");

    free(buf);


}

这是输出

代码语言:javascript
复制
{800/8000 = 0.100}
Time: 0.100000 av: 149.013748
Time: 0.200000 av: 100.323753
Time: 0.300000 av: 146.991257
Time: 0.400000 av: 106.763748
Time: 0.500000 av: 145.020004

{1600/8000 = 0.200}
Time: 0.200000 av: 145.011246
Time: 0.300000 av: 110.718750
Time: 0.400000 av: 154.543747
Time: 0.500000 av: 112.025002

{2400/8000 = 0.300}
Time: 0.300000 av: 149.278748
Time: 0.400000 av: 104.477501
Time: 0.500000 av: 158.162506

请帮帮忙

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-17 03:57:22

在我看来,你的问题是假设下面的代码准确地寻找startTime:

代码语言:javascript
复制
CMTimeRange timeRange = CMTimeRangeMake(startTime, kCMTimePositiveInfinity);
assetReader.timeRange = timeRange;

您可以使用调用

CMSampleBufferGetOutputPresentationTimeStamp(nextBuffer);

由此,您将能够看到缓冲区开始的确切时间(以秒为单位)。

票数 8
EN

Stack Overflow用户

发布于 2012-11-08 17:26:01

在我自己的经验中

代码语言:javascript
复制
assetReader.timeRange = CMTimeRangeMake(CMTimeMake(sample, sample_rate), kCMTimePositiveInfinity)

效果很好。在搜索过程中不存在精度问题。

您可能会遇到淡入问题:实际上,在前1024个样本中,AVAssetReader似乎淡入了(可能更多一点)。我修复了它,在我真正想读的位置之前读了1024个样本,然后跳过那1024个样本。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6083162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档