iOS 第三方音频框架The Amazing Audio Engine使用,实现音频录制、播放,可设置配乐。
首先看一下效果图:
下面贴上核心控制器代码:
#import "ViewController.h" #import#import "HWProgressHUD.h" #import "UIImage+HW.h" #import "AERecorder.h" #import "HWRecordingDrawView.h" #define KMainW [UIScreen mainScreen].bounds.size.width #define KMainH [UIScreen mainScreen].bounds.size.height @interface ViewController () @property (nonatomic, strong) AERecorder *recorder; @property (nonatomic, strong) AEAudioController *audioController; @property (nonatomic, strong) AEAudioFilePlayer *player; @property (nonatomic, strong) AEAudioFilePlayer *backgroundPlayer; @property (nonatomic, strong) NSTimer *timer; @property (nonatomic, strong) NSMutableArray *soundSource; @property (nonatomic, weak) HWRecordingDrawView *recordingDrawView; @property (nonatomic, weak) UILabel *recLabel; @property (nonatomic, weak) UILabel *recordTimeLabel; @property (nonatomic, weak) UILabel *playTimeLabel; @property (nonatomic, weak) UIButton *auditionBtn; @property (nonatomic, weak) UIButton *recordBtn; @property (nonatomic, weak) UISlider *slider; @property (nonatomic, copy) NSString *path; @end @implementation ViewController - (AEAudioController *)audioController { if (!_audioController) { _audioCOntroller= [[AEAudioController alloc] initWithAudioDescription:[AEAudioController nonInterleavedFloatStereoAudioDescription] inputEnabled:YES]; _audioController.preferredBufferDuration = 0.005; _audioController.useMeasurementMode = YES; } return _audioController; } - (NSMutableArray *)soundSource { if (!_soundSource) { _soundSource = [NSMutableArray array]; } return _soundSource; } - (void)viewDidLoad { [super viewDidLoad]; [self creatControl]; } - (void)creatControl { CGFloat marginX = 30.0f; //音频视图 HWRecordingDrawView *recordingDrawView = [[HWRecordingDrawView alloc] initWithFrame:CGRectMake(marginX, 80, KMainW - marginX * 2, 100)]; [self.view addSubview:recordingDrawView]; _recordingDrawView = recordingDrawView; //REC UILabel *recLabel = [[UILabel alloc] initWithFrame:CGRectMake(marginX, CGRectGetMaxY(recordingDrawView.frame) + 20, 80, 40)]; recLabel.text = @"REC"; recLabel.textColor = [UIColor redColor]; [self.view addSubview:recLabel]; _recLabel = recLabel; //录制时间 UILabel *recordTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(recLabel.frame) + 20, CGRectGetMinY(recLabel.frame), 150, 40)]; recordTimeLabel.text = @"录制时长:00:00"; [self.view addSubview:recordTimeLabel]; _recordTimeLabel = recordTimeLabel; //播放时间 UILabel *playTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(recordTimeLabel.frame), CGRectGetMaxY(recordTimeLabel.frame), 150, 40)]; playTimeLabel.text = @"播放时长:00:00"; playTimeLabel.hidden = YES; [self.view addSubview:playTimeLabel]; _playTimeLabel = playTimeLabel; //配乐按钮 NSArray *titleArray = @[@"无配乐", @"夏天", @"阳光海湾"]; CGFloat btnW = 80.0f; CGFloat padding = (KMainW - marginX * 2 - btnW * titleArray.count) / (titleArray.count - 1); for (int i = 0; i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。