YOUPAILZBigCastSoundRecordView.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. //
  2. // YOUPAILZBigCastSoundRecordView.m
  3. // VQU
  4. //
  5. // Created by CY on 2021/7/9.
  6. // Copyright © 2021 leo. All rights reserved.
  7. //
  8. #import "YOUPAILZBigCastSoundRecordView.h"
  9. #import "NSURL+TransferFileFormat.h"
  10. #import "LZCircleView.h"
  11. @interface YOUPAILZBigCastSoundRecordView () <AVAudioPlayerDelegate,AVAudioRecorderDelegate>
  12. @property (nonatomic, weak)UILabel *youpaiptimeL;
  13. @property (nonatomic, weak)LZCircleView *youpaipcircleView;
  14. /// 录制
  15. @property (nonatomic, strong)AVAudioRecorder *youpaiprecorder;
  16. @property (nonatomic, strong)NSURL *youpaiprecordAudioUrl;
  17. /// 播放
  18. @property (nonatomic, strong)AVAudioPlayer *youpaipaudioPlayer;
  19. @property (nonatomic,strong)NSTimer *youpaiptimer;
  20. @property (nonatomic,assign)NSInteger youpaipaudioTime;
  21. @property (nonatomic,assign)NSInteger youpaipplayTime;
  22. @property (nonatomic,assign) LZSoundRecordState youpaipstate;
  23. @end
  24. @implementation YOUPAILZBigCastSoundRecordView
  25. - (instancetype)initWithFrame:(CGRect)frame{
  26. if (self = [super initWithFrame:frame]) {
  27. [self youpaifinitValue];
  28. [self youpaifsetupUI];
  29. self.youpaipstate = LZSoundRecordStateNone;
  30. }
  31. return self;
  32. }
  33. - (void)youpaifinitValue{
  34. self.youpaipminTimer = 5;
  35. self.youpaipmaxTimer = 60;
  36. self.youpaiptype = LZAudioTypeWithPM3;
  37. }
  38. - (void)youpaifsetupUI{
  39. LZCircleView *circleView = [[LZCircleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 74.0f, 74.0f)];
  40. circleView.progress = 0.0f;
  41. circleView.progerssColor = HexColorFromRGB(0xFDD45E);
  42. circleView.progerssBackgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.13f];
  43. circleView.progerWidth = 2.5f;
  44. circleView.backgroundProgerWidth = 1.0f;
  45. circleView.hidden = YES;
  46. [self addSubview:circleView];
  47. self.youpaipcircleView = circleView;
  48. [circleView mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.center.equalTo(self);
  50. make.size.mas_offset(CGSizeMake(74.0f, 74.0f));
  51. }];
  52. UIButton *centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  53. [centerBtn addTarget:self action:@selector(youpaifcenterBtnClick) forControlEvents:UIControlEventTouchUpInside];
  54. [self addSubview:centerBtn];
  55. self.youpaipcenterBtn = centerBtn;
  56. [centerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.center.equalTo(self);
  58. make.size.mas_offset(CGSizeMake(65.0f, 65.0f));
  59. }];
  60. UILabel *timeL = [[UILabel alloc] init];
  61. timeL.font = LCFont12;
  62. timeL.textColor = LZ273145Color;
  63. timeL.text = @"00:00";
  64. [self addSubview:timeL];
  65. self.youpaiptimeL = timeL;
  66. [timeL mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.centerX.equalTo(centerBtn.mas_centerX);
  68. make.bottom.equalTo(centerBtn.mas_top).offset(-15.0f);
  69. }];
  70. UILabel *descL = [[UILabel alloc] init];
  71. descL.font = LCFont12;
  72. descL.textColor = LZA3AABEColor;
  73. descL.text = [NSString stringWithFormat:@"点击录音,至少%@s",@(self.youpaipminTimer)];
  74. [self addSubview:descL];
  75. self.youpaipdescL = descL;
  76. [descL mas_makeConstraints:^(MASConstraintMaker *make) {
  77. make.centerX.equalTo(centerBtn.mas_centerX);
  78. make.top.equalTo(centerBtn.mas_bottom).offset(15.0f);
  79. }];
  80. UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  81. leftBtn.frame = CGRectMake(0.0f, 0.0f, 50.0f, 76.0f);
  82. [leftBtn addTarget:self action:@selector(youpaifleftBtnClick) forControlEvents:UIControlEventTouchUpInside];
  83. leftBtn.titleLabel.font = LCFont12;
  84. [leftBtn setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
  85. [self addSubview:leftBtn];
  86. self.youpaipleftBtn = leftBtn;
  87. [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.top.equalTo(centerBtn.mas_top).offset(4.0f);
  89. make.size.mas_offset(CGSizeMake(50.0f, 76.0f));
  90. make.right.equalTo(centerBtn.mas_left).offset(-40.0f);
  91. }];
  92. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  93. rightBtn.frame = CGRectMake(0.0f, 0.0f, 50.0f, 76.0f);
  94. [rightBtn addTarget:self action:@selector(youpaifrightBtnClick) forControlEvents:UIControlEventTouchUpInside];
  95. rightBtn.titleLabel.font = LCFont12;
  96. [rightBtn setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
  97. [self addSubview:rightBtn];
  98. self.youpaiprightBtn = rightBtn;
  99. [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  100. make.top.equalTo(centerBtn.mas_top).offset(4.0f);
  101. make.size.mas_offset(CGSizeMake(50.0f, 76.0f));
  102. make.left.equalTo(centerBtn.mas_right).offset(40.0f);
  103. }];
  104. }
  105. //将按钮设置为图片在上,文字在下
  106. -(void)youpaifinitButton:(UIButton*)btn{
  107. float spacing = 10;//图片和文字的上下间距
  108. CGSize imageSize = CGSizeMake(50.0f, 50.0f);
  109. CGSize titleSize = btn.titleLabel.frame.size;
  110. CGSize textSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : btn.titleLabel.font}];
  111. CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
  112. if (titleSize.width + 0.5 < frameSize.width) {
  113. titleSize.width = frameSize.width;
  114. }
  115. CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
  116. btn.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
  117. btn.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
  118. }
  119. - (void)youpaifleftBtnClick{
  120. [self youpaifstopTimer];
  121. self.youpaipstate = LZSoundRecordStateNone;
  122. self.youpaiprecordAudioUrl = nil;
  123. self.youpaiprecorder = nil;
  124. // 停止播放
  125. [self.youpaipaudioPlayer stop];
  126. self.youpaipaudioPlayer = nil;
  127. }
  128. - (void)youpaifcenterBtnClick{
  129. if(self.youpaipstate == LZSoundRecordStateNone){
  130. self.youpaipstate = LZSoundRecordStateStart;
  131. //创建录音通道
  132. [self setAudioSession];
  133. // 开始录音
  134. [self.youpaiprecorder record];
  135. // 检查权限
  136. [self youpaifsetupAuthority];
  137. self.youpaipaudioTime = 0;
  138. [self youpaifstartTimer];
  139. }else if(self.youpaipstate == LZSoundRecordStateFinish){
  140. self.youpaipstate = LZSoundRecordStatePlay;
  141. // UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
  142. // AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
  143. UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
  144. AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
  145. AVAudioSession * session = [AVAudioSession sharedInstance];
  146. [session setActive:YES error:nil];
  147. [session setCategory:AVAudioSessionCategoryPlayback error:nil];
  148. NSError *error;
  149. self.youpaipaudioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:self.youpaiprecordAudioUrl error:&error];
  150. if (error) {
  151. NSLog(@"%@",error);
  152. }
  153. self.youpaipaudioPlayer.delegate = self;
  154. // 播放
  155. [self.youpaipaudioPlayer play];
  156. self.youpaipplayTime = self.youpaipaudioTime;
  157. [self youpaifstartTimer];
  158. }else if (self.youpaipstate == LZSoundRecordStatePlay){
  159. self.youpaipstate = LZSoundRecordStateFinish;
  160. // 停止播放
  161. [self.youpaipaudioPlayer stop];
  162. self.youpaipaudioPlayer = nil;
  163. [self youpaifstopTimer];
  164. }else if (self.youpaipstate == LZSoundRecordStateStart){
  165. self.youpaipstate = LZSoundRecordStateFinish;
  166. // 停止录制
  167. [self.youpaiprecorder stop];
  168. self.youpaiprecorder = nil;
  169. [self youpaifstopTimer];
  170. if (self.youpaipaudioTime < self.youpaipminTimer) {
  171. [ZCHUDHelper showTitle:[NSString stringWithFormat:@"录音不可少于%@秒",@(self.youpaipminTimer)]];
  172. [self youpaifleftBtnClick];
  173. }
  174. }
  175. }
  176. - (void)youpaifrightBtnClick{
  177. // self.state = LZSoundRecordStateSubmit;
  178. [self youpaifstopTimer];
  179. // 停止播放
  180. [self.youpaipaudioPlayer stop];
  181. self.youpaipaudioPlayer = nil;
  182. // self.gameModel.audioUrl = [self.recordAudioUrl transformCAFToMP3];
  183. // self.gameModel.audioDuration = self.audioTime;
  184. if (self.soundRecordFinishBlock != nil) {
  185. switch (self.youpaiptype) {
  186. case LZAudioTypeWithPM3:
  187. self.soundRecordFinishBlock([self.youpaiprecordAudioUrl transformCAFToMP3], self.youpaipaudioTime);
  188. break;
  189. case LZAudioTypeWithAAC:{
  190. self.soundRecordFinishBlock(self.youpaiprecordAudioUrl , self.youpaipaudioTime);
  191. }
  192. break;
  193. default:
  194. break;
  195. }
  196. }
  197. self.youpaiprecorder = nil;
  198. self.youpaiprecordAudioUrl = nil;
  199. }
  200. - (void)setYoupaipstate:(LZSoundRecordState)state{
  201. _youpaipstate = state;
  202. NSString *time = @"";
  203. NSString *desc = @"";
  204. self.youpaipleftBtn.hidden = YES;
  205. self.youpaiprightBtn.hidden = YES;
  206. self.youpaipcircleView.hidden = YES;
  207. self.youpaipcircleView.progress = 0.0f;
  208. UIImage *centerImg = nil;
  209. UIImage *leftImg = nil;
  210. UIImage *rightImg = nil;
  211. switch (state) {
  212. case LZSoundRecordStateNone:{
  213. time = @"0s";
  214. desc = [NSString stringWithFormat:@"点击录音,至少%@s",@(self.youpaipminTimer)];;
  215. centerImg = [UIImage imageNamed:@"vqu_images_big_cast_audio_none"];
  216. }
  217. break;
  218. case LZSoundRecordStateStart:{
  219. time = @"0s";
  220. desc = @"点击结束录音";
  221. centerImg = [UIImage imageNamed:@"vqu_images_big_cast_audio_start"];
  222. self.youpaipcircleView.hidden = NO;
  223. [self youpaifprogressAnimation];
  224. }
  225. break;
  226. case LZSoundRecordStateFinish:{
  227. time = [NSString stringWithFormat:@"%@s",@(self.youpaipaudioTime)];
  228. desc = @"点击试听";
  229. centerImg = [UIImage imageNamed:@"vqu_images_big_cast_audio_finish"];
  230. leftImg = [UIImage imageNamed:@"vqu_images_big_case_audio_re_record"];
  231. rightImg = [UIImage imageNamed:@"vqu_images_big_cast_audio_submit"];
  232. self.youpaipleftBtn.hidden = NO;
  233. self.youpaiprightBtn.hidden = NO;
  234. [self.youpaipleftBtn setTitle:@"重录" forState:UIControlStateNormal];
  235. [self.youpaiprightBtn setTitle:@"确定" forState:UIControlStateNormal];
  236. }
  237. break;
  238. case LZSoundRecordStatePlay:{
  239. time = [NSString stringWithFormat:@"%@s",@(self.youpaipaudioTime)];;
  240. desc = @"播放中";
  241. centerImg = [UIImage imageNamed:@"vqu_images_big_cast_audio_play1"];
  242. leftImg = [UIImage imageNamed:@"vqu_images_big_case_audio_re_record"];
  243. rightImg = [UIImage imageNamed:@"vqu_images_big_cast_audio_submit"];
  244. self.youpaipleftBtn.hidden = NO;
  245. self.youpaiprightBtn.hidden = NO;
  246. [self.youpaipleftBtn setTitle:@"重录" forState:UIControlStateNormal];
  247. [self.youpaiprightBtn setTitle:@"确定" forState:UIControlStateNormal];
  248. }
  249. break;
  250. case LZSoundRecordStateSubmit:{
  251. // time = [self timeFormatted:self.audioTime];
  252. // desc = @"已录制";
  253. // centerImg = [UIImage imageNamed:@"vqu_images_game_audio_success"];
  254. }
  255. break;
  256. default:
  257. break;
  258. }
  259. self.youpaiptimeL.text = time;
  260. self.youpaipdescL.text = desc;
  261. [self.youpaipleftBtn setImage:leftImg forState:UIControlStateNormal];
  262. [self.youpaiprightBtn setImage:rightImg forState:UIControlStateNormal];
  263. [self.youpaipcenterBtn setBackgroundImage:centerImg forState:UIControlStateNormal];
  264. [self youpaifinitButton:self.youpaipleftBtn];
  265. [self youpaifinitButton:self.youpaiprightBtn];
  266. }
  267. #pragma mark - AVAudioPlayerDelegate
  268. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
  269. self.youpaipstate = LZSoundRecordStateFinish;
  270. // 停止播放
  271. [self.youpaipaudioPlayer stop];
  272. self.youpaipaudioPlayer = nil;
  273. [self youpaifstopTimer];
  274. }
  275. - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
  276. self.youpaipstate = LZSoundRecordStateFinish;
  277. // 停止播放
  278. [self.youpaipaudioPlayer stop];
  279. self.youpaipaudioPlayer = nil;
  280. [self youpaifstopTimer];
  281. }
  282. - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
  283. self.youpaipstate = LZSoundRecordStateFinish;
  284. // 停止播放
  285. [self.youpaipaudioPlayer stop];
  286. self.youpaipaudioPlayer = nil;
  287. [self youpaifstopTimer];
  288. }
  289. #pragma mark - AVAudioRecorderDelegate
  290. - (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError * _Nullable)error{
  291. self.youpaipstate = LZSoundRecordStateFinish;
  292. // 停止录制
  293. [self.youpaiprecorder stop];
  294. self.youpaiprecorder = nil;
  295. [self youpaifstopTimer];
  296. }
  297. - (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{
  298. self.youpaipstate = LZSoundRecordStateFinish;
  299. // 停止录制
  300. [self.youpaiprecorder stop];
  301. self.youpaiprecorder = nil;
  302. [self youpaifstopTimer];
  303. }
  304. // 开始计时
  305. - (void)youpaifstartTimer{
  306. self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(youpaiftimerAction) userInfo:nil repeats:YES];
  307. [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes];
  308. }
  309. // 结束计时
  310. - (void)youpaifstopTimer{
  311. [self.youpaiptimer invalidate];
  312. self.youpaiptimer = nil;
  313. self.youpaiptimeL.text = [NSString stringWithFormat:@"%@s",@(self.youpaipaudioTime)];
  314. }
  315. - (void)youpaifprogressAnimation{
  316. __block CGFloat timeout = 0.0f;
  317. dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
  318. dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  319. dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 0.025f * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
  320. //3.设置定时器要执行的任务
  321. dispatch_source_set_event_handler(_timer, ^{
  322. if(timeout >= 1.0f){
  323. dispatch_source_cancel(_timer);
  324. dispatch_async(dispatch_get_main_queue(), ^{
  325. });
  326. }else{
  327. timeout += 0.025f;
  328. CGFloat currentTime = (CGFloat)self.youpaipaudioTime + timeout;
  329. dispatch_async(dispatch_get_main_queue(), ^{
  330. self.youpaipcircleView.progress = currentTime / (CGFloat)self.youpaipmaxTimer;
  331. });
  332. }
  333. });
  334. dispatch_resume(_timer);
  335. }
  336. - (void)youpaiftimerAction{
  337. if (self.youpaipstate == LZSoundRecordStateStart) {
  338. self.youpaipaudioTime ++;
  339. self.youpaiptimeL.text = [NSString stringWithFormat:@"%@s",@(self.youpaipaudioTime)];
  340. [self youpaifprogressAnimation];
  341. if (self.youpaipaudioTime >= self.youpaipmaxTimer) {
  342. self.youpaipstate = LZSoundRecordStateFinish;
  343. // 停止录制
  344. [self.youpaiprecorder stop];
  345. self.youpaiprecorder = nil;
  346. [self youpaifstopTimer];
  347. }
  348. }else if (self.youpaipstate == LZSoundRecordStatePlay){
  349. self.youpaipplayTime --;
  350. self.youpaiptimeL.text = [NSString stringWithFormat:@"%@s",@(self.youpaipplayTime)];
  351. }
  352. }
  353. //- (NSString *)timeFormatted:(NSInteger)totalSeconds{
  354. // NSInteger seconds = totalSeconds % 60;
  355. // NSInteger minutes = (totalSeconds / 60);
  356. // return [NSString stringWithFormat:@"%02ld:%02ld", minutes, seconds];
  357. //}
  358. /// 视频本地url路径
  359. - (NSURL *)youpaiprecordAudioUrl{
  360. if (!_youpaiprecordAudioUrl) {
  361. NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
  362. NSString *fileName;
  363. if (self.youpaiptype == LZAudioTypeWithAAC) {
  364. fileName = @"audioVerify.aac";
  365. }else{
  366. fileName = @"audioVerify.caf";
  367. }
  368. _youpaiprecordAudioUrl = [NSURL fileURLWithPath:[path stringByAppendingPathComponent:fileName]];
  369. if ([[NSFileManager defaultManager] fileExistsAtPath:_youpaiprecordAudioUrl.resourceSpecifier]){
  370. [[NSFileManager defaultManager] removeItemAtPath:_youpaiprecordAudioUrl.resourceSpecifier error:nil];
  371. }
  372. }
  373. return _youpaiprecordAudioUrl;
  374. }
  375. - (AVAudioRecorder *)youpaiprecorder{
  376. if (_youpaiprecorder == nil) {
  377. //创建录音机
  378. NSError *error=nil;
  379. _youpaiprecorder = [[AVAudioRecorder alloc] initWithURL:self.youpaiprecordAudioUrl settings:[self getAudioSetting] error:&error];
  380. _youpaiprecorder.delegate = self;
  381. if (error) {
  382. NSLog(@"创建录音机对象时发生错误,错误信息:%@",error.localizedDescription);
  383. return nil;
  384. }
  385. }
  386. return _youpaiprecorder;
  387. }
  388. /**
  389. * 取得录音文件设置
  390. *
  391. * @return 录音设置
  392. */
  393. -(NSDictionary *)getAudioSetting{
  394. if (self.youpaiptype == LZAudioTypeWithAAC) {
  395. NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init] ;
  396. //设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
  397. [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
  398. //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
  399. [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
  400. //录音通道数 1 或 2
  401. [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
  402. //线性采样位数 8、16、24、32
  403. [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
  404. //录音的质量
  405. [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityMedium] forKey:AVEncoderAudioQualityKey];
  406. return recordSetting.copy;
  407. }else{
  408. NSDictionary *recordSetting = @{
  409. AVFormatIDKey : @(kAudioFormatLinearPCM),
  410. AVSampleRateKey : @(44100),
  411. AVNumberOfChannelsKey : @(2),
  412. AVLinearPCMBitDepthKey : @(16),
  413. AVLinearPCMIsNonInterleaved : @NO,
  414. AVLinearPCMIsFloatKey : @NO,
  415. AVLinearPCMIsBigEndianKey : @NO,
  416. };
  417. return recordSetting;
  418. }
  419. }
  420. /// 权限
  421. - (void)youpaifsetupAuthority{
  422. [UCAuthorityManager microPhoneAuthority:^{
  423. } denied:^{
  424. [self youpaifshowAlertVCWithTitle:@"请在iphone的“设置-隐私-麦克风”选项中,允许APP访问您的麦克风。"];
  425. }];
  426. }
  427. - (void)youpaifshowAlertVCWithTitle:(NSString *)title{
  428. UIAlertController *systemAlert = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  429. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"现在去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  430. if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0)
  431. {
  432. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  433. }
  434. else
  435. {
  436. // 去系统设置页面
  437. if (@available(iOS 10.0, *)) {
  438. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
  439. } else {
  440. // Fallback on earlier versions
  441. }
  442. }
  443. }];
  444. UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
  445. [systemAlert addAction:cancelAction];
  446. [systemAlert addAction:action];
  447. dispatch_async(dispatch_get_main_queue(), ^{
  448. [[LCTools getCurrentVC] presentViewController: systemAlert animated: YES completion: nil];
  449. });
  450. }
  451. - (void)setAudioSession{
  452. AVAudioSession *session = [AVAudioSession sharedInstance];
  453. NSError *sessionError;
  454. [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
  455. if(session == nil){
  456. NSLog(@"Error creating session: %@", [sessionError description]);
  457. }
  458. else{
  459. [session setActive:YES error:nil];
  460. }
  461. }
  462. - (void)youpaifstopSoundRecord{
  463. [self youpaifstopTimer];
  464. self.youpaiprecordAudioUrl = nil;
  465. self.youpaiprecorder = nil;
  466. // 停止播放
  467. [self.youpaipaudioPlayer stop];
  468. self.youpaipaudioPlayer = nil;
  469. }
  470. - (void)dealloc{
  471. [self youpaifstopSoundRecord];
  472. }
  473. @end