Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在IPad中使用Line录制音频_Ios_Objective C_Ipad - Fatal编程技术网

Ios 在IPad中使用Line录制音频

Ios 在IPad中使用Line录制音频,ios,objective-c,ipad,Ios,Objective C,Ipad,请问,线路接通后,我怎样才能录制音频 我试着使用AudioToolbox框架,但我看不到合适的选项 我知道这是可能的,但我不知道这是如何与任何类型的输入、集成话筒或线入式话筒(如耳机话筒)配合使用的 您的ClassFile.h如下所示: #import <AVFoundation/AVFoundation.h> #import <AudioToolbox/AudioToolbox.h> @interface RecorderView : UIViewController

请问,线路接通后,我怎样才能录制音频

我试着使用AudioToolbox框架,但我看不到合适的选项


我知道这是可能的,但我不知道这是如何与任何类型的输入、集成话筒或线入式话筒(如耳机话筒)配合使用的

您的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
#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;

}
您必须在界面生成器中链接所有

希望这能帮助你