IOS语音聊天实现
2013-06-25 18:19
344 查看
IOS语音聊天实现
整体思路:
采集(录音)------压缩------上传web
接收-------------解压-------播放
Xcode工程添加:
添加AVFoundation Framework 为使用AVAudioRecorder类,我们需要向项目添加AVFoundationframework:
在项目Groups& Files面板上展开Targets 按Control-点击或右击MicBlow 选择Add >Existing Frameworks…
按下Linked Libraries左下角的+按钮 选择AVFoundation.framework并按下Add AVFoundation.framework出现在Linked Libraries下。
新资料:
1、录音功能
1)引入两个类库
2)
1 //引入头文件
2 #import <AVFoundation/AVFoundation.h>
3 #import <CoreAudio/CoreAudioTypes.h>
4
5 //在.h文件声明变量
6 AVAudioRecorder *audioRecorder;
7
8 //在.m文件
9 AVAudioSession *audioSession = [AVAudioSessionsharedInstance];
10 [audioSession setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];
11 [audioSession setActive:YES error:nil];
12 //此变量音频会话用来设置录音模式
13 /****
14 AVAudioSessionCategorySoloAmbient
15 这个类别非常像AVAudioSessionCategoryAmbient类别,除了会停止其他程序的音频回放,比如iPod程序。当设备被设置为静音模式,你的音频回放将会停止。
16
17 AVAudioSessionCategoryRecord
18 这会停止其他应用的声音(比如iPod)并让你的应用也不能初始化音频回放(比如AVAudioPlayer)。在这种模式下,你只能进行录音。使用这个类别,调用AVAudioPlayer的prepareToPlay会返回YES,但是调用play方法将返回NO。主UI界面会照常工作。这时,即使你的设备屏幕被用户锁定了,应用的录音仍会继续。
19
20 AVAudioSessionCategoryPlayback
21 这个类别会静止其他应用的音频回放(比如iPod应用的音频回放)。你可以使用AVAudioPlayer的prepareToPlay和play方法,在你的应用中播放声音。主UI界面会照常工作。这时,即使屏幕被锁定或者设备为静音模式,音频回放都会继续。
22
23 AVAudioSessionCategoryPlayAndRecord
24 这个类别允许你的应用中同时进行声音的播放和录制。当你的声音录制或播放开始后,其他应用的声音播放将会停止。主UI界面会照常工作。这时,即使屏幕被锁定或者设备为静音模式,音频回放和录制都会继续。
25
26 AVAudioSessionCategoryAudioProcessing
27 这个类别用于应用中进行音频处理的情形,而不是音频回放或录制。设置了这种模式,你在应用中就不能播放和录制任何声音。调用AVAPlayer的prepareToPlay和play方法都将返回NO。其他应用的音频回放,比如iPod,也会在此模式下停止。
28
29 AVAudioSessionCategoryAmbient
30 这个类别不会停止其他应用的声音,相反,它允许你的音频播放于其他应用的声音之上,比如iPod。你的应用的主UI县城会工作正常。调用AVAPlayer的prepareToPlay和play方法都将返回YES。当用户锁屏时,你的应用将停止所有正在回放的音频。仅当你的应用是唯一播放该音频文件的应用时,静音模式将停止你程序的音频回放。如果正当iPod播放一手歌时,你开始播放音频,将设备设为静音模式并不能停止你的音频回放。
31 ****/
32 //创建音频文件的保存路径
33 NSFileManager *fileManager = [NSFileManagerdefaultManager];
34 NSString *recordGroupPath =[NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/record"]];
35 groupPath = [recordGroupPath retain];
36 if(![fileManager
fileExistsAtPath:recordGroupPath]) {
37 NSLog(@"没有此路径");
38 [fileManagercreateDirectoryAtPath:recordGroupPath withIntermediateDirectories:YESattributes:nil error:nil];
39 }
40
41 //根据当前时间给音频文件起名字,后缀名是caf
42 NSDateFormatter *dateFormatter =[[[NSDateFormatter alloc] init] autorelease];
43 [dateFormatter setDateFormat:@"HHmmss"];
44 NSString *recordName = [NSStringstringWithFormat:@"%@.caf",[dateFormatter
stringFromDate:[NSDate date]]];
45 NSString *recordPath = [recordGroupPathstringByAppendingFormat:@"/%@",recordName];
46 NSURL*recordURL = [[NSURL alloc] initWithString:recordPath];
47 //一些设置
48 NSMutableDictionary *recordSetting =[[NSMutableDictionary alloc] init];
49 [recordSetting setValue :[NSNumbernumberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
50 [recordSetting setValue:[NSNumbernumberWithFloat:44100.0] forKey:AVSampleRateKey];
51 [recordSetting setValue:[NSNumbernumberWithInt: 2] forKey:AVNumberOfChannelsKey];
52 [recordSetting setValue :[NSNumbernumberWithInt:16] forKey:AVLinearPCMBitDepthKey];
53 [recordSetting setValue :[NSNumbernumberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
54 [recordSetting setValue :[NSNumbernumberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
55 NSLog(@"recodrdURL= %@, recordSetting = %@",recordURL,recordSetting);
56 //创建实例,参数recordURL是刚刚创建的音频文件的路径
57 audioRecorder = [[AVAudioRecorder alloc]initWithURL:recordURL settings:recordSetting error:nil];
58 [recordURL release], recordURL = nil;
59 [recordSetting release], recordSetting =nil;
60 if([audioRecorder
prepareToRecord]) {
61 [audioRecorder record];
62 }
63
64 //结束录音
65 [audioRecorder stop];
66
67 //播放录音
68 //继承代理AVAudioPlayerDelegate
69 //.h文件声明
70 AVAudioPlayer *audioPlayer;
71 //.m文件
72 NSURL *fileURL = [[NSURL alloc]initFileURLWithPath:recordPath];
73 NSLog(@"fileURL= %@", fileURL);
74 audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
75 [audioPlayer setDelegate:self];
76 NSLog(@"持续时间:%f",audioPlayer.duration);
77 if([audioPlayer
prepareToPlay]) {
78 [audioPlayer play];
79 }
80 [fileURL release], fileURL = nil;
81
82 /* audioPlayerDidFinishPlaying:successfully:is called when a sound has finished playing. This method is NOT called if theplayer is stopped
due to an interruption. */
83 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player
successfully:(BOOL)flag
84 {
85 NSLog(@"播放完毕!");
86 }
可以给录音界面加一个计时器,见效果图
之前见过这种效果觉得很神奇,这怎么做的呢,后来发现这只是一种字体!太坑爹了!
在.h文件
1 NSTimer *timer;
2 UILabel *timeLabel;
3 NSDate *beginDate;
.m文件
4 beginDate = [[NSDate date] retain];
5 timer= [NSTimer scheduledTimerWithTimeInterval:(1.0) target:selfselector:@selector(onTimer) userInfo:nil repeats:YES];
6 timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 320 - 200, 100)];
7 [timeLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack"size:24]];
8 [timeLabel setBackgroundColor:[UIColorclearColor]];
9 [timeLabelsetTextAlignment:UITextAlignmentCenter];
10 [timeLabel setTextColor:[UIColorwhiteColor]];
11 [timeLabel setText:@"00:00:00"];
12 [self.view addSubview:timeLabel];
13
14 //NSTimer调用方法
15 - (void)onTimer
16 {
17 NSDate *nowDate = [NSDate date];
18 NSCalendar *chineseClendar = [[NSCalendaralloc ] initWithCalendarIdentifier:NSGregorianCalendar];
19 NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit;
20 NSDateComponents *dateComponent =[chineseClendar components:unitFlags fromDate:beginDate toDate:nowDateoptions:0];
21 NSLog(@"%d:%d:%d",dateComponent.hour,
dateComponent.minute, dateComponent.second);
22 timeLabel.text = [NSStringstringWithFormat:@"%0.2d:%0.2d:%0.2d",dateComponent.hour,
dateComponent.minute, dateComponent.second];
23 [chineseClendar release], chineseClendar =nil;
24 }
在使用了NSTimer之后会发现dealloc方法不回被调用了,可以在-(void)viewDidDisappear:(BOOL)animated方法中释放内存。
整体思路:
采集(录音)------压缩------上传web
接收-------------解压-------播放
Xcode工程添加:
添加AVFoundation Framework 为使用AVAudioRecorder类,我们需要向项目添加AVFoundationframework:
在项目Groups& Files面板上展开Targets 按Control-点击或右击MicBlow 选择Add >Existing Frameworks…
按下Linked Libraries左下角的+按钮 选择AVFoundation.framework并按下Add AVFoundation.framework出现在Linked Libraries下。
新资料:
1、录音功能
1)引入两个类库
2)
1 //引入头文件
2 #import <AVFoundation/AVFoundation.h>
3 #import <CoreAudio/CoreAudioTypes.h>
4
5 //在.h文件声明变量
6 AVAudioRecorder *audioRecorder;
7
8 //在.m文件
9 AVAudioSession *audioSession = [AVAudioSessionsharedInstance];
10 [audioSession setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];
11 [audioSession setActive:YES error:nil];
12 //此变量音频会话用来设置录音模式
13 /****
14 AVAudioSessionCategorySoloAmbient
15 这个类别非常像AVAudioSessionCategoryAmbient类别,除了会停止其他程序的音频回放,比如iPod程序。当设备被设置为静音模式,你的音频回放将会停止。
16
17 AVAudioSessionCategoryRecord
18 这会停止其他应用的声音(比如iPod)并让你的应用也不能初始化音频回放(比如AVAudioPlayer)。在这种模式下,你只能进行录音。使用这个类别,调用AVAudioPlayer的prepareToPlay会返回YES,但是调用play方法将返回NO。主UI界面会照常工作。这时,即使你的设备屏幕被用户锁定了,应用的录音仍会继续。
19
20 AVAudioSessionCategoryPlayback
21 这个类别会静止其他应用的音频回放(比如iPod应用的音频回放)。你可以使用AVAudioPlayer的prepareToPlay和play方法,在你的应用中播放声音。主UI界面会照常工作。这时,即使屏幕被锁定或者设备为静音模式,音频回放都会继续。
22
23 AVAudioSessionCategoryPlayAndRecord
24 这个类别允许你的应用中同时进行声音的播放和录制。当你的声音录制或播放开始后,其他应用的声音播放将会停止。主UI界面会照常工作。这时,即使屏幕被锁定或者设备为静音模式,音频回放和录制都会继续。
25
26 AVAudioSessionCategoryAudioProcessing
27 这个类别用于应用中进行音频处理的情形,而不是音频回放或录制。设置了这种模式,你在应用中就不能播放和录制任何声音。调用AVAPlayer的prepareToPlay和play方法都将返回NO。其他应用的音频回放,比如iPod,也会在此模式下停止。
28
29 AVAudioSessionCategoryAmbient
30 这个类别不会停止其他应用的声音,相反,它允许你的音频播放于其他应用的声音之上,比如iPod。你的应用的主UI县城会工作正常。调用AVAPlayer的prepareToPlay和play方法都将返回YES。当用户锁屏时,你的应用将停止所有正在回放的音频。仅当你的应用是唯一播放该音频文件的应用时,静音模式将停止你程序的音频回放。如果正当iPod播放一手歌时,你开始播放音频,将设备设为静音模式并不能停止你的音频回放。
31 ****/
32 //创建音频文件的保存路径
33 NSFileManager *fileManager = [NSFileManagerdefaultManager];
34 NSString *recordGroupPath =[NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/record"]];
35 groupPath = [recordGroupPath retain];
36 if(![fileManager
fileExistsAtPath:recordGroupPath]) {
37 NSLog(@"没有此路径");
38 [fileManagercreateDirectoryAtPath:recordGroupPath withIntermediateDirectories:YESattributes:nil error:nil];
39 }
40
41 //根据当前时间给音频文件起名字,后缀名是caf
42 NSDateFormatter *dateFormatter =[[[NSDateFormatter alloc] init] autorelease];
43 [dateFormatter setDateFormat:@"HHmmss"];
44 NSString *recordName = [NSStringstringWithFormat:@"%@.caf",[dateFormatter
stringFromDate:[NSDate date]]];
45 NSString *recordPath = [recordGroupPathstringByAppendingFormat:@"/%@",recordName];
46 NSURL*recordURL = [[NSURL alloc] initWithString:recordPath];
47 //一些设置
48 NSMutableDictionary *recordSetting =[[NSMutableDictionary alloc] init];
49 [recordSetting setValue :[NSNumbernumberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
50 [recordSetting setValue:[NSNumbernumberWithFloat:44100.0] forKey:AVSampleRateKey];
51 [recordSetting setValue:[NSNumbernumberWithInt: 2] forKey:AVNumberOfChannelsKey];
52 [recordSetting setValue :[NSNumbernumberWithInt:16] forKey:AVLinearPCMBitDepthKey];
53 [recordSetting setValue :[NSNumbernumberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
54 [recordSetting setValue :[NSNumbernumberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
55 NSLog(@"recodrdURL= %@, recordSetting = %@",recordURL,recordSetting);
56 //创建实例,参数recordURL是刚刚创建的音频文件的路径
57 audioRecorder = [[AVAudioRecorder alloc]initWithURL:recordURL settings:recordSetting error:nil];
58 [recordURL release], recordURL = nil;
59 [recordSetting release], recordSetting =nil;
60 if([audioRecorder
prepareToRecord]) {
61 [audioRecorder record];
62 }
63
64 //结束录音
65 [audioRecorder stop];
66
67 //播放录音
68 //继承代理AVAudioPlayerDelegate
69 //.h文件声明
70 AVAudioPlayer *audioPlayer;
71 //.m文件
72 NSURL *fileURL = [[NSURL alloc]initFileURLWithPath:recordPath];
73 NSLog(@"fileURL= %@", fileURL);
74 audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
75 [audioPlayer setDelegate:self];
76 NSLog(@"持续时间:%f",audioPlayer.duration);
77 if([audioPlayer
prepareToPlay]) {
78 [audioPlayer play];
79 }
80 [fileURL release], fileURL = nil;
81
82 /* audioPlayerDidFinishPlaying:successfully:is called when a sound has finished playing. This method is NOT called if theplayer is stopped
due to an interruption. */
83 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)player
successfully:(BOOL)flag
84 {
85 NSLog(@"播放完毕!");
86 }
可以给录音界面加一个计时器,见效果图
之前见过这种效果觉得很神奇,这怎么做的呢,后来发现这只是一种字体!太坑爹了!
在.h文件
1 NSTimer *timer;
2 UILabel *timeLabel;
3 NSDate *beginDate;
.m文件
4 beginDate = [[NSDate date] retain];
5 timer= [NSTimer scheduledTimerWithTimeInterval:(1.0) target:selfselector:@selector(onTimer) userInfo:nil repeats:YES];
6 timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 320 - 200, 100)];
7 [timeLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack"size:24]];
8 [timeLabel setBackgroundColor:[UIColorclearColor]];
9 [timeLabelsetTextAlignment:UITextAlignmentCenter];
10 [timeLabel setTextColor:[UIColorwhiteColor]];
11 [timeLabel setText:@"00:00:00"];
12 [self.view addSubview:timeLabel];
13
14 //NSTimer调用方法
15 - (void)onTimer
16 {
17 NSDate *nowDate = [NSDate date];
18 NSCalendar *chineseClendar = [[NSCalendaralloc ] initWithCalendarIdentifier:NSGregorianCalendar];
19 NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit;
20 NSDateComponents *dateComponent =[chineseClendar components:unitFlags fromDate:beginDate toDate:nowDateoptions:0];
21 NSLog(@"%d:%d:%d",dateComponent.hour,
dateComponent.minute, dateComponent.second);
22 timeLabel.text = [NSStringstringWithFormat:@"%0.2d:%0.2d:%0.2d",dateComponent.hour,
dateComponent.minute, dateComponent.second];
23 [chineseClendar release], chineseClendar =nil;
24 }
在使用了NSTimer之后会发现dealloc方法不回被调用了,可以在-(void)viewDidDisappear:(BOOL)animated方法中释放内存。
相关文章推荐
- Unity 实现简单的语音聊天 [iOS版本]
- IOS开发之使用Speex格式实现简单的语音聊天功能(一)
- Unity3D 实现简单的语音聊天 [iOS版本]
- IOS开发之使用Speex格式实现简单的语音聊天功能(一)
- ios播放多个PCM直播音频流,实现多人语音聊天
- IOS开发之使用Speex格式实现简单的语音聊天功能(二)
- IOS开发之使用Speex格式实现简单的语音聊天功能(二)
- iOS 基于环信SDK实现即时通讯-语音、视频聊天
- iOS - 自主实现类似微信语音视频信息聊天 (idoubs详细使用方法)1.0
- iOS开发之语音功能实现
- IOS 7 文本转语音的实现
- Unity3D 实现简单的语音聊天 [Android版本]
- C#实现多人语音聊天
- iOS开发:聊天输入框的实现
- iOS聊天下拉刷新聊天记录的实现
- iOS如何实现语音播报及后台播放
- iPhone蓝牙编程之实现语音聊天 .
- IOS开发,XMPP实现聊天,即时通讯(三)
- 谁有类似于微信的实现语音聊天的android代码,求发
- Android/IOS/Web跳转第三方QQ指定QQ号的聊天框,实现陌生人聊天