您的位置:首页 > 运维架构

AVAudioRecorder&AVAudioPlayer

2015-12-14 08:54 507 查看
#import <AVFoundation/AVFoundation.h>

一、录音需要设置的参数

/* keys for all formats */

AVF_EXPORT NSString *const AVFormatIDKey; /* value is an integer (format ID) from CoreAudioTypes.h */

AVF_EXPORT NSString *const AVSampleRateKey; /* value is floating point in Hertz */

AVF_EXPORT NSString *const AVNumberOfChannelsKey; /* value is an integer */

/* linear PCM keys */

AVF_EXPORT NSString *const AVLinearPCMBitDepthKey; /* value is an integer, one of: 8, 16, 24, 32 */

AVF_EXPORT NSString *const AVLinearPCMIsBigEndianKey; /* value is a BOOL */

AVF_EXPORT NSString *const AVLinearPCMIsFloatKey; /* value is a BOOL */

AVF_EXPORT NSString *const AVLinearPCMIsNonInterleaved NS_AVAILABLE(10_7, 4_0); /* value is a BOOL */

#define AVLinearPCMIsNonInterleavedKey AVLinearPCMIsNonInterleaved

/* encoder property keys */

AVF_EXPORT NSString *const AVEncoderAudioQualityKey; /* value is an integer from enum AVAudioQuality */

AVF_EXPORT NSString *const AVEncoderAudioQualityForVBRKey NS_AVAILABLE(10_9, 7_0); /* value is an integer from enum AVAudioQuality. only relevant for AVAudioBitRateStrategy_Variable */

/* only one of AVEncoderBitRateKey and AVEncoderBitRatePerChannelKey should be provided. */

AVF_EXPORT NSString *const AVEncoderBitRateKey; /* value is an integer. */

AVF_EXPORT NSString *const AVEncoderBitRatePerChannelKey NS_AVAILABLE(10_7, 4_0); /* value is an integer */

AVF_EXPORT NSString *const AVEncoderBitRateStrategyKey NS_AVAILABLE(10_9, 7_0); /* value is an AVAudioBitRateStrategy constant. see below. */

AVF_EXPORT NSString *const AVEncoderBitDepthHintKey; /* value is an integer from 8 to 32 */

/* sample rate converter property keys */

AVF_EXPORT NSString *const AVSampleRateConverterAlgorithmKey NS_AVAILABLE(10_9, 7_0); /* value is an AVSampleRateConverterAlgorithm constant. see below. */

AVF_EXPORT NSString *const AVSampleRateConverterAudioQualityKey; /* value is an integer from enum AVAudioQuality */

/* channel layout */

AVF_EXPORT NSString *const AVChannelLayoutKey NS_AVAILABLE(10_7, 4_0); /* value is an NSData containing an AudioChannelLayout */

/* property values */

/* values for AVEncoderBitRateStrategyKey */

AVF_EXPORT NSString *const AVAudioBitRateStrategy_Constant NS_AVAILABLE(10_9, 7_0);

AVF_EXPORT NSString *const AVAudioBitRateStrategy_LongTermAverage NS_AVAILABLE(10_9, 7_0);

AVF_EXPORT NSString *const AVAudioBitRateStrategy_VariableConstrained NS_AVAILABLE(10_9, 7_0);

AVF_EXPORT NSString *const AVAudioBitRateStrategy_Variable NS_AVAILABLE(10_9, 7_0);

/* values for AVSampleRateConverterAlgorithmKey */

AVF_EXPORT NSString *const AVSampleRateConverterAlgorithm_Normal NS_AVAILABLE(10_9, 7_0);

AVF_EXPORT NSString *const AVSampleRateConverterAlgorithm_Mastering NS_AVAILABLE(10_9, 7_0);

当然没有必然全部设置,示例:

//录音设置

NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];

//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量), 采样率必须要设为11025才能使转化成mp3格式后不会失真

[recordSetting setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey];

//录音通道数 1 或 2 ,要转换成mp3格式必须为双通道

[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];

//存储录音文件 记住以“caf”格式存储

recordUrl = [NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingString:@"selfRecord.caf"]];

//初始化

audioRecorder = [[AVAudioRecorder alloc] initWithURL:recordUrl settings:recordSetting error:nil];

//开启音量检测

audioRecorder.meteringEnabled = YES;

audioRecorder.delegate = self;

开启录音

audioSession = [AVAudioSession sharedInstance];

if(![audioRecorder isRecording])

{

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

[audioSession setActive:YES error:nil];

[audioRecorder prepareToRecord];

[audioRecorder peakPowerForChannel:0.0];

[audioRecorder record];

}

停止录音

[audioRecorder stop];

[audioSession setActive:NO error:nil];

播放录音

audioSession = [AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

[audioSession setActive:YES error:nil];

audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:recordUrl error:nil];

if([audioPlayer prepareToPlay]){

[audioPlayer play]

}

category取值:

/* Use this category for background sounds such as rain, car engine noise, etc. Mixes with other music.
允许你的音频播放于其他应用的声音之上*/

AVF_EXPORT NSString *const AVAudioSessionCategoryAmbient;

/* Use this category for background sounds. Other music will stop playing.
除了会停止其他程序的音频回放*/

AVF_EXPORT NSString *const AVAudioSessionCategorySoloAmbient;

/* Use this category for music tracks.静止其他应用的音频回放
应用中播放声音*/

AVF_EXPORT NSString *const AVAudioSessionCategoryPlayback;

/* Use this category when recording audio.
在这种模式下,你只能进行录音。*/

AVF_EXPORT NSString *const AVAudioSessionCategoryRecord;

/* Use this category when recording and playing back audio.
允许你的应用中同时进行声音的播放和录制*/

AVF_EXPORT NSString *const AVAudioSessionCategoryPlayAndRecord;

/* Use this category when using a hardware codec or signal processor while not playing or recording audio.用于应用中进行音频处理的情形,而不是音频回放或录制
*/

AVF_EXPORT NSString *const AVAudioSessionCategoryAudioProcessing;

/* Use this category to customize the usage of available audio accessories and built-in audio hardware. For example, this category provides an application with the ability to use an available USB output and headphone output simultaneously for separate, distinct
streams of audio data. Use of this category by an application requires a more detailed knowledge of, and interaction with, the capabilities of the available audio routes. May be used for input, output, or both. Note that not all output types and output
combinations are eligible for multi-route. Input is limited to the last-in input port. Eligible inputs consist of the following: AVAudioSessionPortUSBAudio, AVAudioSessionPortHeadsetMic, and AVAudioSessionPortBuiltInMic. Eligible outputs consist of the
following: AVAudioSessionPortUSBAudio, AVAudioSessionPortLineOut, AVAudioSessionPortHeadphones, AVAudioSessionPortHDMI, and AVAudioSessionPortBuiltInSpeaker. Note that AVAudioSessionPortBuiltInSpeaker is only allowed to be used when there are no
other eligible outputs connected. 多路复杂模式 */

AVF_EXPORT NSString *const AVAudioSessionCategoryMultiRoute NS_AVAILABLE_IOS(6_0);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: