WFCCSoundMessageContent.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. if ([@"mp3" isEqualToString:[self.localPath pathExtension]]) {
  31. return [NSData dataWithContentsOfFile:self.localPath];
  32. } else {
  33. NSMutableData *data = [[NSMutableData alloc] init];
  34. decode_amr([self.localPath UTF8String], data);
  35. // UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
  36. // NSString *amrPath = [[WFCCUtilities getDocumentPathWithComponent:@"/Vioce"] stringByAppendingPathComponent:[NSString stringWithFormat:@"img%lld.wav", recordTime]];
  37. //
  38. // [data writeToFile:amrPath atomically:YES];
  39. return data;
  40. }
  41. }
  42. - (WFCCMessagePayload *)encode {
  43. WFCCMediaMessagePayload *payload = [[WFCCMediaMessagePayload alloc] init];
  44. payload.extra = self.extra;
  45. payload.contentType = [self.class getContentType];
  46. payload.searchableContent = @"[声音]";
  47. payload.mediaType = Media_Type_VOICE;
  48. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  49. [dict setObject:@(_duration) forKey:@"duration"];
  50. payload.content = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];
  51. payload.remoteMediaUrl = self.remoteUrl;
  52. payload.localMediaPath = self.localPath;
  53. return payload;
  54. }
  55. - (void)decode:(WFCCMessagePayload *)payload {
  56. [super decode:payload];
  57. if ([payload isKindOfClass:[WFCCMediaMessagePayload class]]) {
  58. WFCCMediaMessagePayload *mediaPayload = (WFCCMediaMessagePayload *)payload;
  59. self.remoteUrl = mediaPayload.remoteMediaUrl;
  60. self.localPath = mediaPayload.localMediaPath;
  61. NSError *__error = nil;
  62. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[payload.content dataUsingEncoding:NSUTF8StringEncoding]
  63. options:kNilOptions
  64. error:&__error];
  65. if (!__error) {
  66. self.duration = [dictionary[@"duration"] longValue];
  67. }
  68. }
  69. }
  70. + (int)getContentType {
  71. return MESSAGE_CONTENT_TYPE_SOUND;
  72. }
  73. + (int)getContentFlags {
  74. return WFCCPersistFlag_PERSIST_AND_COUNT;
  75. }
  76. + (void)load {
  77. [[WFCCIMService sharedWFCIMService] registerMessageContent:self];
  78. }
  79. - (NSString *)digest:(WFCCMessage *)message {
  80. return @"[声音]";
  81. }
  82. @end