iOS下录音功能的实现
来源:互联网 发布:易语言传奇霸业源码 编辑:程序博客网 时间:2024/06/05 07:21
关于iOS系统下,录音功能的实现:
要导入AVFoundation.framework
在ViewController.h中
导入<AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController :UIViewController
//存储路径
@property(nonatomic,strong)NSURL *tmpFile;
//录音
@property(nonatomic,strong)AVAudioRecorder *recorder;
//播放
@property(nonatomic,strong)AVAudioPlayer *player;
//是否录音
@property(nonatomic,assign)BOOL isRecoding;
在ViewController.m中
#import "ViewController.h"
@interfaceViewController ()<AVAudioPlayerDelegate>
@property (weak,nonatomic)IBOutletUIButton *recondButton;
@property (weak,nonatomic)IBOutletUIButton *playButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//刚打开时录音状态为NO
self.isRecoding =NO;
//播放按钮不能点击
[self.playButtonsetEnabled:NO];
self.playButton.titleLabel.alpha = 0.5;
//创建一个临时文件来存储录音文件
self.tmpFile = [NSURLfileURLWithPath:[NSTemporaryDirectory()stringByAppendingString:@"TmpFile"]];
//设置后台播放
AVAudioSession *session = [AVAudioSessionsharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecorderror:&sessionError];
if (session ==nil) {
NSLog(@"Error creating sessing: %@",[sessionErrordescription]);
}else {
[sessionsetActive:YESerror:nil];
}
}
//播放按钮
- (IBAction)playPause:(id)sender
{
//判断是否正在播放
if ([self.playerisPlaying]) {
//暂停播放
[_playerpause];
//按钮显示为播放
[self.playButtonsetTitle:@"播放"forState:UIControlStateNormal];
}else {
//开始播放
[_playerplay];
//按钮显示为暂停
[self.playButtonsetTitle:@"暂停"forState:UIControlStateNormal];
}
}
//录音
- (IBAction)statrStopRecord:(id)sender
{
if (!self.isRecoding) {
//不录音时点击 让录音状态变为yes
self.isRecoding =YES;
//
[self.recondButtonsetTitle:@"停止"forState:UIControlStateNormal];
//设置播放按钮不能点击
[self.playButtonsetEnabled:NO];
self.playButton.titleLabel.alpha =0.5;
//开始录音
self.recorder = [[AVAudioRecorderalloc]initWithURL:_tmpFilesettings:nilerror:nil];
//准备记录录音
[_recorderprepareToRecord];
//启动或恢复记录的录音文件
[_recorderrecord];
_player =nil;
}else {
//录音状态点击录音按钮 停止录音
self.isRecoding =NO;
[self.recondButtonsetTitle:@"录音"forState:UIControlStateNormal];
//播放按钮可以点击
[self.playButtonsetEnabled:YES];
[self.playButton.titleLabelsetAlpha:1];
//停止录音
[_recorderstop];
_recorder =nil;
NSError *playError;
//播放录音
self.player = [[AVAudioPlayeralloc]initWithContentsOfURL:_tmpFileerror:&playError];
//当播放录音为空 打印错误信息
if (self.player ==nil) {
NSLog(@"Error crenting player: %@",[playErrordescription]);
}
self.player.delegate =self;
}
}
//当录音播放结束的时候 调用的方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
//录音在播放停止后 标题变为play
[self.playButtonsetTitle:@"播放"forState:UIControlStateNormal];
}
- iOS下录音功能的实现
- ios录音功能的实现
- iOS 录音功能的实现
- ios录音功能的实现
- iOS 录音功能的实现
- iOS录音功能的实现
- iOS录音功能的实现
- iOS录音功能的实现
- ios录音功能的实现
- IOS-IOS录音功能的实现
- iOS实现录音功能
- ios录音功能实现
- ios-录音功能的简单实现
- android下录音功能实现
- ios中录音功能的实现AudioSession的使用
- android录音功能的实现
- android录音功能的实现
- android 录音功能的实现
- 【java编程】Collections工具类之复写Fill实现部分替换
- Scala类型 6:复合类型 & with关键字
- rm 命令
- Self-aware value network in the context of i4.0 工业4.0背景下,具有感知能力的价值网络
- Linux 学习笔记(二)Linux vi以及vim命令详解
- iOS下录音功能的实现
- 构造和析构的那些事
- 小马哥------高仿红米note刷机 主板h19t h19w h19st v00系列 拆机主板图与开机识别图
- Android混淆代码打包实践总结
- Ubuntu14.04中文输入法与中文乱码问题的解决
- 14.HCNA-HNTD——路由器的FTP服务配置
- tomcat下域名的配置,ROOT.xml的作用
- stm32f103rc
- JAVA中的反射机制