Skip to content

Commit

Permalink
JSQ 竟然没有 AudioCell ,幸好 issues jessesquires/JSQMessagesViewController#…
Browse files Browse the repository at this point in the history
…1495 有人 pull requset ~ :) 完成录音基本功能,发送,明天添加个录音时的提示 HUD 就 ok
  • Loading branch information
BigPi committed Mar 10, 2016
1 parent 7834cf4 commit 7a32a1b
Show file tree
Hide file tree
Showing 19 changed files with 641 additions and 36 deletions.
32 changes: 26 additions & 6 deletions PiChat/PiChat.xcodeproj/project.pbxproj
Expand Up @@ -41,9 +41,11 @@
0E4F34881C763BEC006A9BAA /* CommenUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E4F34871C763BEC006A9BAA /* CommenUtil.m */; };
0E8F091B1C7AE9D10067A7EC /* MediaPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E8F091A1C7AE9D10067A7EC /* MediaPicker.m */; };
0E8F091E1C7B0B6F0067A7EC /* FileUpLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E8F091D1C7B0B6F0067A7EC /* FileUpLoader.m */; };
0EADC1AD1C78964A0001C1D8 /* AVIMTypedMessage+ToJsqMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 0EADC1AC1C78964A0001C1D8 /* AVIMTypedMessage+ToJsqMessage.m */; };
0EADC1B01C789B950001C1D8 /* BubbleImgFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = 0EADC1AF1C789B950001C1D8 /* BubbleImgFactory.m */; };
0EADC1B41C78B27E0001C1D8 /* ImageCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 0EADC1B31C78B27E0001C1D8 /* ImageCache.m */; };
0ED19F021C91DA0F00EF7472 /* JSQAudioMediaItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ED19EFF1C91DA0F00EF7472 /* JSQAudioMediaItem.m */; };
0ED19F031C91DA0F00EF7472 /* UIImage+JSQMessages.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ED19F011C91DA0F00EF7472 /* UIImage+JSQMessages.m */; };
0ED19F061C91DB3700EF7472 /* AVIMTypedMessage+ToJsqMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ED19F051C91DB3700EF7472 /* AVIMTypedMessage+ToJsqMessage.m */; };
0ED39FF21C8B489B00894D4B /* Followee.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ED39FF11C8B489B00894D4B /* Followee.m */; };
0EE2C4A51C907EE900A7DEE0 /* AGEmojiKeyBoardView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2C4A11C907EE800A7DEE0 /* AGEmojiKeyBoardView.m */; };
0EE2C4A61C907EE900A7DEE0 /* AGEmojiPageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2C4A31C907EE900A7DEE0 /* AGEmojiPageView.m */; };
Expand Down Expand Up @@ -138,12 +140,16 @@
0E8F091A1C7AE9D10067A7EC /* MediaPicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MediaPicker.m; sourceTree = "<group>"; };
0E8F091C1C7B0B6F0067A7EC /* FileUpLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileUpLoader.h; sourceTree = "<group>"; };
0E8F091D1C7B0B6F0067A7EC /* FileUpLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FileUpLoader.m; sourceTree = "<group>"; };
0EADC1AB1C78964A0001C1D8 /* AVIMTypedMessage+ToJsqMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "AVIMTypedMessage+ToJsqMessage.h"; sourceTree = "<group>"; };
0EADC1AC1C78964A0001C1D8 /* AVIMTypedMessage+ToJsqMessage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "AVIMTypedMessage+ToJsqMessage.m"; sourceTree = "<group>"; };
0EADC1AE1C789B950001C1D8 /* BubbleImgFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BubbleImgFactory.h; sourceTree = "<group>"; };
0EADC1AF1C789B950001C1D8 /* BubbleImgFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BubbleImgFactory.m; sourceTree = "<group>"; };
0EADC1B21C78B27E0001C1D8 /* ImageCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageCache.h; sourceTree = "<group>"; };
0EADC1B31C78B27E0001C1D8 /* ImageCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageCache.m; sourceTree = "<group>"; };
0ED19EFE1C91DA0F00EF7472 /* JSQAudioMediaItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSQAudioMediaItem.h; sourceTree = "<group>"; };
0ED19EFF1C91DA0F00EF7472 /* JSQAudioMediaItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSQAudioMediaItem.m; sourceTree = "<group>"; };
0ED19F001C91DA0F00EF7472 /* UIImage+JSQMessages.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+JSQMessages.h"; sourceTree = "<group>"; };
0ED19F011C91DA0F00EF7472 /* UIImage+JSQMessages.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+JSQMessages.m"; sourceTree = "<group>"; };
0ED19F041C91DB3700EF7472 /* AVIMTypedMessage+ToJsqMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "AVIMTypedMessage+ToJsqMessage.h"; sourceTree = "<group>"; };
0ED19F051C91DB3700EF7472 /* AVIMTypedMessage+ToJsqMessage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "AVIMTypedMessage+ToJsqMessage.m"; sourceTree = "<group>"; };
0ED39FF01C8B489B00894D4B /* Followee.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Followee.h; sourceTree = "<group>"; };
0ED39FF11C8B489B00894D4B /* Followee.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Followee.m; sourceTree = "<group>"; };
0EE2C4A01C907EE800A7DEE0 /* AGEmojiKeyBoardView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AGEmojiKeyBoardView.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -327,8 +333,6 @@
0E4F34841C763778006A9BAA /* StoryBoardHelper.m */,
0E4F34861C763BEC006A9BAA /* CommenUtil.h */,
0E4F34871C763BEC006A9BAA /* CommenUtil.m */,
0EADC1AB1C78964A0001C1D8 /* AVIMTypedMessage+ToJsqMessage.h */,
0EADC1AC1C78964A0001C1D8 /* AVIMTypedMessage+ToJsqMessage.m */,
0E8F09191C7AE9D10067A7EC /* MediaPicker.h */,
0E8F091A1C7AE9D10067A7EC /* MediaPicker.m */,
0EFDBA1F1C896AAE002685FE /* MBProgressHUD+Addition.h */,
Expand Down Expand Up @@ -379,9 +383,23 @@
path = Cache;
sourceTree = "<group>";
};
0ED19EFD1C91DA0F00EF7472 /* JSQMessage */ = {
isa = PBXGroup;
children = (
0ED19F041C91DB3700EF7472 /* AVIMTypedMessage+ToJsqMessage.h */,
0ED19F051C91DB3700EF7472 /* AVIMTypedMessage+ToJsqMessage.m */,
0ED19EFE1C91DA0F00EF7472 /* JSQAudioMediaItem.h */,
0ED19EFF1C91DA0F00EF7472 /* JSQAudioMediaItem.m */,
0ED19F001C91DA0F00EF7472 /* UIImage+JSQMessages.h */,
0ED19F011C91DA0F00EF7472 /* UIImage+JSQMessages.m */,
);
path = JSQMessage;
sourceTree = "<group>";
};
0EE2C49E1C907EE800A7DEE0 /* Vendor */ = {
isa = PBXGroup;
children = (
0ED19EFD1C91DA0F00EF7472 /* JSQMessage */,
0EE2C49F1C907EE800A7DEE0 /* AGEmojiKeyboard */,
);
path = Vendor;
Expand Down Expand Up @@ -687,6 +705,7 @@
buildActionMask = 2147483647;
files = (
0EE2C4A51C907EE900A7DEE0 /* AGEmojiKeyBoardView.m in Sources */,
0ED19F031C91DA0F00EF7472 /* UIImage+JSQMessages.m in Sources */,
0E4F34721C76027A006A9BAA /* MomentsViewController.m in Sources */,
0E4F34411C75F79B006A9BAA /* AppDelegate.m in Sources */,
0E4F345B1C760096006A9BAA /* UserManager.m in Sources */,
Expand All @@ -702,11 +721,12 @@
0E4F34851C763778006A9BAA /* StoryBoardHelper.m in Sources */,
0E4F34751C7602D7006A9BAA /* RegexUtil.m in Sources */,
0E4F34551C75F9D6006A9BAA /* User.m in Sources */,
0EADC1AD1C78964A0001C1D8 /* AVIMTypedMessage+ToJsqMessage.m in Sources */,
0ED19F021C91DA0F00EF7472 /* JSQAudioMediaItem.m in Sources */,
0E4F346F1C760248006A9BAA /* MessagesTableViewController.m in Sources */,
0E4F34451C75F7C1006A9BAA /* main.m in Sources */,
0E391A2D1C8F170F000066C8 /* InputContentView.m in Sources */,
0E8F091E1C7B0B6F0067A7EC /* FileUpLoader.m in Sources */,
0ED19F061C91DB3700EF7472 /* AVIMTypedMessage+ToJsqMessage.m in Sources */,
0EFDBA211C896AAE002685FE /* MBProgressHUD+Addition.m in Sources */,
0E4F344B1C75F82E006A9BAA /* LoginViewController.m in Sources */,
0E4F34821C76371A006A9BAA /* MainTabBarController.m in Sources */,
Expand Down
30 changes: 20 additions & 10 deletions PiChat/PiChat/App/Chat/AudioRecorderController.m
Expand Up @@ -11,8 +11,6 @@

@import AVFoundation;



@interface AudioRecorderController ()<AVAudioRecorderDelegate>
@property (strong,nonatomic) AVAudioRecorder *recorder;
@property (strong,nonatomic) AVAudioPlayer *player;
Expand All @@ -21,34 +19,40 @@ @interface AudioRecorderController ()<AVAudioRecorderDelegate>
@implementation AudioRecorderController
-(AVAudioRecorder *)recorder{
if(!_recorder){
NSString *audioPath=[NSTemporaryDirectory() stringByAppendingPathComponent:@"test.caf"];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey]; //设置录音格式
[dic setObject:@(8000) forKey:AVSampleRateKey]; //设置采样率
[dic setObject:@(1) forKey:AVNumberOfChannelsKey]; //设置通道,这里采用单声道
[dic setObject:@(8) forKey:AVLinearPCMBitDepthKey]; //每个采样点位数,分为8,16,24,32
[dic setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
_recorder=[[AVAudioRecorder alloc]initWithURL:[NSURL URLWithString:audioPath] settings:dic error:nil];
_recorder=[[AVAudioRecorder alloc]initWithURL:[self cacheFilePathUrl] settings:dic error:nil];
_recorder.delegate=self;
}
return _recorder;
}

-(AVAudioPlayer *)player{
if(!_player){
NSString *audioPath=[NSTemporaryDirectory() stringByAppendingPathComponent:@"test.caf"];
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:audioPath] error:nil];
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:[self cacheFilePathUrl] error:nil];
}
return _player;
}

