Audio Queue Service Example using How to record sound in mp3 format in iPhone

4.4k Views Asked by At

I am new in ios developement.I have downloaded sample audio recorder project from https://github.com/vecter/Audio-Queue-Services-Example using Audio Queue Service with the help of AudioToolBox framework .In that sample project recorded audio format is .aif. while run the downloaded sample project working fine.In the sample example project using how to record audio like .mp3 format .how can i do this?

simply i have changed kAudioFileAIFFType into kAudioFileMP3Type i am getting message from nslog

   //---------nslog message--------------
      Not recording, returning
      Writing buffer 0 
   //------------------

- (void)startRecording
{
[self setupAudioFormat:&recordState.dataFormat];

recordState.currentPacket = 0;

OSStatus status;
status = AudioQueueNewInput(&recordState.dataFormat,
                            AudioInputCallback,
                            &recordState,
                            CFRunLoopGetCurrent(),
                            kCFRunLoopCommonModes,
                            0,
                            &recordState.queue);

if (status == 0)
{
    // Prime recording buffers with empty data
    for (int i = 0; i < NUM_BUFFERS; i++)
    {
        AudioQueueAllocateBuffer(recordState.queue, 16000, &recordState.buffers[i]);
        AudioQueueEnqueueBuffer (recordState.queue, recordState.buffers[i], 0, NULL);
    }

    status = AudioFileCreateWithURL(fileURL,
                                    kAudioFileMP3Type,
                                    &recordState.dataFormat,
                                    kAudioFileFlags_EraseFile,
                                    &recordState.audioFile);
    if (status == 0)
    {
        recordState.recording = true;        
        status = AudioQueueStart(recordState.queue, NULL);
        if (status == 0)
        {
            labelStatus.text = @"Recording";
        }
    }
}

if (status != 0)
{
    [self stopRecording];
    labelStatus.text = @"Record Failed";
}
}

//---------------------------------------------------------------------------

- (void)startPlayback
{
playState.currentPacket = 0;

[self setupAudioFormat:&playState.dataFormat];

OSStatus status;
status = AudioFileOpenURL(fileURL, kAudioFileReadPermission, kAudioFileMP3Type, &playState.audioFile);
if (status == 0)
{
    status = AudioQueueNewOutput(&playState.dataFormat,
                                 AudioOutputCallback,
                                 &playState,
                                 CFRunLoopGetCurrent(),
                                 kCFRunLoopCommonModes,
                                 0,
                                 &playState.queue);

    if (status == 0)
    {
        // Allocate and prime playback buffers
        playState.playing = true;
        for (int i = 0; i < NUM_BUFFERS && playState.playing; i++)
        {
            AudioQueueAllocateBuffer(playState.queue, 16000, &playState.buffers[i]);
            AudioOutputCallback(&playState, playState.queue, playState.buffers[i]);
        }

        status = AudioQueueStart(playState.queue, NULL);
        if (status == 0)
        {
            labelStatus.text = @"Playing";
        }
    }        
}

if (status != 0)
{
    [self stopPlayback];
    labelStatus.text = @"Play failed";
}
}
1

There are 1 best solutions below

0
On

No you cannot record to MP3. The reason behind this is clearly explained in core audio documentation (page no 51 of core audio pdf).

In their own words->
iOS contains the recording codecs listed in Table 2-5. As you can see, neither MP3 nor AAC recording is available. This is due to the high CPU overhead, and consequent battery drain, of these formats.

iOS: recording audio formats

  • Apple Lossless
  • iLBC (internet Low Bitrate Codec, a speech codec)
  • IMA/ADPCM (also known as IMA-4)
  • Linear PCM
  • µμLaw and aLaw