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

标签 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];

}

现在通过这个..我录制声音,当我单击某种方法时,录音停止并开始播放..我应该做什么..它在我开始录制的同时播放声音..

最佳答案

我通过使用 SC_listner 得到了解决方案。

[[SCListener sharedListener] listen];   

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

通过这样做..我得到了实时检测音调

关于iphone - 录制声音并在 iPhone 中同时播放相同的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15430878/

相关文章:

iphone - PhoneGap 禁用 iPad 支持

c++ - 在 C++ 类定义中包含 Objective C++ 类型

ios - 如何修复滚动到 UITableView 的顶部,这样它就不会出现错误?

ios - 如何在任务切换器中显示锁定屏幕并正确恢复

objective-c - AVAudioPlayer不适用于(NSString *)文件名

ios - Tableview 忘记了 UISegmentedControl 中的 selectedSegment?

ios - Apple 喜欢在 iOS 的 UICollectionView 中摇动图标?

iphone - 让另一个类的 UITextField 辞职第一响应者

ios - Swift:如果加载超过 49 个声音文件,SwiftySound 初始化错误

ios - 是否可以在后台模式下播放avaudioplayer?