您的位置:首页 > 移动开发 > IOS开发

在ios中编写程序实现录音功能

2013-10-26 21:31 405 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: