2

我正在寻找将 iPhone 上的系统音频直接录制到 m4a 文件中的 xCode。具体来说,我想录制设备上播放的音频文件并同时从麦克风输入。我了解存在拾取其他音频事件的风险,并且想要暂停或停止我的录制(例如,我收到一条短信并且有提示音......想要停止录制)..

4

2 回答 2

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 = 是;[_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");
}

您可以在此处查看详细信息

于 2012-10-11T13:44:51.113 回答
0

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

AVAudioRecorder 录制 AAC/m4a

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

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

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

于 2012-10-11T21:38:40.463 回答