2012-02-22 9 views
0

私はこの例で探しています:https://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html録音mp3

私の代わりにCAFファイルのmp3を記録するために(AQRecorder.mm)それを修正。私はkAudioFileCAFTypeからkAudioFileMP3Typeに変更しましたが、ファイルを作成しません。

コードが

void AQRecorder::SetupAudioFormat(UInt32 inFormatID) 
{ 
    memset(&mRecordFormat, 0, sizeof(mRecordFormat)); 

    UInt32 size = sizeof(mRecordFormat.mSampleRate); 
    XThrowIfError(AudioSessionGetProperty( kAudioSessionProperty_CurrentHardwareSampleRate, 
             &size, 
             &mRecordFormat.mSampleRate), "couldn't get hardware sample rate"); 

    size = sizeof(mRecordFormat.mChannelsPerFrame); 
    XThrowIfError(AudioSessionGetProperty( kAudioSessionProperty_CurrentHardwareInputNumberChannels, 
             &size, 
             &mRecordFormat.mChannelsPerFrame), "couldn't get input channel count"); 

    mRecordFormat.mFormatID = inFormatID; 
    if (inFormatID == kAudioFormatLinearPCM) 
    { 
     // if we want pcm, default to signed 16-bit little-endian 
     mRecordFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; 
     mRecordFormat.mBitsPerChannel = 16; 
     mRecordFormat.mBytesPerPacket = mRecordFormat.mBytesPerFrame = (mRecordFormat.mBitsPerChannel/8) * mRecordFormat.mChannelsPerFrame; 
     mRecordFormat.mFramesPerPacket = 1; 
    } 
} 

void AQRecorder::StartRecord(CFStringRef inRecordFile) 
{ 
    int i, bufferByteSize; 
    UInt32 size; 
    CFURLRef url; 

    try {  
     mFileName = CFStringCreateCopy(kCFAllocatorDefault, inRecordFile); 

     // specify the recording format 
     SetupAudioFormat(kAudioFormatLinearPCM); 

     // create the queue 
     XThrowIfError(AudioQueueNewInput(
             &mRecordFormat, 
             MyInputBufferHandler, 
             this /* userData */, 
             NULL /* run loop */, NULL /* run loop mode */, 
             0 /* flags */, &mQueue), "AudioQueueNewInput failed"); 

     // get the record format back from the queue's audio converter -- 
     // the file may require a more specific stream description than was necessary to create the encoder. 
     mRecordPacket = 0; 

     size = sizeof(mRecordFormat); 
     XThrowIfError(AudioQueueGetProperty(mQueue, kAudioQueueProperty_StreamDescription, 
             &mRecordFormat, &size), "couldn't get queue's format"); 

     NSString *recordFile = [NSTemporaryDirectory() stringByAppendingPathComponent: (NSString*)inRecordFile];  


     NSLog(recordFile); 

     url = CFURLCreateWithString(kCFAllocatorDefault, (CFStringRef)recordFile, NULL); 

     // create the audio file kAudioFileCAFType 
     XThrowIfError(AudioFileCreateWithURL(url, kAudioFileMP3Type, &mRecordFormat, kAudioFileFlags_EraseFile, 
              &mRecordFile), "AudioFileCreateWithURL failed"); 
     CFRelease(url); 

     // copy the cookie first to give the file object as much info as we can about the data going in 
     // not necessary for pcm, but required for some compressed audio 
     CopyEncoderCookieToFile(); 

     // allocate and enqueue buffers 
     bufferByteSize = ComputeRecordBufferSize(&mRecordFormat, kBufferDurationSeconds); // enough bytes for half a second 
     for (i = 0; i < kNumberRecordBuffers; ++i) { 
      XThrowIfError(AudioQueueAllocateBuffer(mQueue, bufferByteSize, &mBuffers[i]), 
         "AudioQueueAllocateBuffer failed"); 
      XThrowIfError(AudioQueueEnqueueBuffer(mQueue, mBuffers[i], 0, NULL), 
         "AudioQueueEnqueueBuffer failed"); 
     } 
     // start the queue 
     mIsRunning = true; 
     XThrowIfError(AudioQueueStart(mQueue, NULL), "AudioQueueStart failed"); 
    } 
    catch (CAXException &e) { 
     char buf[256]; 
     fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf)); 
    } 
    catch (...) { 
     fprintf(stderr, "An unknown error occurred\n"); 
    } 

} 

になった私は、任意の設定を欠落している、または私のコードで間違って何だろうか? 、mp3はリンゴからサポートされます https://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/AudioFileConvertRef/Reference/reference.html

+1

AFAIR iOS SDKではMP3形式での録音が許可されていません。 AAC録音は可能ですが。 – hoha

+0

しかしそれは文書でサポートされています – AMH

+1

* mp3ファイルをデコードすることはできますが、* encode *することはできません。 mp3に書きたい場合は、サードパーティ製のエンコーダを探す必要があります(Frauenhofer/Thompsonでライセンスを整理する必要があります) –

答えて

2

iOSデバイスは、MP3エンコード形式への録音をサポートしていません。実際には、私はiOSデバイスのいずれかはないと思う。代替フォーマットを選択する必要があります。 Core AudioはMP3ファイルを読み込めますが、書き込むことはできません。

+0

Mac OS XのCore AudioでもMP3エンコーディングはサポートされていません。 – hoha

2

カフェをmp3ファイル形式にエンコードするためのラメライブラリを使用できます。このサンプルを確認するiOSMp3Recorder

関連する問題