录音

来源:互联网 发布:天谕捏脸嘴巴数据 编辑:程序博客网 时间:2024/04/27 20:08

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>


@interface ViewController :UIViewController<AVAudioPlayerDelegate>

{

   NSURL *recordedFile;

   AVAudioPlayer *player;

   AVAudioRecorder *recorder;

   BOOL isRecording;

}


@property (nonatomic)BOOL isRecording;

@property (weak, nonatomic) IBOutletUIButton *playStateButton;

@property (weak, nonatomic) IBOutletUIButton *recordStateButton;

- (IBAction)playStateButtonAction:(id)sender;

- (IBAction)recordStateButtonAction:(id)sender;



@end




- (void)viewDidLoad

{

    [superviewDidLoad];

self.isRecording =NO;

    [self.playStateButtonsetEnabled:NO];

    self.playStateButton.titleLabel.alpha = 0.5;

    recordedFile = [NSURLfileURLWithPath:[NSTemporaryDirectory()stringByAppendingString:@"RecordedFile"]];

    AVAudioSession *session = [AVAudioSessionsharedInstance];

   NSError *sessionError;

    [session setCategory:AVAudioSessionCategoryPlayAndRecorderror:&sessionError];

   if (session ==nil) {

        NSLog(@"Error creating session :%@",[sessionErrordescription]);

    }else

        [sessionsetActive:YESerror:nil];

    

}


- (IBAction)playStateButtonAction:(id)sender

{

    // If the state is laying ,pause and achange playButton text to "Play"

    if ([playerisPlaying]) {

        [self.playStateButtonsetTitle:@"Play"forState:UIControlStateNormal];

    }

    // If the track is not player ,play the track and change the play button to "Pause"

   else{

        [playerplay];

        [self.playStateButtonsetTitle:@"Pause"forState:UIControlStateNormal];

    }

}


- (IBAction)recordStateButtonAction:(id)sender

{

    // if the app is not recording,we want to start recording,disable the paly button,and make the record button say "STOP"

   if (!self.isRecording) {

       self.isRecording =YES;

        [self.recordStateButtonsetTitle:@"Stop"forState:UIControlStateNormal];

        [self.playStateButtonsetEnabled:NO];

        [self.playStateButton.titleLabelsetAlpha:0.5];

        recorder = [[AVAudioRecorderalloc]initWithURL:recordedFilesettings:nilerror:nil];

        [recorder prepareToRecord];

        [recorder record];

       player =nil;

    }

    // if the app is recording ,we want to stop recrding,enable the play button,and make the record button say "Record"

   else{

       self.isRecording =NO;

        [self.recordStateButtonsetTitle:@"record"forState:UIControlStateNormal];

        [self.playStateButtonsetEnabled:YES];

        [self.playStateButton.titleLabelsetAlpha:1];

        [recorder stop];

        

       NSError *playerError;

        player = [[AVAudioPlayeralloc]initWithContentsOfURL:recordedFileerror:&playerError];

       if (player ==nil) {

           NSLog(@"Error creating player :%@",[playerErrordescription]);

            

        }

        player.delegate =self;

        

    }

}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

{

    [self.playStateButtonsetTitle:@"Play"forState:UIControlStateNormal];

}



0 0