最近在做基于XMPP的IM,開發(fā)到發(fā)送語音消息的功能.在某度上搜了很久也沒有找到適合的方法.索性自己琢磨了一個,提供給大家參考.(其中找到的很多文章都是一個復(fù)制另一個的,很煩!沒格式?jīng)]頭沒尾的.給不了人任何思路的一堆文字)
發(fā)送語音功能模塊主要有三個模塊
Part 1 :手勢按鈕
仿照經(jīng)常使用的微信"發(fā)送消息"按鈕.當(dāng)時第一想法就是想到- (BOOL)containsPoint:(CGPoint)p;
方法.
直接上代碼
.h
#import <UIKit/UIKit.h>
@protocol DPChatToolBarAudioDelegate <NSObject>
/*
* 錄音完成
*
* @param audioData amr文件data
* @prram body 附帶信息,比如錄音時長等信息
*/
- (void)DPAudioRecordingFinishWithData:(NSData *)audioData withBodyString:(NSString *)body;
@optional
/*
* 開始錄音
*
* @param isRecording 是否開始
*
*/
- (void)DPAudioStartRecording:(BOOL)isRecording;
/*
* 錄音失敗
*/
- (void)DPAudioRecordingFail:(NSString *)reason;
/*
* 音頻值測量
*
* @param power 音頻值
*/
- (void)DPAudioSpeakPower:(float)power;
@end
@interface ChatToolBarAudioButton : UIButton
@property (nonatomic, assign) id <DPChatToolBarAudioDelegate> delegate;
@end
.m
#import "ChatToolBarAudioButton.h"
@interface ChatToolBarAudioButton () <DPAudioRecorderDelegate>
{
BOOL isShouldSendAudioMessage; //用戶是否取消發(fā)送消息
NSUInteger __block audioTimeLength; //錄音時長
}
@end
@implementation ChatToolBarAudioButton
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.layer.cornerRadius = 4;
self.clipsToBounds = YES;
[self.titleLabel setTextAlignment:NSTextAlignmentCenter];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self setTitle:@"按住 說話" forState:UIControlStateNormal];
[self setTitle:@"松開 結(jié)束" forState:UIControlStateHighlighted];
[self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordBg"] forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordSelectedBg"] forState:UIControlStateHighlighted];
//增加長按手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
longPress.minimumPressDuration = 0;
[self addGestureRecognizer:longPress];
}
return self;
}
- (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint point = [gestureRecognizer locationInView:self];
if(gestureRecognizer.state == UIGestureRecognizerStateBegan) {
[self setTitle:@"松開 結(jié)束" forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordSelectedBg"] forState:UIControlStateNormal];
[self audioStart];
} else if(gestureRecognizer.state == UIGestureRecognizerStateEnded) {
[self setTitle:@"按住 說話" forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageNamed:@"chatBar_recordBg"] forState:UIControlStateNormal];
[self audioStop];
} else if(gestureRecognizer.state == UIGestureRecognizerStateChanged) {
if ([self.layer containsPoint:point]) {
[self setTitle:@"松開 結(jié)束" forState:UIControlStateNormal];
isCancelSendAudioMessage = NO;
} else {
[self setTitle:@"松開 取消" forState:UIControlStateNormal];
isCancelSendAudioMessage = YES;
}
} else if (gestureRecognizer.state == UIGestureRecognizerStateFailed) {
NSLog(@"失敗");
} else if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
NSLog(@"取消");
}
}
- (void)audioStart
{
//開始錄音
[[DPAudioRecorder sharedInstance] startRecording];
}
//結(jié)束錄音
- (void)audioStop
{
[[DPAudioRecorder sharedInstance] stopRecording];
}
//錄音失敗
- (void)audioFailed
{
//do something
}
@end
效果圖:
Demo 地址 :https://github.com/XL-Andrew/ChatToolBarAudioButton