录制音频时如何播放按键敲击声?

时间:2013-09-27 13:16:57

标签: iphone ios avaudiosession

当我开始录制音频时,我正在尝试播放按键敲击声。是否可以在录制时播放声音,直播?。

要求:

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);
}

1 个答案:

答案 0 :(得分:0)

尝试在后台线程中播放点按声音:

[self performSelectorInBackground:@selector(PlayTapSound:) withObject:nil];
// Start recording
    .
    .

-(void)PlayTapSound
{
     // play tap sound code.
}

录制前直接播放声音。喜欢在prepareToRecord之后。