【问题标题】:How to play button tap sound while recording audio?如何在录制音频时播放按钮点击声音?
【发布时间】:2013-09-27 13:16:57
【问题描述】:

我正在尝试在开始录制音频时播放按钮点击声音。是否可以在录制时播放声音,现场直播?

要求:

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

【问题讨论】:

    标签: iphone ios avaudiosession


    【解决方案1】:

    尝试在后台线程中播放敲击声:

    [self performSelectorInBackground:@selector(PlayTapSound:) withObject:nil];
    // Start recording
        .
        .
    
    -(void)PlayTapSound
    {
         // play tap sound code.
    }
    

    在录制之前直接播放声音。就像prepareToRecord之后一样。

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多