如何才能获得录制iPhone系统音频的最佳效果?

时间:2012-10-11 13:37:11

标签: iphone audio recording

我正在寻找xCode将iPhone上的系统音频直接录制到m4a文件中。具体来说,我想录制一个在设备上播放的音频文件,同时从麦克风输入。我知道有接收其他音频事件的风险,并希望暂停或停止我的录音(例如,我收到一个文本,并有一个钟声....想要停止记录)..

2 个答案:

答案 0 :(得分:1)

您可以使用 AVAudioRecorder

以下代码来自techotopia.com

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface RecordViewController : UIViewController
        <AVAudioRecorderDelegate, AVAudioPlayerDelegate>

@property (strong, nonatomic) AVAudioRecorder *audioRecorder;
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
@property (strong, nonatomic) IBOutlet UIButton *recordButton;
@property (strong, nonatomic) IBOutlet UIButton *playButton;
@property (strong, nonatomic) IBOutlet UIButton *stopButton;
- (IBAction)recordAudio:(id)sender;
- (IBAction)playAudio:(id)sender;
- (IBAction)stop:(id)sender;

@end

创建AVAudioRecorder实例

- (void)viewDidLoad {
   [super viewDidLoad];
   _playButton.enabled = NO;
   _stopButton.enabled = NO;

   NSArray *dirPaths;
   NSString *docsDir;

   dirPaths = NSSearchPathForDirectoriesInDomains(
        NSDocumentDirectory, NSUserDomainMask, YES);
   docsDir = dirPaths[0];

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

实施行动方法      - (IBAction)recordAudio:(id)sender {          if(!_audioRecorder.recording)          {                  _playButton.enabled = NO;                  _stopButton.enabled = YES;                  [_audioRecorder记录];          }     }

- (IBAction)playAudio:(id)sender {
    if (!_audioRecorder.recording)
    {
       _stopButton.enabled = YES;
       _recordButton.enabled = NO;

        NSError *error;

        _audioPlayer = [[AVAudioPlayer alloc]
        initWithContentsOfURL:_audioRecorder.url
        error:&error];

        _audioPlayer.delegate = self;

        if (error)
              NSLog(@"Error: %@",
              [error localizedDescription]);
        else
              [_audioPlayer play];
   }
}

- (IBAction)stop:(id)sender {
    _stopButton.enabled = NO;
    _playButton.enabled = YES;
    _recordButton.enabled = YES;

    if (_audioRecorder.recording)
    {
            [_audioRecorder stop];
    } else if (_audioPlayer.playing) {
            [_audioPlayer stop];
    }
}

实施代表方法

-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfully:(BOOL)flag
{
        _recordButton.enabled = YES;
        _stopButton.enabled = NO;
}

-(void)audioPlayerDecodeErrorDidOccur:
(AVAudioPlayer *)player 
error:(NSError *)error
{
        NSLog(@"Decode Error occurred");
}

-(void)audioRecorderDidFinishRecording:
(AVAudioRecorder *)recorder 
successfully:(BOOL)flag
{
}

-(void)audioRecorderEncodeErrorDidOccur:
(AVAudioRecorder *)recorder 
error:(NSError *)error
{
        NSLog(@"Encode Error occurred");
}

您可以看到详细信息here

答案 1 :(得分:0)

我从未尝试同时录制来自麦克风和流式音频的音频,但是我注意到之前的答案是录制到.caf文件而不是.m4a并且它没有设置AVAudioSession ,所以我想我会提供这个链接到我之前回答的类似问题:

AVAudioRecorder record AAC/m4a

请注意,设置AVAudioSession允许其他应用程序(例如手机应用程序)获取麦克风和扬声器的控制权并暂停您的应用程序。在我链接到的代码中,我使用Record会话,您可能希望使用Playback和Record会话来允许这两个活动。此外,您可以处理Apple提供的此文档中描述的通知,以处理应用程序中断或返回控件时发生的情况:

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html

我知道这不是一个完整的答案,但我希望它有所帮助。