123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #import "WFCCSoundMessageContent.h"
- #import "WFCCUtilities.h"
- #import "wav_amr.h"
- #import "WFCCIMService.h"
- #import "Common.h"
- @implementation WFCCSoundMessageContent
- + (instancetype)soundMessageContentForWav:(NSString *)wavPath duration:(long)duration {
- WFCCSoundMessageContent *soundMsg = [[WFCCSoundMessageContent alloc] init];
- soundMsg.duration = duration;
-
- UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
-
- NSString *amrPath = [[WFCCUtilities getDocumentPathWithComponent:@"/Vioce"] stringByAppendingPathComponent:[NSString stringWithFormat:@"img%lld.amr", recordTime]];
-
- encode_amr([wavPath UTF8String], [amrPath UTF8String]);
-
- soundMsg.localPath = amrPath;
-
- return soundMsg;
- }
- - (void)updateAmrData:(NSData *)voiceData {
- UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
-
- NSString *amrPath = [[WFCCUtilities getDocumentPathWithComponent:@"/Vioce"] stringByAppendingPathComponent:[NSString stringWithFormat:@"img%lld.amr", recordTime]];
- [voiceData writeToFile:amrPath atomically:YES];
-
- self.localPath = amrPath;
- }
- - (NSData *)getWavData {
- if ([@"mp3" isEqualToString:[self.localPath pathExtension]]) {
- return [NSData dataWithContentsOfFile:self.localPath];
- } else {
- NSMutableData *data = [[NSMutableData alloc] init];
- decode_amr([self.localPath UTF8String], data);
-
- return data;
- }
- }
- - (WFCCMessagePayload *)encode {
- WFCCMediaMessagePayload *payload = [[WFCCMediaMessagePayload alloc] init];
- payload.extra = self.extra;
- payload.contentType = [self.class getContentType];
- payload.searchableContent = @"[声音]";
- payload.mediaType = Media_Type_VOICE;
- NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
- [dict setObject:@(_duration) forKey:@"duration"];
- payload.content = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil] encoding:NSUTF8StringEncoding];
-
- payload.remoteMediaUrl = self.remoteUrl;
- payload.localMediaPath = self.localPath;
- return payload;
- }
- - (void)decode:(WFCCMessagePayload *)payload {
- [super decode:payload];
- if ([payload isKindOfClass:[WFCCMediaMessagePayload class]]) {
- WFCCMediaMessagePayload *mediaPayload = (WFCCMediaMessagePayload *)payload;
- self.remoteUrl = mediaPayload.remoteMediaUrl;
- self.localPath = mediaPayload.localMediaPath;
-
- NSError *__error = nil;
- NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[payload.content dataUsingEncoding:NSUTF8StringEncoding]
- options:kNilOptions
- error:&__error];
- if (!__error) {
- self.duration = [dictionary[@"duration"] longValue];
- }
- }
- }
- + (int)getContentType {
- return MESSAGE_CONTENT_TYPE_SOUND;
- }
- + (int)getContentFlags {
- return WFCCPersistFlag_PERSIST_AND_COUNT;
- }
- + (void)load {
- [[WFCCIMService sharedWFCIMService] registerMessageContent:self];
- }
- - (NSString *)digest:(WFCCMessage *)message {
- return @"[声音]";
- }
- @end
|