视同控制器.h
//
// ACYL_RecordViewController.h
// Ancun
//
// Created by Dubai on 15/4/1.
//
//
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface ACYL_RecordViewController : UIViewController<AVAudioRecorderDelegate>
{
AVAudioRecorder *recorder;
NSTimer *timer;
NSURL *urlPlay;
}
@property (strong, nonatomic) UIButton *recordBtn;//录音按钮
@property (strong, nonatomic) UIButton *playBtn;//播放按钮
@property (strong ,nonatomic) UIImageView *imageViewWave;//显示波形图的视图
@property (strong, nonatomic) AVAudioPlayer *avPlay;
@end
//
// ACYL_RecordViewController.m
// Ancun
//
// Created by Dubai on 15/4/1.
//
//
#import "ACYL_RecordViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface ACYL_RecordViewController ()
{
NSTimer *timeRecordNow;//录音时间戳
UILabel *timeLabel;//显示时间戳
}
@end
@implementation ACYL_RecordViewController
-(void)viewWillAppear:(BOOL)animated
{
self.view.backgroundColor = [UIColor whiteColor];
self.tabBarController.tabBar.translucent = NO;
}
-(void)viewDidLoad
{
[super viewDidLoad];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
[audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
if (granted) {
// Microphone enabled code
}
else {
// Microphone disabled code
}
}];
}
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
[self audio];//设置录音
//波形图
self.imageViewWave = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -SCREEN_WIDTH/1.2, 80.0, SCREEN_WIDTH/1.5, 200)];
self.imageViewWave.backgroundColor = [UIColor redColor];
[self.view addSubview:self.imageViewWave];
self.recordBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
self.recordBtn.frame = CGRectMake(SCREEN_WIDTH/6, self.imageViewWave.frame.origin.y+self.imageViewWave.frame.size.height+30, 60, 40);
[self.recordBtn setTitle:@"录音"