AudioDataPacketCount返回ValueUnknown [英] AudioDataPacketCount returns ValueUnknown

查看:81
本文介绍了AudioDataPacketCount返回ValueUnknown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS的音频队列服务播放AAC(kAudioFormatMPEG4AAC)文件.一切正常,因此我的代码可以正常工作.

I'm playing an AAC (kAudioFormatMPEG4AAC) file using iOS' Audio Queue Services. It's playing fine, so my code works.

现在,我正在寻找搜索功能.为此,我需要音频数据包的总数.当我的property-listener-proc收到kAudioFileStreamProperty_ReadyToProducePackets时,我会这样做:

Now I'm looking into seek functionality. For this I need the total number of audio packets. When my property-listener-proc receives a kAudioFileStreamProperty_ReadyToProducePackets I do:

UInt64   totalPackets;
UInt32   size = sizeof(totalPackets);
OSStatus status;

status = AudioFileStreamGetProperty(inAudioFileStream,
                                    kAudioFileStreamProperty_AudioDataPacketCount,
                                    &packetCountSize,
                                    &myData->totalPackets);

问题是AudioFileStreamGetProperty()返回kAudioFileStreamError_ValueUnknown(在调试器中打印时为1970170687).

The issue is that AudioFileStreamGetProperty() returns kAudioFileStreamError_ValueUnknown (1970170687 when printed in the debugger).

我做错什么了吗?

推荐答案

原来,我根本没有做错任何事情.

It turned out that I was not doing something wrong at all.

我发现iOS API不提供此功能,因为此文件格式可能包含片段,每个片段都有自己的数据包计数.因此,在读取第一个标头之后,无法知道文件的总包数.

I found that the iOS APIs don't supply this because this file format may contain fragments, each having their own packet count. Hence the total packet count for the file can't be know after having having read the first header.

但是,在许多音频文件中只有一个片段,因此,iOS不会仅在某个点(即在读取文件头之后)提供它知道的数据包计数,这有点令人遗憾.

However, in many audio files there is only one fragment, so it's a bit sad that iOS won't just supply the packet count it knows at at certain point (i.e. after reading the file header).

使用AudioFileStreamSeek()时,我想到了以下方法来将信息压缩到iOS之外:

When working with AudioFileStreamSeek() I thought of the following to squeeze the information out of iOS:

- (SInt64)getTotalPacketCount
{
    OSStatus  status;
    UInt32    ioFlags    = 0;
    long long byteOffset = 0;
    SInt64    lower      = 0;
    SInt64    upper      = 1000000; // Large enough to fit any packet count.
    SInt64    current;              // Current packet count.

    // Binary search to highest packet count that has successful seek.
    while (upper - lower > 1 || status != 0)
    {
        current = (upper + lower) / 2;
        status = AudioFileStreamSeek(audioFileStream, current, &byteOffset, &ioFlags);

        if (status == 0)
        {
            lower = current;
        }
        else
        {
            upper = current;
        }
    }

    return current + 1; // Go from packet number to count.
}

这篇关于AudioDataPacketCount返回ValueUnknown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