#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
AVAudioRecorder *recorder;//錄音對象
AVAudioPlayer *player;//播放對象
NSURL *fileUrl;//文件路徑
}
@end
//按下錄音
- (IBAction)recoder:(id)sender {
//(1)url
NSString *urlStr = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/recoder.aac"];
//刪除之前的錄音
[[NSFileManager defaultManager]removeItemAtPath:urlStr error:nil];
fileUrl = [NSURL fileURLWithPath:urlStr];
//(2)設置錄音的音頻參數(shù)
/*
1 ID號:acc
2 采樣率(HZ):每秒從連續(xù)的信號中提取并組成離散信號的采樣個數(shù)
3 通道的個數(shù):(1 單聲道 2 立體聲)
4 采樣位數(shù)(8 16 24 32) 衡量聲音波動變化的參數(shù)
5 大端或者小端 (內存的組織方式)
6 采集信號是整數(shù)還是浮點數(shù)
7 音頻編碼質量
*/
NSDictionary *info = @{
AVFormatIDKey:[NSNumber numberWithInt:kAudioFormatMPEG4AAC],//音頻格式
AVSampleRateKey:@1000,//采樣率
AVNumberOfChannelsKey:@2,//聲道數(shù)
AVLinearPCMBitDepthKey:@8,//采樣位數(shù)
AVLinearPCMIsBigEndianKey:@NO,
AVLinearPCMIsFloatKey:@NO,
AVEncoderAudioQualityKey:[NSNumber numberWithInt:AVAudioQualityMedium],
};
/*
url:錄音文件保存的路徑
settings: 錄音的設置
error:錯誤
*/
recorder = [[AVAudioRecorder alloc]initWithURL:fileUrl settings:info error:nil];
[recorder prepareToRecord];
[recorder record];
}
//抬手結束錄音
- (IBAction)stopRecoder:(id)sender {
[recorder stop];
//手動釋放
recorder =nil;
}
//播放錄音
- (IBAction)playRecoder:(id)sender {
//多次播放的時候
player = nil;
player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileUrl error:nil];
if (player) {
[player prepareToPlay];
[player play];
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者