Skip to content

Commit cfbc0c2

Browse files
sumengsumeng
authored andcommitted
语音私信增加服务端接口
1 parent c7da086 commit cfbc0c2

8 files changed

Lines changed: 67 additions & 31 deletions

File tree

Coding_iOS/Controllers/ConversationViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
237237
PrivateMessage *curMsg = [_myPriMsgs.dataList objectAtIndex:curIndex];
238238
if (curMsg.hasMedia) {
239239
cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_MessageMedia forIndexPath:indexPath];
240-
}else if (curMsg.voiceMedia) {
240+
}else if (curMsg.file || curMsg.voiceMedia) {
241241
cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_MessageVoice forIndexPath:indexPath];
242242
}else{
243243
cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_Message forIndexPath:indexPath];
@@ -280,7 +280,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
280280
[menuItemArray addObject:@"拷贝文字"];
281281
}
282282
}else{
283-
if (!curMsg.voiceMedia) {
283+
if (!(curMsg.voiceMedia || curMsg.file)) {
284284
[menuItemArray addObject:@"拷贝"];
285285
}
286286
}

Coding_iOS/Models/PrivateMessage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ typedef NS_ENUM(NSInteger, PrivateMessageSendStatus) {
1717
};
1818

1919
@interface PrivateMessage : NSObject
20-
@property (readwrite, nonatomic, strong) NSString *content, *extra;
20+
@property (readwrite, nonatomic, strong) NSString *content, *extra, *file;
2121
@property (readwrite, nonatomic, strong) User *friend, *sender;
22-
@property (readwrite, nonatomic, strong) NSNumber *count, *unreadCount, *id, *read_at, *status;
22+
@property (readwrite, nonatomic, strong) NSNumber *count, *unreadCount, *id, *read_at, *status, *duration;
2323
@property (readwrite, nonatomic, strong) NSDate *created_at;
2424
@property (readwrite, nonatomic, strong) HtmlMedia *htmlMedia;
2525
@property (assign, nonatomic) PrivateMessageSendStatus sendStatus;

Coding_iOS/Util/Manager/CodingNetAPIClient.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ typedef NS_ENUM(NSInteger, IllegalContentType) {
5353
successBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
5454
failureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
5555
progerssBlock:(void (^)(CGFloat progressValue))progress;
56+
57+
- (void)uploadVoice:(NSString *)file
58+
withPath:(NSString *)path
59+
withParams:(NSDictionary*)params
60+
andBlock:(void (^)(id data, NSError *error))block;
5661
@end

Coding_iOS/Util/Manager/CodingNetAPIClient.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,37 @@ - (void)uploadImage:(UIImage *)image path:(NSString *)path name:(NSString *)name
264264
}];
265265
[operation start];
266266
}
267+
268+
- (void)uploadVoice:(NSString *)file
269+
withPath:(NSString *)path
270+
withParams:(NSDictionary*)params
271+
andBlock:(void (^)(id data, NSError *error))block {
272+
273+
if (![[NSFileManager defaultManager] fileExistsAtPath:file]) {
274+
return;
275+
}
276+
277+
NSData *data = [NSData dataWithContentsOfFile:file];
278+
NSString *fileName = [file lastPathComponent];
279+
280+
DebugLog(@"\nuploadVoiceSize\n%@ : %.0f", fileName, (float)data.length/1024);
281+
282+
AFHTTPRequestOperation *operation = [self POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
283+
[formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"audio/amr"];
284+
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
285+
DebugLog(@"\n===========response===========\n%@:\n%@", path, responseObject);
286+
id error = [self handleResponse:responseObject autoShowError:YES];
287+
if (error) {
288+
block(nil, error);
289+
}else{
290+
block(responseObject, nil);
291+
}
292+
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
293+
DebugLog(@"\n===========response===========\n%@:\n%@", path, error);
294+
[self showError:error];
295+
block(nil, error);
296+
}];
297+
298+
[operation start];
299+
}
267300
@end

