当我开始录制音频时,我正在尝试播放按钮点击声音。是否可以在录制时播放声音,现场直播?
要求:
1)如果我点击录制按钮不想在录制完成后开始录制按钮点击声音。它需要时间延迟。
2)如果我点击录制按钮不想将牛肉声音录制到录制的音频文件中。
3)我想同时播放按钮点击声音和录制音频而不重叠。
开始录制:
-(IBAction)btnRecordTap:(id)sender
{
[self playTapSound:@"press.wav"];
// Set the audio file
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
播放系统声音
//---------------play button sound--------------------
-(SystemSoundID) playASound: (NSString *) fileName
{
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath],
fileName];
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundFileObject);
//play the file
AudioServicesPlaySystemSound(soundFileObject);
return soundFileObject;
}
-(void)playTapSound:(NSString *)file
{
SystemSoundID id = [self playASound:file];
AudioServicesAddSystemSoundCompletion (id,NULL,NULL,completionCallback,(__bridge_retained void *)self);
}
static void completionCallback (SystemSoundID ssID,void *clientData)
{
AudioServicesRemoveSystemSoundCompletion (ssID);
AudioServicesDisposeSystemSoundID(ssID);
}