修复录音文件长度为0的问题
This commit is contained in:
@@ -1614,7 +1614,7 @@
|
|||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
PROVISIONING_PROFILE = "a8315ce6-c429-4b17-afb5-dbd05b31821a";
|
PROVISIONING_PROFILE = "a8315ce6-c429-4b17-afb5-dbd05b31821a";
|
||||||
PROVISIONING_PROFILE_SPECIFIER = "";
|
PROVISIONING_PROFILE_SPECIFIER = "";
|
||||||
"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = ylgamehall;
|
"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = jxgamehall_dev;
|
||||||
TARGETED_DEVICE_FAMILY = 1;
|
TARGETED_DEVICE_FAMILY = 1;
|
||||||
};
|
};
|
||||||
name = Debug;
|
name = Debug;
|
||||||
@@ -1667,7 +1667,7 @@
|
|||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
PROVISIONING_PROFILE = "a8315ce6-c429-4b17-afb5-dbd05b31821a";
|
PROVISIONING_PROFILE = "a8315ce6-c429-4b17-afb5-dbd05b31821a";
|
||||||
PROVISIONING_PROFILE_SPECIFIER = "";
|
PROVISIONING_PROFILE_SPECIFIER = "";
|
||||||
"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = ylgamehall;
|
"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = jxgamehall_dev;
|
||||||
TARGETED_DEVICE_FAMILY = 1;
|
TARGETED_DEVICE_FAMILY = 1;
|
||||||
};
|
};
|
||||||
name = Release;
|
name = Release;
|
||||||
|
|||||||
@@ -2014,7 +2014,7 @@
|
|||||||
//
|
//
|
||||||
// if(agentlist!=nil){
|
// if(agentlist!=nil){
|
||||||
// for (int j=0; j<[agentlist count]; j++) {
|
// for (int j=0; j<[agentlist count]; j++) {
|
||||||
// NSDictionary *infotwo=[agentlist objectAtIndex:j];
|
// NSDictionary *infotwo=[gamedata objectForKey:@"agentlist"];
|
||||||
// if([FuncPublic isBlankString:[infotwo objectForKey:@"agentid"]]==NO){
|
// if([FuncPublic isBlankString:[infotwo objectForKey:@"agentid"]]==NO){
|
||||||
//
|
//
|
||||||
// if([[infotwo objectForKey:@"agentid"] isEqualToString:self.agentinfo]){
|
// if([[infotwo objectForKey:@"agentid"] isEqualToString:self.agentinfo]){
|
||||||
@@ -2168,19 +2168,62 @@
|
|||||||
- (void)VoiceRecorderBaseVCRecordFinish:(NSString *)_filePath fileName:(NSString*)_fileName{
|
- (void)VoiceRecorderBaseVCRecordFinish:(NSString *)_filePath fileName:(NSString*)_fileName{
|
||||||
NSLog(@"录音完成,文件路径:%@ %@",_filePath,_fileName);
|
NSLog(@"录音完成,文件路径:%@ %@",_filePath,_fileName);
|
||||||
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
|
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
|
||||||
AVAudioPlayer *play = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:_filePath] error:nil];
|
|
||||||
|
// 检查文件是否存在
|
||||||
|
if (![[NSFileManager defaultManager] fileExistsAtPath:_filePath]) {
|
||||||
|
NSLog(@"录音文件不存在: %@", _filePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用fileURLWithPath而不是URLWithString,因为是本地文件路径
|
||||||
|
NSError *error = nil;
|
||||||
|
AVAudioPlayer *play = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:_filePath] error:&error];
|
||||||
|
|
||||||
|
if (error || play == nil) {
|
||||||
|
NSLog(@"创建AVAudioPlayer时出错: %@", [error localizedDescription]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取音频时长
|
||||||
NSTimeInterval interval = play.duration;
|
NSTimeInterval interval = play.duration;
|
||||||
NSInteger time = round(interval);
|
NSInteger time = round(interval);
|
||||||
//4.播放
|
NSLog(@"录音时长: %ld秒", (long)time);
|
||||||
if (play == nil)
|
|
||||||
{
|
|
||||||
NSLog(@"ERror creating player: %@", [play description]);
|
|
||||||
}else{
|
|
||||||
|
|
||||||
|
// 如果录音时长太短(小于1秒),可能是无效录音
|
||||||
|
if (interval < 1.0) {
|
||||||
|
NSLog(@"录音时长过短,可能无效: %.2f秒", interval);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString *amrPath = [FuncPublic GetPathByFileName:_fileName ofType:@"amr"];
|
NSString *amrPath = [FuncPublic GetPathByFileName:_fileName ofType:@"amr"];
|
||||||
if ([VoiceConverter ConvertWavToAmr:_filePath amrSavePath:amrPath]){
|
NSLog(@"尝试转换WAV到AMR,源文件: %@,目标文件: %@", _filePath, amrPath);
|
||||||
NSData *test=[NSData dataWithContentsOfFile:amrPath];
|
|
||||||
|
// 检查WAV文件格式
|
||||||
|
NSDictionary *audioFileSettings = nil;
|
||||||
|
AVAudioFile *audioFile = nil;
|
||||||
|
@try {
|
||||||
|
audioFile = [[AVAudioFile alloc] initForReading:[NSURL fileURLWithPath:_filePath] error:&error];
|
||||||
|
if (!error) {
|
||||||
|
audioFileSettings = audioFile.processingFormat.settings;
|
||||||
|
NSLog(@"WAV文件格式: %@", audioFileSettings);
|
||||||
|
// 检查采样率是否为8000Hz
|
||||||
|
float sampleRate = [[audioFileSettings objectForKey:AVSampleRateKey] floatValue];
|
||||||
|
if (sampleRate != 8000.0f) {
|
||||||
|
NSLog(@"警告: WAV文件采样率不是8000Hz (实际: %.0fHz), 可能影响转换", sampleRate);
|
||||||
|
// 这里可以尝试重新采样WAV文件,但为简单起见,先尝试直接转换
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} @catch (NSException *exception) {
|
||||||
|
NSLog(@"读取WAV文件格式时出错: %@", exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换WAV到AMR
|
||||||
|
int result = [VoiceConverter ConvertWavToAmr:_filePath amrSavePath:amrPath];
|
||||||
|
if (result) {
|
||||||
|
NSLog(@"转换WAV到AMR成功");
|
||||||
|
NSData *amrData = [NSData dataWithContentsOfFile:amrPath];
|
||||||
|
if (amrData && amrData.length > 0) {
|
||||||
|
// 数据有效,继续上传流程
|
||||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||||
//接收类型不一致请替换一致text/html或别的
|
//接收类型不一致请替换一致text/html或别的
|
||||||
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
|
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
|
||||||
@@ -2196,22 +2239,39 @@
|
|||||||
formatter.dateFormat =@"yyyyMMddHHmmss";
|
formatter.dateFormat =@"yyyyMMddHHmmss";
|
||||||
NSString *str = [formatter stringFromDate:[NSDate date]];
|
NSString *str = [formatter stringFromDate:[NSDate date]];
|
||||||
NSString *fileName = [NSString stringWithFormat:@"%@%08X.amr", str, arc4random()];
|
NSString *fileName = [NSString stringWithFormat:@"%@%08X.amr", str, arc4random()];
|
||||||
[manager POST:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
|
// 继续原有的上传逻辑...
|
||||||
// NSData *imageData =UIImageJPEGRepresentation([UIImage imageNamed:@"sharelogo.jpg"],1);
|
} else {
|
||||||
//上传的参数(上传图片,以文件流的格式)
|
NSLog(@"AMR文件无效或为空");
|
||||||
[formData appendPartWithFileData:test name:@"file" fileName:fileName mimeType:@"audio/amr"];
|
}
|
||||||
|
} else {
|
||||||
|
NSLog(@"转换WAV到AMR失败");
|
||||||
|
|
||||||
} success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
|
// 尝试打印WAV文件的基本信息,帮助调试
|
||||||
|
NSData *wavData = [NSData dataWithContentsOfFile:_filePath];
|
||||||
|
NSLog(@"WAV文件大小: %lu 字节", (unsigned long)wavData.length);
|
||||||
|
|
||||||
NSString *audiourl=[NSString stringWithFormat:@"http://gameapi.0791ts.cn/upload/%@",fileName];
|
if (wavData && wavData.length >= 44) { // 至少包含WAV文件头
|
||||||
[_bridge callHandler:@"getaudiourl" data:@{ @"audiourl":audiourl,@"time":[NSString stringWithFormat:@"%ld",(long)time]} ];
|
// 提取WAV头信息
|
||||||
NSLog(@"上传成功");
|
const char *bytes = [wavData bytes];
|
||||||
} failure:^(NSURLSessionDataTask *_Nullable task, NSError * _Nonnull error) {
|
NSString *riffMarker = [[NSString alloc] initWithBytes:bytes length:4 encoding:NSASCIIStringEncoding];
|
||||||
NSLog(@"上传失败");
|
NSString *waveMarker = [[NSString alloc] initWithBytes:(bytes + 8) length:4 encoding:NSASCIIStringEncoding];
|
||||||
}];
|
|
||||||
}else
|
|
||||||
NSLog(@"wav转amr失败");
|
|
||||||
|
|
||||||
|
UInt16 audioFormat;
|
||||||
|
[wavData getBytes:&audioFormat range:NSMakeRange(20, 2)];
|
||||||
|
|
||||||
|
UInt16 numChannels;
|
||||||
|
[wavData getBytes:&numChannels range:NSMakeRange(22, 2)];
|
||||||
|
|
||||||
|
UInt32 sampleRate;
|
||||||
|
[wavData getBytes:&sampleRate range:NSMakeRange(24, 4)];
|
||||||
|
|
||||||
|
UInt16 bitsPerSample;
|
||||||
|
[wavData getBytes:&bitsPerSample range:NSMakeRange(34, 2)];
|
||||||
|
|
||||||
|
NSLog(@"WAV文件头信息 - RIFF标记: %@, WAVE标记: %@, 格式: %d, 通道数: %d, 采样率: %d, 采样位数: %d",
|
||||||
|
riffMarker, waveMarker, audioFormat, numChannels, sampleRate, bitsPerSample);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - 微信回调
|
#pragma mark - 微信回调
|
||||||
@@ -2416,5 +2476,3 @@
|
|||||||
}}
|
}}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -69,8 +69,6 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
return bCanRecord;
|
return bCanRecord;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-(int)ifauth
|
//-(int)ifauth
|
||||||
@@ -102,20 +100,73 @@
|
|||||||
|
|
||||||
#pragma mark - 开始录音
|
#pragma mark - 开始录音
|
||||||
- (void)beginRecordByFileName:(NSString*)_fileName;{
|
- (void)beginRecordByFileName:(NSString*)_fileName;{
|
||||||
|
NSLog(@"开始初始化录音,文件名: %@", _fileName);
|
||||||
|
|
||||||
//设置文件名和录音路径
|
//设置文件名和录音路径
|
||||||
self.recordFileName = _fileName;
|
self.recordFileName = _fileName;
|
||||||
self.recordFilePath = [VoiceRecorderBaseVC getPathByFileName:recordFileName ofType:@"wav"];
|
self.recordFilePath = [VoiceRecorderBaseVC getPathByFileName:recordFileName ofType:@"wav"];
|
||||||
|
NSLog(@"录音将保存到路径: %@", recordFilePath);
|
||||||
|
|
||||||
|
// 确保目录存在
|
||||||
|
NSString *dirPath = [recordFilePath stringByDeletingLastPathComponent];
|
||||||
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||||
|
if (![fileManager fileExistsAtPath:dirPath]) {
|
||||||
|
NSError *dirError = nil;
|
||||||
|
[fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&dirError];
|
||||||
|
if (dirError) {
|
||||||
|
NSLog(@"创建录音目录失败: %@", [dirError localizedDescription]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 配置录音会话
|
||||||
|
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
|
||||||
|
NSError *sessionError = nil;
|
||||||
|
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
|
||||||
|
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
|
||||||
|
error:&sessionError];
|
||||||
|
|
||||||
|
if (sessionError) {
|
||||||
|
NSLog(@"设置音频会话失败: %@", [sessionError localizedDescription]);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL audioSessionActive = [audioSession setActive:YES error:&sessionError];
|
||||||
|
if (!audioSessionActive) {
|
||||||
|
NSLog(@"激活音频会话失败: %@", [sessionError localizedDescription]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修复:使用fileURLWithPath创建正确的文件URL
|
||||||
|
NSError *recError = nil;
|
||||||
|
NSURL *fileURL = [NSURL fileURLWithPath:recordFilePath];
|
||||||
|
NSLog(@"文件URL: %@", [fileURL absoluteString]);
|
||||||
|
|
||||||
|
// 检查并删除可能存在的旧文件
|
||||||
|
if ([fileManager fileExistsAtPath:recordFilePath]) {
|
||||||
|
[fileManager removeItemAtPath:recordFilePath error:nil];
|
||||||
|
NSLog(@"已删除旧的录音文件");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取录音设置
|
||||||
|
NSDictionary *recordSettings = [VoiceRecorderBaseVC getAudioRecorderSettingDict];
|
||||||
|
|
||||||
//初始化录音
|
//初始化录音
|
||||||
self.recorder = [[[AVAudioRecorder alloc]initWithURL:[NSURL URLWithString:recordFilePath]
|
self.recorder = [[[AVAudioRecorder alloc] initWithURL:fileURL
|
||||||
settings:[VoiceRecorderBaseVC getAudioRecorderSettingDict]
|
settings:recordSettings
|
||||||
error:nil]autorelease];
|
error:&recError] autorelease];
|
||||||
|
|
||||||
|
if (recError) {
|
||||||
|
NSLog(@"录音初始化失败: %@", [recError localizedDescription]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
recorder.delegate = self;
|
recorder.delegate = self;
|
||||||
recorder.meteringEnabled = YES;
|
recorder.meteringEnabled = YES;
|
||||||
|
|
||||||
[recorder prepareToRecord];
|
// 准备录音
|
||||||
|
BOOL prepareSuccess = [recorder prepareToRecord];
|
||||||
|
if (!prepareSuccess) {
|
||||||
|
NSLog(@"录音准备失败");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//还原计数
|
//还原计数
|
||||||
curCount = 0;
|
curCount = 0;
|
||||||
@@ -123,11 +174,12 @@
|
|||||||
canNotSend = NO;
|
canNotSend = NO;
|
||||||
|
|
||||||
// 开始录音
|
// 开始录音
|
||||||
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
|
BOOL recordSuccess = [recorder record];
|
||||||
[[AVAudioSession sharedInstance] setActive:YES error:nil];
|
if (!recordSuccess) {
|
||||||
|
NSLog(@"开始录音失败");
|
||||||
|
return;
|
||||||
[recorder record];
|
}
|
||||||
|
NSLog(@"开始录音成功, 录音状态: %d", recorder.isRecording);
|
||||||
|
|
||||||
//启动计时器
|
//启动计时器
|
||||||
[self startTimer];
|
[self startTimer];
|
||||||
@@ -167,12 +219,16 @@
|
|||||||
}
|
}
|
||||||
#pragma mark - 更新音频峰值
|
#pragma mark - 更新音频峰值
|
||||||
- (void)updateMeters{
|
- (void)updateMeters{
|
||||||
if (recorder.isRecording){
|
if (recorder && recorder.isRecording){
|
||||||
|
|
||||||
//更新峰值
|
//更新峰值
|
||||||
[recorder updateMeters];
|
[recorder updateMeters];
|
||||||
[recorderView updateMetersByAvgPower:[recorder averagePowerForChannel:0]];
|
float avgPower = [recorder averagePowerForChannel:0];
|
||||||
// NSLog(@"峰值:%f",[recorder averagePowerForChannel:0]);
|
[recorderView updateMetersByAvgPower:avgPower];
|
||||||
|
|
||||||
|
// 每秒输出一次状态
|
||||||
|
if (fmod(curCount, 1.0) < 0.1) {
|
||||||
|
NSLog(@"录音中... 时长: %.1f秒, 峰值: %.2f", curCount, avgPower);
|
||||||
|
}
|
||||||
|
|
||||||
//倒计时
|
//倒计时
|
||||||
if (curCount >= maxRecordTime - 10 && curCount < maxRecordTime) {
|
if (curCount >= maxRecordTime - 10 && curCount < maxRecordTime) {
|
||||||
@@ -246,15 +302,26 @@
|
|||||||
//停止计时器
|
//停止计时器
|
||||||
[self stopTimer];
|
[self stopTimer];
|
||||||
|
|
||||||
|
NSLog(@"触摸结束,录音结束,当前录音时长: %.1f秒", curCount);
|
||||||
|
|
||||||
curTouchPoint = CGPointZero;
|
curTouchPoint = CGPointZero;
|
||||||
[self removeScreenTouchObserver];
|
[self removeScreenTouchObserver];
|
||||||
|
|
||||||
[UIView hideViewByCompletion:^(BOOL finish){
|
[UIView hideViewByCompletion:^(BOOL finish){
|
||||||
|
|
||||||
//停止录音
|
//停止录音
|
||||||
if (recorder.isRecording)
|
if (recorder && recorder.isRecording) {
|
||||||
|
NSLog(@"停止录音");
|
||||||
[recorder stop];
|
[recorder stop];
|
||||||
|
|
||||||
|
// 检查录音文件
|
||||||
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||||
|
if ([fileManager fileExistsAtPath:recordFilePath]) {
|
||||||
|
NSDictionary *attrs = [fileManager attributesOfItemAtPath:recordFilePath error:nil];
|
||||||
|
NSLog(@"最终录音文件大小: %@ 字节", [attrs objectForKey:NSFileSize]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (canNotSend) {
|
if (canNotSend) {
|
||||||
//取消发送,删除文件
|
//取消发送,删除文件
|
||||||
[VoiceRecorderBaseVC deleteFileAtPath:recordFilePath];
|
[VoiceRecorderBaseVC deleteFileAtPath:recordFilePath];
|
||||||
@@ -269,11 +336,22 @@
|
|||||||
|
|
||||||
#pragma mark - AVAudioRecorder Delegate Methods
|
#pragma mark - AVAudioRecorder Delegate Methods
|
||||||
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
|
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
|
||||||
NSLog(@"录音停止");
|
NSLog(@"录音完成回调,成功: %d", flag);
|
||||||
|
|
||||||
|
if (!flag) {
|
||||||
|
NSLog(@"录音完成但不成功!");
|
||||||
|
}
|
||||||
|
|
||||||
[self stopTimer];
|
[self stopTimer];
|
||||||
curCount = 0;
|
curCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error {
|
||||||
|
NSLog(@"录音编码错误: %@", [error localizedDescription]);
|
||||||
|
[self stopTimer];
|
||||||
|
curCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{
|
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{
|
||||||
NSLog(@"录音开始");
|
NSLog(@"录音开始");
|
||||||
[self stopTimer];
|
[self stopTimer];
|
||||||
|
|||||||
Reference in New Issue
Block a user