修复录音文件长度为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,22 +2168,65 @@
|
|||||||
- (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];
|
|
||||||
NSTimeInterval interval= play.duration;
|
|
||||||
NSInteger time = round(interval);
|
|
||||||
//4.播放
|
|
||||||
if (play == nil)
|
|
||||||
{
|
|
||||||
NSLog(@"ERror creating player: %@", [play description]);
|
|
||||||
}else{
|
|
||||||
|
|
||||||
|
// 检查文件是否存在
|
||||||
|
if (![[NSFileManager defaultManager] fileExistsAtPath:_filePath]) {
|
||||||
|
NSLog(@"录音文件不存在: %@", _filePath);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
NSString *amrPath = [FuncPublic GetPathByFileName: _fileName ofType:@"amr"];
|
|
||||||
if ([VoiceConverter ConvertWavToAmr:_filePath amrSavePath:amrPath]){
|
// 使用fileURLWithPath而不是URLWithString,因为是本地文件路径
|
||||||
NSData *test=[NSData dataWithContentsOfFile:amrPath];
|
NSError *error = nil;
|
||||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
AVAudioPlayer *play = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:_filePath] error:&error];
|
||||||
//接收类型不一致请替换一致text/html或别的
|
|
||||||
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
|
if (error || play == nil) {
|
||||||
|
NSLog(@"创建AVAudioPlayer时出错: %@", [error localizedDescription]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取音频时长
|
||||||
|
NSTimeInterval interval = play.duration;
|
||||||
|
NSInteger time = round(interval);
|
||||||
|
NSLog(@"录音时长: %ld秒", (long)time);
|
||||||
|
|
||||||
|
// 如果录音时长太短(小于1秒),可能是无效录音
|
||||||
|
if (interval < 1.0) {
|
||||||
|
NSLog(@"录音时长过短,可能无效: %.2f秒", interval);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSString *amrPath = [FuncPublic GetPathByFileName:_fileName ofType:@"amr"];
|
||||||
|
NSLog(@"尝试转换WAV到AMR,源文件: %@,目标文件: %@", _filePath, 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];
|
||||||
|
//接收类型不一致请替换一致text/html或别的
|
||||||
|
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
|
||||||
@"text/html",
|
@"text/html",
|
||||||
@"image/jpeg",
|
@"image/jpeg",
|
||||||
@"image/png",
|
@"image/png",
|
||||||
@@ -2191,27 +2234,44 @@
|
|||||||
@"text/json",
|
@"text/json",
|
||||||
@"audio/amr",
|
@"audio/amr",
|
||||||
nil];
|
nil];
|
||||||
NSString *string=@"http://gameapi.0791ts.cn/api/UpLoad/PostFile";
|
NSString *string=@"http://gameapi.0791ts.cn/api/UpLoad/PostFile";
|
||||||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||||||
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 - 微信回调
|
||||||
@@ -2288,133 +2348,131 @@
|
|||||||
|
|
||||||
[_bridge callHandler:@"sharelogin" data:@{ @"openid":[obj_returntwo objectForKey:@"openid"],@"headimgurl":[obj_returntwo objectForKey:@"headimgurl"],@"nickname":nickname ,@"sex":[obj_returntwo objectForKey:@"sex"] ,@"city":[FuncPublic danbian:[obj_returntwo objectForKey:@"city"]] ,@"Province":[FuncPublic danbian:[obj_returntwo objectForKey:@"province"]] ,@"unionid":[obj_returntwo objectForKey:@"unionid"] } ];
|
[_bridge callHandler:@"sharelogin" data:@{ @"openid":[obj_returntwo objectForKey:@"openid"],@"headimgurl":[obj_returntwo objectForKey:@"headimgurl"],@"nickname":nickname ,@"sex":[obj_returntwo objectForKey:@"sex"] ,@"city":[FuncPublic danbian:[obj_returntwo objectForKey:@"city"]] ,@"Province":[FuncPublic danbian:[obj_returntwo objectForKey:@"province"]] ,@"unionid":[obj_returntwo objectForKey:@"unionid"] } ];
|
||||||
|
|
||||||
}
|
}
|
||||||
#pragma mark - 手机震动
|
#pragma mark - 手机震动
|
||||||
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
|
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
|
||||||
{
|
|
||||||
if (motion == UIEventSubtypeMotionShake)
|
|
||||||
{
|
{
|
||||||
if(canshake)
|
if (motion == UIEventSubtypeMotionShake)
|
||||||
{
|
{
|
||||||
[_bridge callHandler:@"shakeEnd" data:nil];
|
if(canshake)
|
||||||
if(canvoice)
|
|
||||||
{
|
{
|
||||||
NSURL *url_=[[NSBundle mainBundle]URLForResource:@"shake_sound_male" withExtension:@"mp3"];
|
[_bridge callHandler:@"shakeEnd" data:nil];
|
||||||
AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url_ error:Nil];
|
if(canvoice)
|
||||||
[audioPlayer prepareToPlay];
|
{
|
||||||
[audioPlayer play];
|
NSURL *url_=[[NSBundle mainBundle]URLForResource:@"shake_sound_male" withExtension:@"mp3"];
|
||||||
|
AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url_ error:Nil];
|
||||||
|
[audioPlayer prepareToPlay];
|
||||||
|
[audioPlayer play];
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
地图定位
|
||||||
|
*/
|
||||||
|
- (void)configLocationManager
|
||||||
|
{
|
||||||
|
self.locationManager = [[AMapLocationManager alloc] init];
|
||||||
|
[self.locationManager setDelegate:self];
|
||||||
|
//设置期望定位精度
|
||||||
|
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
|
||||||
|
//设置不允许系统暂停定位
|
||||||
|
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
|
||||||
|
//设置允许在后台定位
|
||||||
|
// [self.locationManager setAllowsBackgroundLocationUpdates:YES];
|
||||||
|
//设置允许连续定位逆地理
|
||||||
|
[self.locationManager setLocatingWithReGeocode:YES];
|
||||||
|
//设置定位超时时间
|
||||||
|
[self.locationManager setLocationTimeout:DefaultLocationTimeout];
|
||||||
|
//设置逆地理超时时间
|
||||||
|
[self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
- (void)cleanUpAction
|
||||||
/*
|
|
||||||
地图定位
|
|
||||||
*/
|
|
||||||
- (void)configLocationManager
|
|
||||||
{
|
|
||||||
self.locationManager = [[AMapLocationManager alloc] init];
|
|
||||||
[self.locationManager setDelegate:self];
|
|
||||||
//设置期望定位精度
|
|
||||||
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
|
|
||||||
//设置不允许系统暂停定位
|
|
||||||
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
|
|
||||||
//设置允许在后台定位
|
|
||||||
// [self.locationManager setAllowsBackgroundLocationUpdates:YES];
|
|
||||||
//设置允许连续定位逆地理
|
|
||||||
[self.locationManager setLocatingWithReGeocode:YES];
|
|
||||||
//设置定位超时时间
|
|
||||||
[self.locationManager setLocationTimeout:DefaultLocationTimeout];
|
|
||||||
//设置逆地理超时时间
|
|
||||||
[self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)cleanUpAction
|
|
||||||
{
|
|
||||||
//停止定位
|
|
||||||
[self.locationManager stopUpdatingLocation];
|
|
||||||
[self.locationManager setDelegate:nil];
|
|
||||||
}
|
|
||||||
- (void)reGeocodeAction
|
|
||||||
{
|
|
||||||
//进行单次带逆地理定位请求
|
|
||||||
[self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)locAction
|
|
||||||
{
|
|
||||||
//进行单次定位请求
|
|
||||||
[self.locationManager requestLocationWithReGeocode:NO completionBlock:self.completionBlock];
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma mark - Initialization
|
|
||||||
|
|
||||||
- (void)initCompleteBlock
|
|
||||||
{
|
|
||||||
//__weak RootVC *weakSelf = self;
|
|
||||||
self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
|
|
||||||
{
|
{
|
||||||
if (error)
|
//停止定位
|
||||||
|
[self.locationManager stopUpdatingLocation];
|
||||||
|
[self.locationManager setDelegate:nil];
|
||||||
|
}
|
||||||
|
- (void)reGeocodeAction
|
||||||
|
{
|
||||||
|
//进行单次带逆地理定位请求
|
||||||
|
[self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)locAction
|
||||||
|
{
|
||||||
|
//进行单次定位请求
|
||||||
|
[self.locationManager requestLocationWithReGeocode:NO completionBlock:self.completionBlock];
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - Initialization
|
||||||
|
|
||||||
|
- (void)initCompleteBlock
|
||||||
|
{
|
||||||
|
//__weak RootVC *weakSelf = self;
|
||||||
|
self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
|
||||||
{
|
{
|
||||||
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
|
if (error)
|
||||||
|
|
||||||
//如果为定位失败的error,则不进行后续操作
|
|
||||||
if (error.code == AMapLocationErrorLocateFailed)
|
|
||||||
{
|
{
|
||||||
return;
|
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//得到定位信息
|
//如果为定位失败的error,则不进行后续操作
|
||||||
if (location)
|
if (error.code == AMapLocationErrorLocateFailed)
|
||||||
{
|
|
||||||
if (regeocode)
|
|
||||||
{
|
|
||||||
NSLog(@"%@",[NSString stringWithFormat:@"%@ \n %@-%@-%.2fm", regeocode.formattedAddress,regeocode.citycode, regeocode.adcode, location.horizontalAccuracy]);
|
|
||||||
|
|
||||||
|
|
||||||
if(regeocode.formattedAddress!=nil)
|
|
||||||
{
|
{
|
||||||
@try{
|
return;
|
||||||
[_bridge callHandler:@"getlocationinfo" data:@{@"address":regeocode.formattedAddress,@"city":regeocode.city,@"cityCode":regeocode.citycode,@"country":regeocode.country,@"district":regeocode.district,@"latitude":[NSString stringWithFormat:@"%f",location.coordinate.latitude],@"longitude":[NSString stringWithFormat:@"%f",location.coordinate.longitude],@"province":regeocode.province,@"street":regeocode.street} ];
|
|
||||||
} @catch (NSException * e) {
|
|
||||||
NSLog(@"Exception: %@", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//得到定位信息
|
||||||
}else
|
if (location)
|
||||||
{
|
{
|
||||||
NSLog(@"%@",[NSString stringWithFormat:@"lat:%f;lon:%f \n accuracy:%.2fm", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy]);
|
if (regeocode)
|
||||||
|
{
|
||||||
|
NSLog(@"%@",[NSString stringWithFormat:@"%@ \n %@-%@-%.2fm", regeocode.formattedAddress,regeocode.citycode, regeocode.adcode, location.horizontalAccuracy]);
|
||||||
|
|
||||||
|
|
||||||
|
if(regeocode.formattedAddress!=nil)
|
||||||
|
{
|
||||||
|
@try{
|
||||||
|
[_bridge callHandler:@"getlocationinfo" data:@{@"address":regeocode.formattedAddress,@"city":regeocode.city,@"cityCode":regeocode.citycode,@"country":regeocode.country,@"district":regeocode.district,@"latitude":[NSString stringWithFormat:@"%f",location.coordinate.latitude],@"longitude":[NSString stringWithFormat:@"%f",location.coordinate.longitude],@"province":regeocode.province,@"street":regeocode.street} ];
|
||||||
|
} @catch (NSException * e) {
|
||||||
|
NSLog(@"Exception: %@", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
NSLog(@"%@",[NSString stringWithFormat:@"lat:%f;lon:%f \n accuracy:%.2fm", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#pragma mark - AMapLocationManager Delegate
|
#pragma mark - AMapLocationManager Delegate
|
||||||
|
|
||||||
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
|
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
|
||||||
{
|
{
|
||||||
NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
|
NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
|
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
|
||||||
{
|
{
|
||||||
NSLog(@"location:{lat:%f; lon:%f; accuracy:%f; reGeocode:%@}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy, reGeocode.formattedAddress);
|
NSLog(@"location:{lat:%f; lon:%f; accuracy:%f; reGeocode:%@}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy, reGeocode.formattedAddress);
|
||||||
if (reGeocode!=nil) {
|
if (reGeocode!=nil) {
|
||||||
if(reGeocode.formattedAddress!=nil)
|
if(reGeocode.formattedAddress!=nil)
|
||||||
{
|
{
|
||||||
@try{
|
@try{
|
||||||
[_bridge callHandler:@"getlocationinfo" data:@{@"address":reGeocode.formattedAddress,@"city":reGeocode.city,@"cityCode":reGeocode.citycode,@"country":reGeocode.country,@"district":reGeocode.district,@"latitude":[NSString stringWithFormat:@"%f",location.coordinate.latitude],@"longitude":[NSString stringWithFormat:@"%f",location.coordinate.longitude],@"province":reGeocode.province,@"street":reGeocode.street} ];
|
[_bridge callHandler:@"getlocationinfo" data:@{@"address":reGeocode.formattedAddress,@"city":reGeocode.city,@"cityCode":reGeocode.citycode,@"country":reGeocode.country,@"district":reGeocode.district,@"latitude":[NSString stringWithFormat:@"%f",location.coordinate.latitude],@"longitude":[NSString stringWithFormat:@"%f",location.coordinate.longitude],@"province":reGeocode.province,@"street":reGeocode.street} ];
|
||||||
} @catch (NSException * e) {
|
} @catch (NSException * e) {
|
||||||
NSLog(@"Exception: %@", e);
|
NSLog(@"Exception: %@", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}}
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
|
}}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|||||||
@@ -69,8 +69,6 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
return bCanRecord;
|
return bCanRecord;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-(int)ifauth
|
//-(int)ifauth
|
||||||
@@ -102,32 +100,86 @@
|
|||||||
|
|
||||||
#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;
|
||||||
//还原发送
|
//还原发送
|
||||||
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