WFCCSoundMessageContent.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // WFCCSoundMessageContent.m
  3. // WFChatClient
  4. //
  5. // Created by heavyrain on 2017/9/9.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCCSoundMessageContent.h"
  9. #import "WFCCUtilities.h"
  10. #import "wav_amr.h"
  11. #import "WFCCIMService.h"
  12. #import "Common.h"
  13. @implementation WFCCSoundMessageContent
  14. + (instancetype)soundMessageContentForWav:(NSString *)wavPath duration:(long)duration {
  15. WFCCSoundMessageContent *soundMsg = [[WFCCSoundMessageContent alloc] init];
  16. soundMsg.duration = duration;
  17. UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
  18. NSString *amrPath = [[WFCCUtilities getDocumentPathWithComponent:@"/Vioce"] stringByAppendingPathComponent:[NSString stringWithFormat:@"img%lld.amr", recordTime]];
  19. encode_amr([wavPath UTF8String], [amrPath UTF8String]);
  20. soundMsg.localPath = amrPath;
  21. return soundMsg;
  22. }
  23. - (void)updateAmrData:(NSData *)voiceData {
  24. UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
  25. NSString *amrPath = [[WFCCUtilities getDocumentPathWithComponent:@"/Vioce"] stringByAppendingPathComponent:[NSString stringWithFormat:@"img%lld.amr", recordTime]];
  26. [voiceData writeToFile:amrPath atomically:YES];
  27. self.localPath = amrPath;
  28. }
  29. - (NSData *)getWavData {
  30. NSMutableData *data = [[NSMutableData alloc] init];
  31. decode_amr([self.localPath UTF8String], data);
  32. // UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
  33. // NSString *amrPath = [[WFCCUtilities getDocumentPathWithComponent:@"/Vioce"] stringByAppendingPathComponent:[NSString stringWithFormat:@"img%lld.wav", recordTime]];
  34. //
  35. // [data writeToFile:amrPath atomically:YES];
  36. return data;
  37. }
  38. - (WFCCMessagePayload *)encode {
  39. WFCCMediaMessagePayload *payload = [[WFCCMediaMessagePayload alloc] init];
  40. payload.contentType = [self.class getContentType];
  41. payload.searchableContent = @"[声音]";
  42. payload.mediaType = Media_Type_VOICE;
  43. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  44. [dict setObject:@(_duration) forKey:@"duration"];
  45. payload.content = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];
  46. payload.remoteMediaUrl = self.remoteUrl;
  47. payload.localMediaPath = self.localPath;
  48. return payload;
  49. }
  50. - (void)decode:(WFCCMessagePayload *)payload {
  51. if ([payload isKindOfClass:[WFCCMediaMessagePayload class]]) {
  52. WFCCMediaMessagePayload *mediaPayload = (WFCCMediaMessagePayload *)payload;
  53. self.remoteUrl = mediaPayload.remoteMediaUrl;
  54. self.localPath = mediaPayload.localMediaPath;
  55. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[payload.content dataUsingEncoding:NSUTF8StringEncoding]
  56. options:kNilOptions
  57. error:nil];
  58. self.duration = [dictionary[@"duration"] longValue];
  59. }
  60. }
  61. + (int)getContentType {
  62. return MESSAGE_CONTENT_TYPE_SOUND;
  63. }
  64. + (int)getContentFlags {
  65. return WFCCPersistFlag_PERSIST_AND_COUNT;
  66. }
  67. + (void)load {
  68. [[WFCCIMService sharedWFCIMService] registerMessageContent:self];
  69. }
  70. - (NSString *)digest {
  71. return @"[声音]";
  72. }
  73. @end