文档章节

简易的OC录音机

C
 CBETA-陈
发布于 2016/05/23 08:14
字数 421
阅读 7
收藏 0

码上生花,ECharts 作品展示赛正式启动!>>>

 

//类库

CoreGraphics.framework

Foundation.framework

AVFoundation.framework

UIKit.framework

 

 

 

 

 

//控制器.h

 

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController<AVAudioPlayerDelegate,AVAudioRecorderDelegate>//协议

@property (strong, nonatomic) AVAudioRecorder *audioRecorder;
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

@property   UIButton *recordButton;//录音按钮
@property   UIButton *playButton;//播放按钮

-(void)recordButtonAction:(id)sender;//方法
-(void)playButtonAction:(id)sender;

@end

 

 

 

 

 

 

 

 

//控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//初始化按钮
    self.recordButton = [UIButton  buttonWithType:UIButtonTypeCustom];
    self.recordButton.frame = CGRectMake(80, 202, 200, 200);
    [self.recordButton setImage:[UIImage imageNamed:@"record"] forState:UIControlStateNormal];
    [self.recordButton  addTarget:self action:@selector(recordButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.recordButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.recordButton];
    
    
    
    
    self.playButton = [UIButton  buttonWithType:UIButtonTypeCustom];
    self.playButton.frame = CGRectMake(100, 102, 200, 200);
    [self.playButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    [self.playButton  addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.playButton];
    self.playButton.backgroundColor = [UIColor blackColor];
    self.playButton.hidden = YES;
    
    //配置Recorder,
    NSDictionary *recordSetting = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:AVAudioQualityLow],AVEncoderAudioQualityKey,
                                   [NSNumber numberWithInt:16],AVEncoderBitRateKey,
                                   [NSNumber numberWithInt:2],AVNumberOfChannelsKey,
                                   [NSNumber numberWithFloat:44100.0],AVSampleRateKey,
                                   nil];
    //录音文件保存地址的URL
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/record.caf", [[NSBundle mainBundle] resourcePath]]];
    NSError *error = nil;
    self.audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];
    
    if (error != nil) {
        NSLog(@"Init audioRecorder error: %@",error);
    }else{
        //准备就绪,等待录音,注意该方法会返回Boolean,最好做个成功判断,因为其失败的时候无任何错误信息抛出
        if ([self.audioRecorder prepareToRecord]) {
            NSLog(@"Prepare successful");
        }
    }
    

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

//方法

-(void)recordButtonAction:(id)sender
{
    if (!self.audioRecorder.recording) {
        self.playButton.hidden = YES;
        [self.audioRecorder record];
        [self.recordButton setImage:[UIImage imageNamed:@"MicButtonPressed.png"] forState:UIControlStateNormal];
    }else {
        self.playButton.hidden = NO;
        [self.audioRecorder stop];
        [self.recordButton setImage:[UIImage imageNamed:@"MicButton.png"] forState:UIControlStateNormal];
    }
}

-(void)playButtonAction:(id)sender
{
    if (!self.audioPlayer.playing) {
        self.recordButton.hidden = YES;
        NSError *error;
        NSLog(@"%@",self.audioRecorder.url);
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.audioRecorder.url error:&error];
        self.audioPlayer.delegate = self;
        if (error != nil) {
            NSLog(@"Wrong init player:%@", error);
        }else{
            [self.audioPlayer play];
        }
        [self.playButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
    }else {
        self.recordButton.hidden = NO;
        [self.audioPlayer pause];
        [self.playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    }
}

#pragma mark audio delegate
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"Finsh playing");
    self.recordButton.hidden = NO;
    [self.playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    
}

-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"Decode Error occurred");
}

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
    NSLog(@"Finish record!");
}

-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
    NSLog(@"Encode Error occurred");
}


@end

 

© 著作权归作者所有

上一篇: FMDB 的增删改查
下一篇: 简易录音机
C
粉丝 0
博文 5
码字总数 3395
作品 0
朝阳
私信 提问
加载中
请先登录后再评论。
(win+R)Windows运行常用命令

===========电脑运行常见命令============ Windows+R输入cmd 运行 rononce p 15秒关机 shutdown /s /t 30倒计时关机命令 tsshutdn 60秒倒计时关机命令 taskmgr 任务管理器 notepad 打开记事本...

osc_zg36vpk2
2019/05/10
0
0
Windows server 常用命令-备忘录

在开始-》运行,输入以下命令 gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址侦测器 explorer-------打开资源管理器 logoff---------注销命令 tsshutdn-------60秒倒计...

桩子尚品
2018/07/24
0
0
Android开源git40个App源码

(JamsMusicPlayer)很棒的音乐播放器(new) (F8)日程安排的软件 (Conversations)基于XMPP的应用 (Bitocle)可以在手机上查看自己github上的项目 (Amphitheatre)Android TV应用,数据来源于TMDb,...

拉偶有所依
2015/06/03
1.8K
2
直接开启电脑功能的命令

color 颜色值 设置cmd控制台前景和背景颜色;0=黑、1=蓝、2=绿、3=浅绿、4=红、5=紫、6=黄、7=白、8=灰、9=淡蓝、A=淡绿、B=淡浅绿、C=淡红、D=淡紫、E=淡黄、F=亮白cmd.exe--------...

lyf执念
2017/12/25
0
0
windows中dos下命令

windows中下载东西,因为文件大的缘故,下载缓慢,不知道何时下载完,可以通过在windows中使用dos命令,一定时间后自动关机,节约用电等,故此总结常用的dos命令。 mplayer2-------简易widno...

爱情89757
2016/08/13
0
0

没有更多内容

加载失败,请刷新页面

加载更多

解决Cannot download "https://github.com/sass/node-sass/releases/download/binding.nod的问题

输入命令解决即可 npm i node-sass --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/

千年典韦
19分钟前
10
0
测试jar 是否完整有错

cd WEB-INF/lib/for j in *.jar; do echo $j; jar tvf $j > /dev/null ; echo $j done; done

xiaodong16
24分钟前
19
0
ats02.

https://xdays.me/proxy%E6%9C%8D%E5%8A%A1%E5%99%A8-trafficserver%E5%9F%BA%E7%A1%80/ apache traffic server 简称ats 入坑(一)开始使用...

MtrS
25分钟前
19
0
Elasticsearch如何查询使用JSON字符串的DSL

背景 Java处理ES查询逻辑,有一种情况是用户从其他地方粘贴或者手动输入的JSON字符串的DSL语句;这个时候就没法使用QueryBuilder来构建所需要的SearchSource。查了查发现有个Wrapper的方式可...

叫我哀木涕
今天
19
0
JDBC:ResultSet Types

ResultSet Types 用于确定ResultSet的某些特征和功能。 ResultSet.TYPE_FORWARD_ONLY 只能调用 next,不能调用 previous 。否则报错 ResultSet.TYPE_SCROLL_INSENSITIVE 能调用 next/previou...

mrsuperli
昨天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部