簡體   English   中英

錄制音頻時如何播放按鈕輕擊聲音?

[英]How to play button tap sound while recording audio?

當我開始錄制音頻時,我正在嘗試播放按鈕敲擊聲音。是否可以在錄制聲音時實時播放聲音?

要求:

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

嘗試在后台線程中播放敲擊聲音:

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

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

錄制之前直接播放聲音。 就像在prepareToRecord之后一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM