Recording audio in mp3 format in an iPhone app

前端 未结 2 1513
有刺的猬
有刺的猬 2020-12-08 23:57

I am developing an iPhone app which records the audio in .wav format.

Here is the code:

NSMutableDictionary* recordSetting = [[NSMutableDictionary al         


        
相关标签:
2条回答
  • 2020-12-09 00:41

    You cannot record in MP3, it is a proprietary format when it comes to encoding. These are the settings I use, it comes out to ~150KB for 2 min of recording time:

    NSString *tempDir = NSTemporaryDirectory();
    NSString *soundFilePath = [tempDir stringByAppendingPathComponent:@"sound.m4a"];
    
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
                                      [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
                                      [NSNumber numberWithInt:16], AVEncoderBitRateKey,
                                      [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                      [NSNumber numberWithFloat:8000.0], AVSampleRateKey,
                                      [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey,
                                      nil];
    

    Also doing conversion is a processor intense operation, if you're sending this audio to a server you can use FFMPEG with the mp3lame library to do audio conversion on the server.

    EDIT: this is the code for Android recording, it's set to AMR encoding because AAC is only supported as of Honeycomb.

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mediaRecorder.setAudioChannels(1);
    mediaRecorder.setOutputFile("sample.m4a");
    
    0 讨论(0)
  • 2020-12-09 00:56

    You cannot record audio in .mp3 file. kAudioFormatMPEGLayer3 is only for playback the .mp3 format audio. There is no codec available for .mp3 recording. You need record in aac or wav format and convert it into .mp3 format.

    Better check out the below link and images:

    Audio Recording Reference

    enter image description here

    Here it is a link to learn the basic for converting the audio format:

    Audio Converter Reference

    0 讨论(0)
提交回复
热议问题