Coding_iOS/Util/Manager/Coding_NetAPIManager.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,6 @@ typedef NS_ENUM(NSUInteger, VerifyType){
199199
doneBlock:(void (^)(NSString *imagePath, NSError *error))done
200200
progerssBlock:(void (^)(CGFloat progressValue))progress;
201201

202-
- (void)uploadTweetVoice:(NSString *)voiceFile
203-
duration:(NSTimeInterval)duration
204-
doneBlock:(void (^)(NSString *voicePath, NSError *error))done
205-
progerssBlock:(void (^)(CGFloat progressValue))progress;
206-
207202
- (void)request_UpdateUserIconImage:(UIImage *)image
208203
successBlock:(void (^)(id responseObj))success
209204
failureBlock:(void (^)(NSError *error))failure

Coding_iOS/Util/Manager/Coding_NetAPIManager.m

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,17 +1743,17 @@ - (void)request_SendPrivateMessage:(PrivateMessage *)nextMsg andBlock:(void (^)(
17431743
} progerssBlock:^(CGFloat progressValue) {
17441744
}];
17451745
} else if (nextMsg.voiceMedia) {
1746-
[MobClick event:kUmeng_Event_Request label:@"发送私信_有语音"];
1747-
[self uploadTweetVoice:nextMsg.voiceMedia doneBlock:^(NSString *voicePath, NSError *error) {
1748-
if (voicePath) {
1749-
// 上传成功后,发送私信
1750-
nextMsg.extra = voicePath;
1751-
[self request_SendPrivateMessage:nextMsg andBlock:block];
1746+
[MobClick event:kUmeng_Event_Request label:@"发送私信_语音"];
1747+
[[CodingNetAPIClient sharedJsonClient] uploadVoice:nextMsg.voiceMedia.file withPath:@"api/message/send_voice" withParams:[nextMsg toSendParams] andBlock:^(id data, NSError *error) {
1748+
if (data) {
1749+
id resultData = [data valueForKeyPath:@"data"];
1750+
PrivateMessage *result = [NSObject objectOfClass:@"PrivateMessage" fromJSON:resultData];
1751+
nextMsg.sendStatus = PrivateMessageStatusSendSucess;
1752+
block(result, nil);
17521753
}else{
17531754
nextMsg.sendStatus = PrivateMessageStatusSendFail;
17541755
block(nil, error);
17551756
}
1756-
} progerssBlock:^(CGFloat progressValue) {
17571757
}];
17581758
} else {
17591759
// 发送私信
@@ -1950,16 +1950,6 @@ - (void)uploadTweetImage:(UIImage *)image
19501950
progress(progressValue);
19511951
}];
19521952
}
1953-
- (void)uploadTweetVoice:(VoiceMedia *)voiceMedia
1954-
doneBlock:(void (^)(NSString *voicePath, NSError *error))done
1955-
progerssBlock:(void (^)(CGFloat progressValue))progress {
1956-
if (voiceMedia == 0) {
1957-
done(nil, [NSError errorWithDomain:@"DATA EMPTY" code:0 userInfo:@{NSLocalizedDescriptionKey : @"语音没有读取成功"}]);
1958-
return;
1959-
}
1960-
NSError *error = [NSError errorWithDomain:@"语音上传失败" code:200 userInfo:nil];
1961-
done(nil, error);
1962-
}
19631953
- (void)request_UpdateUserIconImage:(UIImage *)image
19641954
successBlock:(void (^)(id responseObj))success
19651955
failureBlock:(void (^)(NSError *error))failure

Coding_iOS/Views/AudioView/BubblePlayView.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
2828
self = [super initWithFrame:frame];
2929
if (self) {
3030
_bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
31+
_bgImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
3132
[self addSubview:_bgImageView];
3233

3334
_playImageView = [[UIImageView alloc] init];
@@ -41,6 +42,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
4142

4243
_activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
4344
_activityView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
45+
_activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
4446
_activityView.hidesWhenStopped = YES;
4547
[_activityView stopAnimating];
4648
[self addSubview:_activityView];

Coding_iOS/Views/Cell/MessageCell.m

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ - (void)setCurPriMsg:(PrivateMessage *)curPriMsg andPrePriMsg:(PrivateMessage *)
104104
[self configSendStatus];
105105
//refresh voice view play state
106106
if (_voiceView) {
107-
[_voiceView setUrl:[NSURL fileURLWithPath:curPriMsg.voiceMedia.file]];
107+
if (curPriMsg.file) {
108+
[_voiceView setUrl:[NSURL URLWithString:curPriMsg.file]];
109+
}
110+
else {
111+
[_voiceView setUrl:[NSURL fileURLWithPath:curPriMsg.voiceMedia.file]];
112+
}
108113
}
109114
return;
110115
}else{
@@ -163,7 +168,7 @@ - (void)setCurPriMsg:(PrivateMessage *)curPriMsg andPrePriMsg:(PrivateMessage *)
163168

164169
bgImgViewSize = CGSizeMake(kMessageCell_ContentWidth +2*kMessageCell_PadingWidth,
165170
mediaViewHeight +textSize.height + kMessageCell_PadingHeight*(_curPriMsg.content.length > 0? 3:2));
166-
} else if (curPriMsg.voiceMedia) {
171+
} else if (curPriMsg.file || curPriMsg.voiceMedia) {
167172
bgImgViewSize = CGSizeMake(kMessageCell_ContentWidth, 40);
168173
} else{
169174
[_contentLabel setY:kMessageCell_PadingHeight];
@@ -172,8 +177,14 @@ - (void)setCurPriMsg:(PrivateMessage *)curPriMsg andPrePriMsg:(PrivateMessage *)
172177
}
173178

174179
if (_voiceView) {
175-
[_voiceView setUrl:[NSURL fileURLWithPath:curPriMsg.voiceMedia.file]];
176-
_voiceView.duration = curPriMsg.voiceMedia.duration;
180+
if (curPriMsg.file) {
181+
[_voiceView setUrl:[NSURL URLWithString:curPriMsg.file]];
182+
_voiceView.duration = curPriMsg.duration.doubleValue/1000;
183+
}
184+
else {
185+
[_voiceView setUrl:[NSURL fileURLWithPath:curPriMsg.voiceMedia.file]];
186+
_voiceView.duration = curPriMsg.voiceMedia.duration;
187+
}
177188
_voiceView.playStartedBlock = ^(AudioPlayView *view) {
178189
BubblePlayView *bubbleView = (BubblePlayView *)view;
179190
if (bubbleView.isUnread) {
@@ -273,7 +284,7 @@ + (CGFloat)cellHeightWithObj:(id)obj preObj:(id)preObj{
273284
CGSize textSize = [curPriMsg.content getSizeWithFont:kMessageCell_FontContent constrainedToSize:CGSizeMake(kMessageCell_ContentWidth, CGFLOAT_MAX)];
274285
CGFloat mediaViewHeight = [MessageCell mediaViewHeightWithObj:curPriMsg];
275286
cellHeight += mediaViewHeight;
276-
if (curPriMsg.voiceMedia) {
287+
if (curPriMsg.voiceMedia || curPriMsg.file) {
277288
cellHeight += kMessageCell_PadingHeight*2+40;
278289
} else {
279290
cellHeight += textSize.height + kMessageCell_PadingHeight*4;

0 commit comments

Comments
 (0)