- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章iOS开发项目- 基于WebSocket的聊天通讯(2)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
公司项目需要开发一个类似qq、微信的即时im聊天功能,做到实时监控消息,需要用的技术是websocket,今天整理下语言聊天这块;其实语言聊天,包含两部分,录音和音乐播放,关于简单语言聊天功能如下图:
录音 。
在avfoundation框架中有一个avaudiorecorder类专门处理录音操作,它同样支持多种音频格式。与avaudioplayer类似,你完全可以将它看成是一个录音机控制类,下面是常用的属性和方法:
先来了解下avaudiorecorder的常用属性:
1
2
3
4
5
|
@property (readonly, getter=isrecording)
bool
recording;
//是否正在录音
@property (readonly) nsdictionary<nsstring *, id> *settings;
//录音配置
@property (readonly) nsurl *url;
//录音文件存放url
@property (readonly) nstimeinterval currenttime;
//录音时长
@property (getter=ismeteringenabled)
bool
meteringenabled;
//是否监控声波
|
常用对象方法:
1
2
3
4
5
6
7
8
|
- (
bool
)preparetorecord;
//为录音准备缓冲区
- (
bool
)record;
//录音开始,暂停后调用会恢复录音
- (
bool
)recordattime:(nstimeinterval)
time
;
//在指定时间后开始录音
- (
bool
)recordforduration:(nstimeinterval) duration;
//按指定时长录音
- (
bool
)recordattime:(nstimeinterval)
time
forduration:(nstimeinterval)duration;
//上面2个的合体
- (
void
)pause;
//中断录音
- (
void
)stop;
//停止录音
- (
bool
)deleterecording;
//删除录音,必须先停止录音再删除
|
常用的代理方法:
1
2
3
|
//录音完成后调用
- (
void
)audiorecorderdidfinishrecording:(avaudiorecorder *)recorder successfully:(
bool
)flag;
//录音编码发送错误时调用
- (
void
)audiorecorderencodeerrordidoccur:(avaudiorecorder *)recorder error:(nserror *)error;
|
音频 。
如果播放较大的音频或者要对音频有精确的控制则system sound service可能就很难满足实际需求了,通常这种情况会选择使用avfoundation.framework中的avaudioplayer来实现。avaudioplayer可以看成一个播放器,它支持多种音频格式,而且能够进行进度、音量、播放速度等控制 。
avaudioplayer的使用比较简单:
1.初始化avaudioplayer对象,此时通常指定本地文件路径.
2.设置播放器属性,例如重复次数、音量大小等.
3.调用play方法播放.
具体实现代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#import <avfoundation/avfoundation.h>
#define krecordaudiofile @"myrecord.caf"
@interface viewcontroller ()<avaudiorecorderdelegate>
{
nsstring *datename;
}
@property (weak, nonatomic) iboutlet uitableview *table;
@property (nonatomic,strong) avaudiorecorder *audiorecorder;
//音频录音机
@property (nonatomic,strong) avaudioplayer *audioplayer;
//音频播放器,用于播放录音文件
@property(nonatomic,strong) nsmutablearray *spacedata;
@end
@implementation viewcontroller
#pragma mark - 私有方法
/**
* 设置音频会话
*/
-(
void
)setaudiosession{
avaudiosession *audiosession=[avaudiosession sharedinstance];
//设置为播放和录音状态,以便可以在录制完之后播放录音
[audiosession setcategory:avaudiosessioncategoryplayandrecord error:nil];
[audiosession setactive:yes error:nil];
}
/**
* 取得录音文件设置
*
* @return 录音设置
*/
-(nsdictionary *)getaudiosetting{
nsmutabledictionary *dicm=[nsmutabledictionary dictionary];
//设置录音格式
[dicm setobject:@(kaudioformatlinearpcm) forkey:avformatidkey];
//设置录音采样率,8000是电话采样率,对于一般录音已经够了
[dicm setobject:@(8000) forkey:avsampleratekey];
//设置通道,这里采用单声道
[dicm setobject:@(1) forkey:avnumberofchannelskey];
//每个采样点位数,分为8、16、24、32
[dicm setobject:@(8) forkey:avlinearpcmbitdepthkey];
//是否使用浮点数采样
[dicm setobject:@(yes) forkey:avlinearpcmisfloatkey];
//....其他设置等
return
dicm;
}
/**
* 取得录音文件保存路径
*
* @return 录音文件路径
*/
-(nsurl *)getplaypath:(nsstring *)title{
// static int index = 0;
nsstring *urlstr=[nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) lastobject];
urlstr=[urlstr stringbyappendingpathcomponent:[nsstring stringwithformat:@
"%@%@"
,title,krecordaudiofile]];
nslog(@
"play file path:%@"
,urlstr);
nsurl *url=[nsurl fileurlwithpath:urlstr];
return
url;
}
/**
* 以日期为title,来保存录音
*
* @return <#return value description#>
*/
- (nsstring *) convertdatefromstring
{
nsdate *date = [nsdate date];
// nslog(@"%@--askl",date);
//
nsdateformatter *dateformatter = [[nsdateformatter alloc] init];
//zzz表示时区,zzz可以删除,这样返回的日期字符将不包含时区信息。
[dateformatter setdateformat:@
"yyyy-mm-dd hh:mm:ss"
];
nsstring *destdatestring = [dateformatter stringfromdate:date];
return
destdatestring;
}
|
长按录音,松开停止 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
- (
void
)setclikespacestate:(nsstring *)astate
{
nslog(@
"点击语音---"
);
if
([astate isequaltostring:@
"begin"
])
{
nslog(@
"begin---"
);
datename = [self convertdatefromstring];
//创建录音文件保存路径
nsurl *url=[self getplaypath:datename];
//创建录音格式设置
nsdictionary *setting=[self getaudiosetting];
//创建录音机
nserror *error=nil;
_audiorecorder=[[avaudiorecorder alloc]initwithurl:url settings:setting error:&error];
_audiorecorder.delegate=self;
_audiorecorder.meteringenabled=yes;
//如果要监控声波则必须设置为yes
if
(![self.audiorecorder isrecording]) {
[self.audiorecorder record];
//首次使用应用时如果调用record方法会询问用户是否允许使用麦克风
// self.timer.firedate=[nsdate distantpast];
nslog(@
"111"
);
}
}
else
{
nslog(@
"end---"
);
/** 停止录音*/
[self.audiorecorder stop];
/** 录音地址*/
nsurl *url = [self getplaypath:datename];
/** 加载数据*/
avaudioplayer *audioplayer1 = [[avaudioplayer alloc] initwithcontentsofurl:url error:nil];
model *model = [[model alloc]init];
model.duration = [nsstring stringwithformat:@
"%.f"
,audioplayer1.duration];
model.spacepath = datename;
/** table 刷新*/
[self.spacedata addobject:model];
[self.table reloaddata];
/** table 滚动到当前row*/
[self.table selectrowatindexpath:[nsindexpath indexpathforrow:(self.spacedata.count - 1) insection:0] animated:yes scrollposition:uitableviewscrollpositiontop];
}
}
|
点击table 播放 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
- (
void
)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{
model *model = self.spacedata[indexpath.row];
/** 播放录音*/
nsurl *url=[self getplaypath:model.spacepath];
nserror *error=nil;
_audioplayer=[[avaudioplayer alloc]initwithcontentsofurl:url error:&error];
_audioplayer.numberofloops=0;
[_audioplayer preparetoplay];
[self.audioplayer play];
nslog(@
"%.0f---aaaa"
,_audioplayer.duration);
/** uiimage动画数组*/
nsmutablearray *imgdata = [nsmutablearray array];
for
(
int
i=0;i<4;i++)
{
uiimage *aimage = [uiimage imagenamed:[nsstring stringwithformat:@
"chat_receiver_audio_playing00%d"
,i]];
[imgdata addobject:aimage];
}
twotableviewcell *twocell = [self.table cellforrowatindexpath:indexpath];
/** 点击动画*/
[twocell.speak setanimationimages:imgdata];
// [twocell.speak setanimationrepeatcount:1];
[twocell.speak setanimationduration:1];
[twocell.speak startanimating];
dispatch_after(dispatch_time(dispatch_time_now, (int64_t)([model.duration intvalue] * nsec_per_sec)), dispatch_get_main_queue(), ^{
[twocell.speak stopanimating];
});
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
最后此篇关于iOS开发项目- 基于WebSocket的聊天通讯(2)的文章就讲到这里了,如果你想了解更多关于iOS开发项目- 基于WebSocket的聊天通讯(2)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
有没有办法只允许 https 而不是 http 与 Neo4j 服务器通信?另外,Neo4j Shell 的通信使用哪个 channel ,http 还是 https? 最佳答案 这来自 Neo4j
您好,我有新问题 :) 我正在构建带有面板的简单时事通讯,以向注册用户和时事通讯邮件地址发送邮件。 我有此代码,但时事通讯仅发送给注册用户。谁能告诉我为什么? $zapytanie = mys
第一次发帖,所以可能会有比必要的更多的信息,但我想彻底: 我们的 C 练习之一是创建发送器和接收器程序,通过 RS232 串行通信与零调制解调器交换数据。我们使用了虚拟端口程序(如果你想测试的话,我使
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我有一台通过 RS485 连接到另一台设备的单板计算机。计算机应向设备发送请求并接收响应(使用设备相关协议(protocol))。我可以毫无问题地发送消息并且设备接收它们(例如,我可以更改设备的参数)
我目前正在尝试在我的 Visual Basic 6 应用程序中引用 .NET COM 库。我已经使用 Regasm 注册了它,并且在我的类(class)中将 ComVisible 设置为 true。但
Closed. This question needs to be more focused 。它目前不接受答案。 想改善这个问题吗?更新问题,使其仅通过 editing this post 关注一个
我尝试通过 https 协议(protocol)在 Archiva 和 Jenkins 之间建立通信,但我收到以下错误: [WARNING] Could not transfer metadata .
我完成了一个运行良好的客户端/服务器套接字通信程序。现在我想弄清楚如何做到这一点,以便我可以同时拥有到服务器的多个客户端连接。我环顾四周,似乎有不止几种不同的方法可以做到这一点。所以我来这里是想向你们
我正在 mailchimp 中制作时事通讯,我在使用 outlook 时遇到了这个问题,它一直在干扰我的两个专栏,如图所示: 这是这部分的代码:
我正在创建一份时事通讯,经过大量努力,它在除 android 的 gmail 应用程序之外的任何地方都有效。问题是它似乎有最小字体大小,这会导致我的表格损坏。 有没有办法克服最小字体大小而不是媒体查询
C++ 作为Client端 view plaincopy to clipboardprint? 复制代码 代码如下: // Client.cpp : Defines the entry poi
我创建了 ECM NewsLetter,其中包含一些网站链接(另一个项目)。在在线版本的 NewsLetter 链接中工作正常,但是当我将此 NewsLetter 发送到我的电子邮件并尝试单击我的邮件
无论出于何种原因,我的文本大小调整在 iPhone 上无法正常工作,但在 Android 和其他电子邮件格式中工作正常。似乎看不出这两个文件之间有任何区别。图片也调整了大小,似乎只是文本的问题。
我正在开发一个网站,我需要将 HTML 新闻稿发送到邮件列表。 我构建了一个 html 框架,其中包含“在浏览器中查看”和“取消订阅”链接(原因会有所不同)。在管理模块中,我发布 html 并为 ht
我正在尝试在 B-L072Z-LRWAN(Master) 和 Arduino(Slave) 之间进行 I2C 通信。 我使用以下代码成功将数据从主站发送到从站: B-L072Z-LRWAN 代码: #
我有 ECM NewsLetter,其中包含指向站点(另一个项目)的一些链接。当我通过单击发送按钮将此时事通讯发送到我的电子邮件时 - 当我从电子邮件收件箱中单击它们时,链接不起作用。它显示 404
我是一名优秀的程序员,十分优秀!