#import <UIKit/UIKit.h>
#import "AVFoundation/AVFoundation.h"
@interface AudioRecoderViewController : UIViewController
{
UILabel *label;
AVAudioRecorder *recorder; //定义专门录制的类AVAudioRecoder
AVAudioPlayer *player;
}
@property(nonatomic,retain) AVAudioRecorder *recorder;
@property(nonatomic,retain) AVAudioPlayer *player;
@end
#import "AudioRecoderViewController.h"
@interface AudioRecoderViewController ()
@end
@implementation AudioRecoderViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)loadView{
//定义UIView
UIView *view=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame] ;
view.backgroundColor=[UIColor purpleColor];
self.view=view;
//初始化label
label=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 160, 40)];
label.text=@"等待录制";
label.textColor=[UIColor greenColor];
label.textAlignment=NSTextAlignmentCenter;
[self.view addSubview:label];
//定义按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(90, 100, 160, 40);
[button setTitle:@"开始录制" forState:UIControlStateNormal];
[button addTarget:self action:@selector(startRecoder) forControlEvents:UIControlEventTouchUpInside];
//添加显示
[self.view addSubview:button];
//定义按钮
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(90, 160, 160, 40);
[btn2 setTitle:@"停止录制" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(stopRecoder) forControlEvents:UIControlEventTouchUpInside];
//添加显示
[self.view addSubview:btn2];
//定义按钮
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn3.frame = CGRectMake(90, 220, 160, 40);
[btn3 setTitle:@"播放录制" forState:UIControlStateNormal];
[btn3 addTarget:self action:@selector(startRecoderPlay) forControlEvents:UIControlEventTouchUpInside];
//添加显示
[self.view addSubview:btn3];
}
-(void)startRecoder{
//设置label的显示 显示为正在录制
label.textColor=[UIColor redColor];
label.text=@"录制中...";
label.textAlignment=NSTextAlignmentCenter;
//判断当前的录制状态和播放状态
if (recorder.isRecording)
{
[recorder stop];
}
if (player.isPlaying)
{
[recorder stop];
}
NSError
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end