今天我們來(lái)說(shuō)一下如何實(shí)現(xiàn)錄音與播放功能援雇。AVAudioRecorder這個(gè)類可用來(lái)實(shí)現(xiàn)錄音的采集,而我們用AVAudioPlayer則可實(shí)現(xiàn)錄音的播放时迫。首先記得導(dǎo)入#import <AVFoundation/AVFoundation.h>
我的做法是自己寫了個(gè)類颅停,重寫了init方法,稍微封裝了一下掠拳。你也可以在controller中直接用癞揉。 廢話不多說(shuō),上代碼:
- (id)init {
if (self = [super init]) {
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
//設(shè)置錄音格式 AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
//設(shè)置錄音采樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質(zhì)量)
[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
//錄音通道數(shù) 1 或 2
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
//線性采樣位數(shù) 8溺欧、16喊熟、24、32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//錄音的質(zhì)量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
//文件存放路徑
_url = [NSURL URLWithString:[self getFilePath]];
_recorder = [[AVAudioRecorder alloc]initWithURL:_url settings:recordSetting error:nil];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
//_recorder.meteringEnabled = YES;
//設(shè)置錄音時(shí)長(zhǎng)姐刁,超過(guò)這個(gè)時(shí)間后芥牌,會(huì)暫停單位是秒
//[_recorder recordForDuration:30];
//準(zhǔn)備錄音
[_recorder prepareToRecord];
}
return self;
}
- (NSString *)getFilePath {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *urlPath = [path stringByAppendingPathComponent:@"123.caf"];
return urlPath;
}
//錄音開(kāi)始
- (void)beginSoundRecording {
[_recorder record];
}
//錄音暫停
- (void)pauseSoundRecording {
[_recorder pause];
}
//錄音結(jié)束
- (void)stopSoundRecording {
[_recorder stop];
}
播放的代碼:
- (void)beginPlay {
if (_url) {
_player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[self getFilePath]] error:nil];
//設(shè)置播放循環(huán)次數(shù)
//[_player setNumberOfLoops:0];
//音量,0-1之間
//[_player setVolume:1];
[_player prepareToPlay];
//開(kāi)始播放
[_player play];
}
}
- (void)resumePlay {
//暫停之后恢復(fù)播放
[_player play];
}
- (void)pausePlay {
//暫停播放
[_player pause];
}
錄音出來(lái)的通常是caf pcm格式聂使,文件較大壁拉,而且在其他設(shè)備上還可能無(wú)法播放,可以使用lame進(jìn)行mp3轉(zhuǎn)碼:
- (NSString *)mp3FilePath {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *urlPath = [path stringByAppendingPathComponent:@"123.mp3"];
return urlPath;
}
//轉(zhuǎn)為Mp3
- (NSString *)audioCAFtoMP3 {
NSString *cafFilePath = [self getFilePath];
NSString *mp3FilePath = [self mp3FilePath];
@try {
int read, write;
FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source 被轉(zhuǎn)換的音頻文件位置
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, 44100.0);
lame_set_VBR(lame, vbr_default);
lame_set_brate(lame,8);
lame_set_mode(lame,3);
lame_set_quality(lame,2);
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 {
return mp3FilePath;
}
}
功能代碼差不多就這些柏靶,UI界面大家各抒己見(jiàn)弃理。
下面是一份帶UI界面功能實(shí)現(xiàn)的Demo,有興趣的可以看一下: