Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6562907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:45:56+00:00 2026-05-25T13:45:56+00:00

I am using AudioQueue to stream some song, my question is how can i

  • 0

I am using AudioQueue to stream some song, my question is how can i tell the length of playback of already queued buffers? I want to stream two seconds of data at a time, the problem i am having is how do i know how many bytes actually correspond to two seconds of music (so i can always be ahead by two seconds).

Thanks

Daniel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:45:57+00:00Added an answer on May 25, 2026 at 1:45 pm

    Here is a class that uses Audio File Services to get at bitrate / packet / frame data to grab the amount of bytes from a music file that correspond to x seconds, the example has been tested with mp3 and m4a files

    Header

    #import <Foundation/Foundation.h>
    #import <AudioToolbox/AudioToolbox.h>
    @interface MusicChunker : NSObject
    {
        AudioFileID audioFile;
        int _sampleRate;
        int _totalFrames;
        UInt64 _framesPerPacket;
        UInt64 _totalPackets;
        UInt64 fileDataSize;
        AudioFilePacketTableInfo _packetInfo;
        int _fileLength;
        AudioStreamBasicDescription _fileDataFormat;
        NSFileHandle * _fileHandle;
        int _packetOffset;
        int _totalReadBytes;
        int _maxPacketSize;
        BOOL firstTime;
        BOOL _ism4a;
    }
    -(id)initWithURL:(NSURL*)url andFileType:(NSString*)ext;
    //gets next chunk that corresponds to seconds of audio
    -(NSData*)getNextDataChunk:(int)seconds;
    @end
    

    Implementation

    #import "MusicChunker.h"
    void ReportAudioError(OSStatus statusCode);
    @implementation MusicChunker
    
    - (id)init
    {
        self = [super init];
        if (self) {
            // Initialization code here.
        }
    
        return self;
    }
    void ReportAudioError(OSStatus statusCode) {
        switch (statusCode) {
            case noErr:
                break;
            case kAudioFileUnspecifiedError:
                [NSException raise:@"AudioFileUnspecifiedError" format:@"An unspecified error occured."];
                break;
            case kAudioFileUnsupportedDataFormatError:
                [NSException raise:@"AudioFileUnsupportedDataFormatError" format:@"The data format is not supported by the output file type."];
                break;
            case kAudioFileUnsupportedFileTypeError:
                [NSException raise:@"AudioFileUnsupportedFileTypeError" format:@"The file type is not supported."];
                break;
            case kAudioFileUnsupportedPropertyError:
                [NSException raise:@"AudioFileUnsupportedPropertyError" format:@"A file property is not supported."];
                break;
            case kAudioFilePermissionsError:
                [NSException raise:@"AudioFilePermissionsError" format:@"The operation violated the file permissions. For example, an attempt was made to write to a file opened with the kAudioFileReadPermission constant."];
                break;
            case kAudioFileNotOptimizedError:
                [NSException raise:@"AudioFileNotOptimizedError" format:@"The chunks following the audio data chunk are preventing the extension of the audio data chunk. To write more data, you must optimize the file."];
                break;
            case kAudioFileInvalidChunkError:
                [NSException raise:@"AudioFileInvalidChunkError" format:@"Either the chunk does not exist in the file or it is not supported by the file."];
                break;
            case kAudioFileDoesNotAllow64BitDataSizeError:
                [NSException raise:@"AudioFileDoesNotAllow64BitDataSizeError" format:@"The file offset was too large for the file type. The AIFF and WAVE file format types have 32-bit file size limits."];
                break;
            case kAudioFileInvalidPacketOffsetError:
                [NSException raise:@"AudioFileInvalidPacketOffsetError" format:@"A packet offset was past the end of the file, or not at the end of the file when a VBR format was written, or a corrupt packet size was read when the packet table was built."];
                break;
            case kAudioFileInvalidFileError:
                [NSException raise:@"AudioFileInvalidFileError" format:@"The file is malformed, or otherwise not a valid instance of an audio file of its type."];
                break;
            case kAudioFileOperationNotSupportedError:
                [NSException raise:@"AudioFileOperationNotSupportedError" format:@"The operation cannot be performed. For example, setting the kAudioFilePropertyAudioDataByteCount constant to increase the size of the audio data in a file is not a supported operation. Write the data instead."];
                break;
            case -50:
                [NSException raise:@"AudioFileBadParameter" format:@"An invalid parameter was passed, possibly the current packet and/or the inNumberOfPackets."];
                break;
            default:
                [NSException raise:@"AudioFileUknownError" format:@"An unknown error type %@ occured. [%s]", [NSNumber numberWithInteger:statusCode], (char*)&statusCode];
                break;
        }
    }
    
    + (AudioFileTypeID)hintForFileExtension:(NSString *)fileExtension
    {
        AudioFileTypeID fileTypeHint = kAudioFileAAC_ADTSType;
        if ([fileExtension isEqual:@"mp3"])
        {
            fileTypeHint = kAudioFileMP3Type;
        }
        else if ([fileExtension isEqual:@"wav"])
        {
            fileTypeHint = kAudioFileWAVEType;
        }
        else if ([fileExtension isEqual:@"aifc"])
        {
            fileTypeHint = kAudioFileAIFCType;
        }
        else if ([fileExtension isEqual:@"aiff"])
        {
            fileTypeHint = kAudioFileAIFFType;
        }
        else if ([fileExtension isEqual:@"m4a"])
        {
            fileTypeHint = kAudioFileM4AType;
        }
        else if ([fileExtension isEqual:@"mp4"])
        {
            fileTypeHint = kAudioFileMPEG4Type;
        }
        else if ([fileExtension isEqual:@"caf"])
        {
            fileTypeHint = kAudioFileCAFType;
        }
        else if ([fileExtension isEqual:@"aac"])
        {
            fileTypeHint = kAudioFileAAC_ADTSType;
        }
        return fileTypeHint;
    }
    
    -(id)initWithURL:(NSURL*)url andFileType:(NSString*)ext
    {
        self = [super init];
        if (self) {
            // Initialization code here.
            //OSStatus theErr = noErr;
            if([ext isEqualToString:@"mp3"])
            {
                _ism4a=FALSE;
            }
            else
                _ism4a=TRUE;
            firstTime=TRUE;
            _packetOffset=0;
            AudioFileTypeID hint=[MusicChunker hintForFileExtension:ext];
            OSStatus theErr = AudioFileOpenURL((CFURLRef)url, kAudioFileReadPermission, hint, &audioFile);
            if(theErr)
            {
                ReportAudioError(theErr);
    
            }
    
            UInt32 thePropertySize;// = sizeof(theFileFormat);
    
            thePropertySize = sizeof(fileDataSize);
            theErr = AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataByteCount, &thePropertySize, &fileDataSize);
            if(theErr)
            {
                ReportAudioError(theErr);
    
            }
    
            theErr = AudioFileGetProperty(audioFile,    kAudioFilePropertyAudioDataPacketCount, &thePropertySize, &_totalPackets);
            if(theErr)
            {
                ReportAudioError(theErr);
    
            }
            /*
            UInt32 size;
    
            size= sizeof(_packetInfo);
            theErr= AudioFileGetProperty(audioFile, kAudioFilePropertyPacketTableInfo, &size, &_packetInfo);
      g(@"Key %@", key );
             }
    
            if(theErr)
            {
                ReportAudioError(theErr);
            }
            */
            UInt32 size;
            size=sizeof(_maxPacketSize);
            theErr=AudioFileGetProperty(audioFile,  kAudioFilePropertyMaximumPacketSize , &size, &_maxPacketSize);
    
            size = sizeof( _fileDataFormat );
            theErr=AudioFileGetProperty( audioFile, kAudioFilePropertyDataFormat, &size, &_fileDataFormat );
            _framesPerPacket=_fileDataFormat.mFramesPerPacket;
            _totalFrames=_fileDataFormat.mFramesPerPacket*_totalPackets;
    
            _fileHandle=[[NSFileHandle fileHandleForReadingFromURL:url error:nil] retain];    
            _fileLength=[_fileHandle seekToEndOfFile];
            _sampleRate=_fileDataFormat.mSampleRate;
            _totalReadBytes=0;
            /*
             AudioFramePacketTranslation tran;//= .mFrame = 0, .mPacket = packetCount - 1, .mFrameOffsetInPacket = 0 };
             tran.mFrame=0;
             tran.mFrameOffsetInPacket=0;
             tran.mPacket=1;
             UInt32 size=sizeof(tran);
             theErr=AudioFileGetProperty(audioFile, kAudioFilePropertyPacketToFrame, &size, &tran);
             */
            /*
             AudioBytePacketTranslation bt;
             bt.mPacket=4;
             bt.mByteOffsetInPacket=0;
             size=sizeof(bt);
             theErr=AudioFileGetProperty(audioFile, kAudioFilePropertyPacketToByte, &size, &bt);
             */
    
    
        }
    
        return self;
    }
    //gets next chunk that corresponds to seconds of audio
    -(NSData*)getNextDataChunk:(int)seconds
    {
    
        //NSLog(@"%d, total packets",_totalPackets);
    
        if(_packetOffset>=_totalPackets)
            return nil;
    
        //sampleRate * seconds = number of wanted frames
        int framesWanted= _sampleRate*seconds;
        NSData *header=nil;
        int wantedPackets=  framesWanted/_framesPerPacket;
        if(firstTime && _ism4a)
        {
            firstTime=false;
            //when we have a header that was stripped off, we grab it from the original file
            int totallen= [_fileHandle seekToEndOfFile];
            int dif=totallen-fileDataSize;
            [_fileHandle seekToFileOffset:0];
            header= [_fileHandle readDataOfLength:dif];
         }
    
    
    
        int packetOffset=_packetOffset+wantedPackets;
    
        //bound condition
        if(packetOffset>_totalPackets)
        {
            packetOffset=_totalPackets;
        }
    
    
        UInt32 outBytes;
    
        UInt32 packetCount = wantedPackets;
        int x=packetCount * _maxPacketSize;
        void *data = (void *)malloc(x);
    
        OSStatus theErr=AudioFileReadPackets(audioFile, false, &outBytes, NULL, _packetOffset, &packetCount, data);
    
        if(theErr)
        {
            ReportAudioError(theErr);
        }
        //calculate bytes to read
    
        int bytesRead=outBytes;
    
        //update read bytes
        _totalReadBytes+=bytesRead;
       // NSLog(@"total bytes read %d", _totalReadBytes);
        _packetOffset=packetOffset;
    
    
    
        NSData *subdata=[[NSData dataWithBytes:data length:outBytes] retain];    
    
        free(data);
    
        if(header)
        {
            NSMutableData *data=[[NSMutableData alloc]init];
            [data appendData:header];
            [data appendData:subdata];
            [subdata release];
            return [data autorelease];
        }
    
        return [subdata autorelease];
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using AudioQueue for recording voice. I want to know how can we
I have implemented audio playback using AVPlayer, playing a remote mp3 url. I want
I am using AudioQueue to stream audio from an arbitrary source (the class basically
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Does anybody know of any examples using AudioQueue that play from an in-memory source?
I am currently using Audio Queues on the iPhone to record and playback audio.
Can somebody tell me how to scrub the AQPlayer ( used in Apple's SpeakHere
I am Using AUdioQueue to record the audio from built-in mic and send it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.