D3RecordButton.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // D3RecordButton.m
  3. // D3RecordButtonDemo
  4. //
  5. // Created by bmind on 15/7/28.
  6. // Copyright (c) 2015年 bmind. All rights reserved.
  7. //
  8. #import "D3RecordButton.h"
  9. #import "RecordHUD.h"
  10. @implementation D3RecordButton
  11. //上滑取消录音
  12. -(void)initRecord:(id<D3RecordDelegate>)delegate maxtime:(int)_maxTime title:(NSString *)_title{
  13. self.delegate = delegate;
  14. maxTime = _maxTime;
  15. title = _title;
  16. mp3 = [[Mp3Recorder alloc]initWithDelegate:self];
  17. [self addTarget:self action:@selector(startRecord) forControlEvents:UIControlEventTouchDown];
  18. [self addTarget:self action:@selector(stopRecord) forControlEvents:UIControlEventTouchUpInside];
  19. [self addTarget:self action:@selector(cancelRecord) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchCancel];
  20. [self addTarget:self action:@selector(RemindDragExit:) forControlEvents:UIControlEventTouchDragExit];
  21. [self addTarget:self action:@selector(RemindDragEnter:) forControlEvents:UIControlEventTouchDragEnter];
  22. }
  23. //松开按钮取消录音
  24. -(void)initRecord:(id<D3RecordDelegate>)delegate maxtime:(int)_maxTime{
  25. [self initRecord:delegate maxtime:_maxTime title:nil];
  26. }
  27. //开始录音
  28. -(void)startRecord{
  29. [mp3 startRecord];
  30. [RecordHUD show];
  31. [self setHUDTitle];
  32. if ([_delegate respondsToSelector:@selector(dragEnter)]) {
  33. [_delegate dragEnter];
  34. }
  35. }
  36. //正常停止录音,开始转换数据
  37. -(void)stopRecord{
  38. [mp3 stopRecord];
  39. [RecordHUD dismiss];
  40. }
  41. //取消录音
  42. -(void)cancelRecord{
  43. [mp3 cancelRecord];
  44. [RecordHUD dismiss];
  45. [RecordHUD setTitle:@"已取消录音"];
  46. }
  47. //离开按钮范围
  48. - (void)RemindDragExit:(UIButton *)button
  49. {
  50. [RecordHUD setTitle:@"松手取消录音"];
  51. if ([_delegate respondsToSelector:@selector(dragExit)]) {
  52. [_delegate dragExit];
  53. }
  54. }
  55. //进入按钮范围
  56. - (void)RemindDragEnter:(UIButton *)button
  57. {
  58. [self setHUDTitle];
  59. }
  60. -(void)setHUDTitle{
  61. if (title != nil) {
  62. [RecordHUD setTitle:title];
  63. }
  64. else{
  65. [RecordHUD setTitle:@"离开按钮取消录音"];
  66. }
  67. }
  68. #pragma mark Mp3RecordDelegate
  69. -(void)beginConvert{
  70. }
  71. //录音失败
  72. - (void)failRecord
  73. {
  74. }
  75. //回调录音资料
  76. - (void)endConvertWithData:(NSData *)voiceData filePath:(NSString *)path
  77. {
  78. [RecordHUD setTitle:@"录音成功"];
  79. if ([_delegate respondsToSelector:@selector(endRecord:filePath:)]) {
  80. [_delegate endRecord:voiceData filePath:path];
  81. }
  82. }
  83. -(void)recording:(float)recordTime volume:(float)volume{
  84. if (recordTime>=maxTime) {
  85. [self stopRecord];
  86. }
  87. [RecordHUD setImage:[NSString stringWithFormat:@"mic_%.0f.png",volume*10 > 5 ? 5 : volume*10]];
  88. [RecordHUD setTimeTitle:[NSString stringWithFormat:@"录音: %.0f\"",recordTime]];
  89. }
  90. @end