Skip to content

修复边录边转mp3丢失1秒左右的问题 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions RecorderDemo/RecorderDemo/ConvertAudio/ConvertAudioFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

@interface ConvertAudioFile ()
@property (nonatomic, assign) BOOL stopRecord;
@property (nonatomic, assign) BOOL isSleep;
@end

@implementation ConvertAudioFile
Expand Down Expand Up @@ -44,12 +43,12 @@ - (void)conventToMp3WithCafFilePath:(NSString *)cafFilePath
callback:(void(^)(BOOL result))callback
{

NSLog(@"convert begin!!");
NSLog(@"convert begin!!");
__weak typeof(self) weakself = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

weakself.stopRecord = NO;

@try {

int read, write;
Expand All @@ -69,6 +68,7 @@ - (void)conventToMp3WithCafFilePath:(NSString *)cafFilePath

long curpos;
BOOL isSkipPCMHeader = NO;
BOOL finish = NO;

do {
curpos = ftell(pcm);
Expand All @@ -92,20 +92,30 @@ - (void)conventToMp3WithCafFilePath:(NSString *)cafFilePath
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
fwrite(mp3_buffer, write, 1, mp3);
NSLog(@"read %d bytes", write);
weakself.isSleep = NO;
} else {
weakself.isSleep = YES;
}
// 停止录音时 length 不够 PCM_SIZE * 2 导致漏转的问题
else if (weakself.stopRecord && length) {
read = (int)fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
if (read == 0) {
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
} else {
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
}
fwrite(mp3_buffer, write, 1, mp3);
finish = YES;
}
else {
[NSThread sleepForTimeInterval:0.05];
NSLog(@"sleep");
}

} while (!weakself.stopRecord || !weakself.isSleep);
} while (!finish);

read = (int)fread(pcm_buffer, 2 * sizeof(short int), PCM_SIZE, pcm);
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);

NSLog(@"read %d bytes and flush to mp3 file", write);
lame_mp3_tags_fid(lame, mp3);

lame_close(lame);
fclose(mp3);
fclose(pcm);
Expand All @@ -123,7 +133,7 @@ - (void)conventToMp3WithCafFilePath:(NSString *)cafFilePath
NSLog(@"convert mp3 finish!!! %@", mp3FilePath);
}
});

}

/**
Expand Down Expand Up @@ -170,18 +180,18 @@ + (void)conventToMp3WithCafFilePath:(NSString *)cafFilePath

read = (int)fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
if (read == 0) {
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);

write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
} else {
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
}

fwrite(mp3_buffer, write, 1, mp3);

} while (read != 0);

lame_mp3_tags_fid(lame, mp3);

lame_close(lame);
fclose(mp3);
fclose(pcm);
Expand All @@ -202,3 +212,4 @@ + (void)conventToMp3WithCafFilePath:(NSString *)cafFilePath
}

@end