-(NSURL*)cacheFilePathUrl{
NSString *cacheDir= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
return [NSURL fileURLWithPath:[cacheDir stringByAppendingPathComponent:@"baba.caf"]];
}

#pragma mark - AVAudioRecorderDelegate
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)success{
if(success){
NSError *error;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
[audioSession setActive:YES error:&error];
//测试用 录音后立即播放 ~
// [self.player play];
[self.delegate audioRecorder:self didEndRecord:recorder.url];
[self.player play];
}else{
[self.delegate audioRecorder:self didEndRecord:nil];
}
Expand All @@ -58,12 +62,18 @@ -(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:

- (void)startRecord {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
NSError *error;
[audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
[audioSession setActive:YES error:&error];
if(error){
NSLog(@"%@",error);
return;
}
[self.recorder record];
}

- (void)endRecord {
[self.recorder stop];
}

@end
@end
10 changes: 4 additions & 6 deletions PiChat/PiChat/App/Chat/PrivateChatController.m
Expand Up @@ -203,13 +203,12 @@ -(void)uploadingMediaNotification:(NSNotification*)noti{
msg=[AVIMVideoMessage messageWithText:@"" file:media attributes:nil];
break;
case UploadedMediaTypeAduio:
msg=[AVIMAudioMessage messageWithText:@"" file:media attributes:nil];
msg=[AVIMAudioMessage messageWithText:@"asd" file:media attributes:nil];
break;
case UploadedMediaTypePhoto:
msg=[AVIMImageMessage messageWithText:@"" file:media attributes:nil];
break;
case UploadedMediaTypeFile:

break;

}
Expand Down Expand Up @@ -300,12 +299,11 @@ -(void)addTypedMessage:(AVIMTypedMessage*)msgToAdd {
}

#pragma mark - AudioRecorderDelegate

-(void)audioRecorder:(AudioRecorderController *)recorder didEndRecord:(NSURL *)audio{
if(audio){
[MBProgressHUD showProgressInView:self.view];
NSString *cachePath= [CommenUtil saveFileToCache:audio];
[self.fileUpLoader uploadAudioAtUrl:[NSURL URLWithString:cachePath]];
// NSString *cachePath= [CommenUtil saveFileToCache:audio];
// [self.fileUpLoader uploadAudioAtUrl:[NSURL URLWithString:cachePath]];
[self.fileUpLoader uploadAudioAtUrl:audio];
}
}

Expand Down
4 changes: 2 additions & 2 deletions PiChat/PiChat/App/LeanCloud/FileUpLoader.m
Expand Up @@ -51,8 +51,8 @@ -(void)uploadAudioAtUrl:(NSURL*)url{
}

-(void)uploadTypedFileAtUrl:(NSURL *)url type:(UploadedMediaType)type{
NSString *cachePath= [CommenUtil saveFileToCache:url];
AVFile *file=[AVFile fileWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cachePath]]];
NSData *data= [NSData dataWithContentsOfURL:url];
AVFile *file=[AVFile fileWithData:[NSData dataWithContentsOfURL:url]];
[self uploadAVFileAtUrl:file type:type];
}

Expand Down
3 changes: 2 additions & 1 deletion PiChat/PiChat/App/Util/CommenUtil.m
Expand Up @@ -40,7 +40,8 @@ +(NSString*)saveFileToDocument:(NSURL*)file{

//
+(NSString*)saveDataToCache:(NSData *)data fileName:(NSString*)fileName{
return [self saveData:data toDirectory:NSTemporaryDirectory() fileName:fileName];
NSString *cachePath= [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
return [self saveData:data toDirectory:cachePath fileName:fileName];
}

+(NSString*)saveDataToDocument:(NSData *)data fileName:(NSString*)fileName{
Expand Down
Expand Up @@ -12,6 +12,7 @@
#import "UserManager.h"
#import <AVOSCloudIM.h>
#import <JSQMessagesViewController/JSQMessages.h>
#import "JSQAudioMediaItem.h"

@implementation AVIMTypedMessage (ToJsqMessage)

Expand Down Expand Up @@ -48,11 +49,13 @@ -(void)toJsqMessageWithCallback:(JsqMsgBlock)callback{
break;
}
case kAVIMMessageMediaTypeVideo:{
AVIMVideoMessage* receiveVideoMessage=(AVIMVideoMessage*)self;
NSString* format=receiveVideoMessage.format;
NSError* error;
NSString* path=[self fetchDataOfMessageFile:self.file fileName:[NSString stringWithFormat:@"%@.%@",self.messageId,format] error:&error];
NSURL *videoURL = [NSURL fileURLWithPath:path];
AVIMVideoMessage* videoMessage=(AVIMVideoMessage*)self;
NSString *cachePath= [self fileCacheUrlForFileName:[NSString stringWithFormat:@"%@.%@",self.messageId,videoMessage.format]];
if(![[NSFileManager defaultManager]fileExistsAtPath:cachePath]){
[self fetchDataOfMessageFile:videoMessage.file toDirctory:cachePath error:nil];
}

NSURL *videoURL = [NSURL fileURLWithPath:cachePath];
JSQVideoMediaItem *videoItem = [[JSQVideoMediaItem alloc] initWithFileURL:videoURL isReadyToPlay:YES];
videoItem.appliesMediaViewMaskAsOutgoing=outgoing;
message = [[JSQMessage alloc] initWithSenderId:senderId senderDisplayName:senderDisplayName date:timestamp media:videoItem];
Expand All @@ -68,20 +71,33 @@ -(void)toJsqMessageWithCallback:(JsqMsgBlock)callback{
locationItem.appliesMediaViewMaskAsOutgoing=outgoing;
message=[[JSQMessage alloc] initWithSenderId:senderId senderDisplayName:senderDisplayName date:timestamp media:locationItem];
}
default:
case kAVIMMessageMediaTypeAudio:{
AVIMAudioMessage *audioMsg=(AVIMAudioMessage*)self;
NSString *cachePath= [self fileCacheUrlForFileName:[NSString stringWithFormat:@"%@.%@",self.messageId,audioMsg.format]];
if(![[NSFileManager defaultManager]fileExistsAtPath:cachePath]){
[self fetchDataOfMessageFile:audioMsg.file toDirctory:cachePath error:nil];
}

JSQAudioMediaItem *audioItem=[[JSQAudioMediaItem alloc]initWithURL:[NSURL URLWithString:cachePath] isReadyToPlay:YES];
message=[JSQMessage messageWithSenderId:senderId displayName:senderDisplayName media:audioItem];

}
break;
}

callback(message);
}

-(NSString*)fileCacheUrlForFileName:(NSString *)fileName{
return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:fileName];
}

//把接收到的 媒体文件存到 Document 目录
-(NSString*)fetchDataOfMessageFile:(AVFile*)file fileName:(NSString*)fileName error:(NSError**)error{
NSString* path=[[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:fileName];
-(NSString*)fetchDataOfMessageFile:(AVFile*)file toDirctory:(NSString*)cachePath error:(NSError**)error{
NSData* data=[file getData:error];
if(*error==nil){
[data writeToFile:path atomically:YES];
if(error==nil){
[data writeToFile:cachePath atomically:YES];
}
return path;
return cachePath;
}
@end

0 comments on commit 7a32a1b

Please sign in to comment.