iOS下录音功能的实现
2015-02-27 21:13
417 查看
关于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.playButton
4000
setEnabled: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 {
//录音状态点击录音按钮 停止录音
<
b5a9
p style="margin-top:0px;margin-bottom:0px;font-family:Menlo;"> 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];
}
要导入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.playButton
4000
setEnabled: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 {
//录音状态点击录音按钮 停止录音
<
b5a9
p style="margin-top:0px;margin-bottom:0px;font-family:Menlo;"> 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 实现使用lame库 录音转MP3格式 边录边转功能
- iOS录音功能的实现
- ios中录音功能的实现AudioSession的使用
- iOS之利用系统自带API实现录音功能
- phonegap(cordova)在ios系统实现录音功能的几种特殊情况
- ios录音功能的实现
- 实现一个录音播放功能的app(ios)
- IOS开发实现录音功能
- ios中录音功能的实现AudioSession的使用
- ios录音功能实现
- iOS实现录音功能
- IOS-IOS录音功能的实现
- ios录音功能的实现