录制声音并在iphone中同时播放相同的声音

时间:2013-03-15 11:11:10

标签: iphone ios avaudioplayer avaudiorecorder avaudiosession

我想录制声音并同时在我的应用程序中播放相同的声音。

录制了声音: -

 NSString *soundFilePath = [docsDir
                               stringByAppendingPathComponent:@"sound.caf"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];    
    NSDictionary *recordSettings = [NSDictionary
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],
                                    AVEncoderAudioQualityKey,
                                    [NSNumber numberWithInt:16],
                                    AVEncoderBitRateKey,
                                    [NSNumber numberWithInt: 2],
                                    AVNumberOfChannelsKey,
                                    [NSNumber numberWithFloat:44100.0],
                                    AVSampleRateKey,
                                    nil];

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:recordSettings
                     error:&error];

    if (error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
    } else {
        [audioRecorder prepareToRecord];
        [audioRecorder record];
    }

-(void)StopRecord{
[audioRecorder stop];
 NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];  
    AVAudioPlayer *newPlayer =
    [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error: nil];    
    if([soundFileURL isFileURL]){
        printf("isFileURL\n");
    }   
    [newPlayer prepareToPlay];
    [newPlayer setDelegate: self];
       [newPlayer play];

}

现在通过这个...记录声音,当我点击一些方法时,录音停止并开始播放。我应该怎么做......它在我开始录音的同一时间播放声音..

1 个答案:

答案 0 :(得分:2)

我使用SC_listner获得了解决方案。

[[SCListener sharedListener] listen];   

   if ([SCListener sharedListener] != nil)
    {
        Float32 percentage = [[SCListener sharedListener] peakPower];
        }

通过这样做..我可以检测到实时的音高