iOS 녹음

3689 단어
  • 먼저 AVAudioRecorder 녹음으로 생성wav 형식 오디오
  • lamewav형식을 mp3
  • 로 변환
    - (void)recordAudio
    {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
    
        NSError *error = nil;
        audioRecorder = [[ AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:[self getAudioFilePath]] settings:[self audioRecordingSettings] error:&error];
        audioRecorder.meteringEnabled = YES;
    
        if ([audioRecorder prepareToRecord] == YES)
        {
            audioRecorder.meteringEnabled = YES;
            [audioRecorder record];
        }
        else 
        {
            NSLog(@" ");
        }
    }
    
    - (void)convertToMp3
    {
        NSString *mp3FilePath = [self getMp3FilePath];
        
        @try {
            int read, write;
            
            FILE *pcm = fopen([[self getAudioFilePath] cStringUsingEncoding:1], "rb");  //source  
            fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header
            FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");  //output  Mp3 
            
            const int PCM_SIZE = 8192;
            const int MP3_SIZE = 8192;
            short int pcm_buffer[PCM_SIZE*2];
            unsigned char mp3_buffer[MP3_SIZE];
            
            lame_t lame = lame_init();
            lame_set_in_samplerate(lame, 11025.0);
            lame_set_VBR(lame, vbr_default);
            lame_init_params(lame);
            
            do {
                read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
                if (read == 0)
                    write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
                else
                    write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
                
                fwrite(mp3_buffer, write, 1, mp3);
                
            } while (read != 0);
            
            lame_close(lame);
            fclose(mp3);
            fclose(pcm);
        }
        @catch (NSException *exception)
        {
            NSLog(@"%@",[exception description]);
        }
        @finally
        {
            NSLog(@"MP3 : %@",mp3FilePath);
            [self loadData];
        }
    }
    
    - (NSDictionary *)audioRecordingSettings{
        
        NSDictionary *result = nil;
        
        NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
        //   AVFormatIDKey==kAudioFormatLinearPCM
        [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    //    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
        // (Hz)  :AVSampleRateKey==8000/44100/96000( )
        [recordSetting setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey];
        //   1   2
        [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
        //   8、16、24、32
    //    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        // 
        [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
        
        result = [NSDictionary dictionaryWithDictionary:recordSetting];
        return result;
    }
    
    - (NSString *)getAudioFilePath
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        return [paths[0] stringByAppendingPathComponent:@"asrview.wav"];
    }
    
    - (NSString *)getMp3FilePath
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        return [paths[0] stringByAppendingPathComponent:@"asrview.mp3"];
    }
    

    http://blog.csdn.net/ysy441088327/article/details/7392842 https://github.com/Superbil/build-lame-for-iOS

    좋은 웹페이지 즐겨찾기