2013-07-05 33 views
0

請問,我如何獲得帶有線的錄音音頻?用iPad中的線錄製音頻

我嘗試使用AudioToolbox框架,但我沒有看到Line中的選項。

我看到,這是可能的,但我也不怎麼

回答

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; 

} 

你得麗在你的界面構建器中全部使用。

希望這可以幫助你。