0

请问,我怎样才能在线路输入时录制音频?

我尝试使用 AudioToolbox 框架,但在 Line 中没有看到该选项。

我看到这是可能的,但我不知道如何

4

1 回答 1

1

这适用于任何类型的输入、集成麦克风或 lineIn 麦克风(如耳机麦克风)

你的 ClassFile.h 是这样的:

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

@interface RecorderView : UIViewController <AVAudioRecorderDelegate> {


    NSString *mediaPath;
    NSTimer *currentTimeUpdateTimer;
    UILabel *currentTimeLabel;
    SystemSoundID soundID;
    AVAudioRecorder *recorder;
    NSMutableDictionary *recordSetting;


    IBOutlet UIButton *btnStartRecord;
    IBOutlet UIButton *btnStopRecord;
    IBOutlet UIButton *btnPauseRecord;
    IBOutlet UIButton *btnResumeRecord;
    IBOutlet UIButton *btnPlay;

    AVAudioSession *audioSession;

}


@property (nonatomic, retain)IBOutlet NSString * mediaPath;
@property (nonatomic ,retain)IBOutlet UILabel *currentTimeLabel;

- (IBAction) startRecording;
- (IBAction) stopRecording;
- (IBAction) pauseRecording;
- (IBAction) resumeRecording;
- (IBAction) playRecording;

@end

你的 classFile.m 是这样的:

#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]

@interface RecorderView ()

@end

@implementation RecorderView

@synthesize mediaPath, currentTimeLabel;

int isAudioRecord = 0;

NSError *err = nil;

- (void)viewWillAppear:(BOOL)animated {

    NSError *error = nil;
    audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&error];

    if(err) {

        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }

    [audioSession setActive:YES error:&error];


    btnStartRecord.hidden = FALSE;
    btnPlay.hidden = FALSE;
    btnStopRecord.hidden = TRUE;
    btnPauseRecord.hidden = TRUE;
    btnResumeRecord.hidden = TRUE;
    currentTimeLabel.hidden = TRUE;

}
- (void)viewDidLoad {

    [super viewDidLoad];

    // setup clock
    currentTimeUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                              target:self selector:@selector(updateAudioDisplay)
                                                            userInfo:NULL repeats:YES];

}

//---------------------------AUDIO recorder---------------------//
- (IBAction) startRecording {

    isAudioRecord = 1;
    currentTimeLabel.hidden = FALSE;
    btnStartRecord.hidden = TRUE;
    btnPlay.hidden = TRUE;
    btnStopRecord.hidden = FALSE;
    btnPauseRecord.hidden = FALSE ;
    btnResumeRecord.hidden = TRUE;


    // We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

    // We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
    [recordSetting setValue:[NSNumber numberWithFloat:32000.0] forKey:AVSampleRateKey];

    // We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
    [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

    recordSetting = [[NSMutableDictionary alloc] init];

    NSError *error;

    if (![[NSFileManager defaultManager] fileExistsAtPath:mediaPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:mediaPath withIntermediateDirectories:NO attributes:nil error:&error];
    }


    mediaPath = [NSString stringWithFormat:@"%@/myVoice.caf", DOCUMENTS_FOLDER];
    NSURL *url = [NSURL fileURLWithPath:mediaPath];
    err = nil;
    NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&error];

    if(audioData) {

        NSFileManager *fm = [NSFileManager defaultManager];
        [fm removeItemAtPath:[url path] error:&error];

    }

    err = nil;
    recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];

    if(!recorder){

        NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: [err localizedDescription]
                                  delegate: nil cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [alert show];
        return;
    }

    //prepare to record
    [recorder setDelegate:self];
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;

    BOOL audioHWAvailable = YES;

    if (! audioHWAvailable) {

        UIAlertView *cantRecordAlert = [[UIAlertView alloc] initWithTitle: @"Warning"
                                                                  message: @"Audio input hardware not available"
                                                                 delegate: nil cancelButtonTitle:@"OK"
                                                        otherButtonTitles:nil];
        [cantRecordAlert show];
        return;
    }

    // start recording
    [recorder record];

}
- (IBAction)playRecording {

    if(!mediaPath)
        mediaPath = [NSString stringWithFormat:@"%@/BastardLine.caf", DOCUMENTS_FOLDER];

    //NSLog(@"Playing sound from Path: %@",recorderFilePath);

    if(soundID) {

        AudioServicesDisposeSystemSoundID(soundID);

    }

    //Get a URL for the sound file
    NSURL *filePath = [NSURL fileURLWithPath:mediaPath isDirectory:NO];

    //Use audio sevices to create the sound
    AudioServicesCreateSystemSoundID((CFURLRef)CFBridgingRetain(filePath), &soundID);

    //Use audio services to play the sound
    AudioServicesPlaySystemSound(soundID);

}
- (void) updateAudioDisplay {

    double currentTime = recorder.currentTime;

    if (recorder == nil) {

        currentTimeLabel.text = @"";

    } else if (!recorder.isRecording) {

        currentTimeLabel.text = [NSString stringWithFormat: @"Recording  %02d:%02d",
                                 (int) currentTime/60,
                                 (int) currentTime%60];

    } else {

        currentTimeLabel.text = [NSString stringWithFormat: @"Recording %02d:%02d",
                                 (int) currentTime/60,
                                 (int) currentTime%60];
        [recorder updateMeters];

    }

}
- (IBAction) stopRecording {

    [recorder stop];
    currentTimeLabel.hidden = TRUE;
    btnStartRecord.hidden = FALSE;
    btnPlay.hidden = FALSE;
    btnStopRecord.hidden = TRUE;
    btnPauseRecord.hidden = TRUE ;
    btnResumeRecord.hidden = TRUE;
    isAudioRecord = 0;

}
- (IBAction) pauseRecording {

    [recorder pause];
    [self updateAudioDisplay];
    btnStartRecord.hidden = TRUE;
    btnResumeRecord.hidden = FALSE;
    btnPlay.hidden = FALSE;
    btnStopRecord.hidden = TRUE;
    btnPauseRecord.hidden = TRUE;

}
- (IBAction) resumeRecording {

    [recorder record];
    btnStartRecord.hidden = TRUE;
    btnPlay.hidden = TRUE;
    btnStopRecord.hidden = FALSE;
    btnPauseRecord.hidden = FALSE;
    btnResumeRecord.hidden = TRUE;

}

您必须在界面构建器中链接所有内容。

希望这可以帮到你。

于 2013-07-05T10:15:11.997 回答