iphone实现声音的录制和播放
来源:互联网 发布:销售是干嘛的知乎 编辑:程序博客网 时间:2024/06/11 23:10
在之前iPhone播放声音文件的例子中,只是实现了声音的播放,下面主要实现声音的录制,同时播放声音。采用AVAudioRecorder进行声音的录制。
下面是实现的代码:
#import "TalkingDemoViewController.h"
@implementation TalkingDemoViewController
@synthesize recorder;
// Implement viewDidLoad to do additiona l setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
-(IBAction)sayTalking:(id)sender
{
NSLog(@"播放声音");
[self playRecord];
[player play];}
-(void)playRecord
{
if (player) {
[player release];
}
NSArray *paths=NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
NSString *soundPath=[documentsDirectory stringByAppendingPathComponent:@"record.caf"];
NSLog(@"logpath<<<%@",soundPath);
//NSString *soundPath=[[NSBundle mainBundle] pathForResource:@"record" ofType:@"caf"];
NSURL *soundUrl=[[NSURL alloc] initFileURLWithPath:soundPath];
player=[[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[player prepareToPlay];
[soundUrl release];}
-(IBAction)startrecording:(id)sender
{
NSLog(@"开始录音");
[self beginRecord];
}
-(IBAction)stoprecording:(id)sender
{
NSLog(@"停止录音");
[self stopRecord];
}-(void)beginRecord
{
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//获取路径
//参数NSDocumentDirectory要获取那种路径
NSArray *paths=NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory=[paths objectAtIndex:0];//去处需要的路径
//更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
[fileManager createFileAtPath:@"fileName" contents:nil attributes:nil];
//获取文件路径
NSString *soundFilePath=[documentsDirectory stringByAppendingPathComponent:@"record.caf"];
NSLog(@"path>>%@",soundFilePath);
NSURL *fileURL=[[NSURL alloc]initFileURLWithPath:soundFilePath];
AVAudioRecorder *newRecorder=[[AVAudioRecorder alloc]initWithURL:fileURL settings:nil error:nil];
[fileURL release];
self.recorder=newRecorder;
[newRecorder release];
[self.recorder prepareToRecord];
[self.recorder setDelegate:self];
[self.recorder record];
}
-(void)stopRecord
{
if(self.recorder.recording)
{
[self.recorder stop];
}
}
- (void)dealloc {
[player release];
[super dealloc];
}@end
其中也要引入AVFoundation.framework组建。
源代码:http://easymorse-android.googlecode.com/svn/trunk/TalkingDemo/
- iphone实现声音的录制和播放
- iphone实现声音的录制和播放
- iphone实现声音的录制和播放
- IOS 实现声音的录制和播放
- iPhone中录制和播放声音
- iOS 声音的录制和播放
- iphone声音后台播放实现
- recordMP3实现网页录制声音,上传,播放
- java 声音的播放与录制
- Android 简单异步处理类------实现声音的录制及播放
- mp4v2同时录制视频和声音播放时会在中途自动终止的解决方法
- 用matlab录制声音然后进行读取和播放
- tapi3.x播放录制声音
- iPhone中播放声音
- iPhone播放声音
- linux下声音的录制与播放 /dev/dsp
- Swift - 制作一个录音机(声音的录制与播放)
- AudioRecord、AudioTractor实现音频的录制和播放
- apt解释链接(开源文摘)
- 输入子系统--event层分析
- 標準ALV 的標準按鈕實現自己的事件
- Iphone实现Loading效果
- gcc: error trying to exec 'cc1plus': execvp: No such file or directory
- iphone实现声音的录制和播放
- Flash CS5.5 新特性介绍
- 服务器建模--多访问点
- 为添加用户偷哈懒
- GeoServer开发手册1 目录
- iPhone播放声音文件的例子
- 实例详解$_SERVER函数中QUERY_STRING和REQUEST_URI等四个变量区别
- 计算机端口相关
- 在字符串里表示路径