溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS如何使用音頻處理框架The Amazing Audio Engine實(shí)現(xiàn)音頻錄制播放

發(fā)布時(shí)間:2021-08-06 14:05:40 來(lái)源:億速云 閱讀:122 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

小編給大家分享一下iOS如何使用音頻處理框架The Amazing Audio Engine實(shí)現(xiàn)音頻錄制播放,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

首先看一下效果圖:

iOS如何使用音頻處理框架The Amazing Audio Engine實(shí)現(xiàn)音頻錄制播放

下面貼上核心控制器代碼:

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#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;
 
 //錄制時(shí)間
 UILabel *recordTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(recLabel.frame) + 20, CGRectGetMinY(recLabel.frame), 150, 40)];
 recordTimeLabel.text = @"錄制時(shí)長(zhǎng):00:00";
 [self.view addSubview:recordTimeLabel];
 _recordTimeLabel = recordTimeLabel;
 
 //播放時(shí)間
 UILabel *playTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(recordTimeLabel.frame), CGRectGetMaxY(recordTimeLabel.frame), 150, 40)];
 playTimeLabel.text = @"播放時(shí)長(zhǎng):00:00";
 playTimeLabel.hidden = YES;
 [self.view addSubview:playTimeLabel];
 _playTimeLabel = playTimeLabel;
 
 //配樂(lè)按鈕
 NSArray *titleArray = @[@"無(wú)配樂(lè)", @"夏天", @"陽(yáng)光海灣"];
 CGFloat btnW = 80.0f;
 CGFloat padding = (KMainW - marginX * 2 - btnW * titleArray.count) / (titleArray.count - 1);
 for (int i = 0; i < titleArray.count; i++) {
 CGFloat btnX = marginX + (btnW + padding) * i;
 UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnX, CGRectGetMaxY(playTimeLabel.frame) + 20, btnW, btnW)];
 [btn setTitle:titleArray[i] forState:UIControlStateNormal];
 btn.layer.cornerRadius = btnW * 0.5;
 btn.layer.masksToBounds = YES;
 [btn setBackgroundImage:[UIImage imageWithColor:[UIColor grayColor]] forState:UIControlStateNormal];
 [btn setBackgroundImage:[UIImage imageWithColor:[UIColor orangeColor]] forState:UIControlStateSelected];
 if (i == 0) btn.selected = YES;
 btn.tag = 100 + i;
 [btn addTarget:self action:@selector(changeBackgroundMusic:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:btn];
 }
 
 //配樂(lè)音量
 UILabel *backgroundLabel = [[UILabel alloc] initWithFrame:CGRectMake(marginX + 10, CGRectGetMaxY(playTimeLabel.frame) + 120, 80, 40)];
 backgroundLabel.text = @"配樂(lè)音量";
 [self.view addSubview:backgroundLabel];
 
 //配樂(lè)音量
 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(CGRectGetMaxX(backgroundLabel.frame) + 10, CGRectGetMinY(backgroundLabel.frame), 210, 40)];
 slider.value = 0.4f;
 [slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
 [self.view addSubview:slider];
 _slider = slider;
 
 //試聽(tīng)按鈕
 UIButton *auditionBtn = [[UIButton alloc] initWithFrame:CGRectMake(marginX, KMainH - 150, 120, 80)];
 auditionBtn.hidden = YES;
 auditionBtn.backgroundColor = [UIColor blackColor];
 [auditionBtn setTitle:@"試聽(tīng)" forState:UIControlStateNormal];
 [auditionBtn setTitle:@"停止" forState:UIControlStateSelected];
 [auditionBtn addTarget:self action:@selector(auditionBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:auditionBtn];
 _auditionBtn = auditionBtn;
 
 //錄音按鈕
 UIButton *recordBtn = [[UIButton alloc] initWithFrame:CGRectMake(KMainW - marginX - 120, KMainH - 150, 120, 80)];
 recordBtn.backgroundColor = [UIColor blackColor];
 [recordBtn setTitle:@"開(kāi)始" forState:UIControlStateNormal];
 [recordBtn setTitle:@"暫停" forState:UIControlStateSelected];
 [recordBtn addTarget:self action:@selector(recordBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:recordBtn];
 _recordBtn = recordBtn;
}
 
//配樂(lè)按鈕點(diǎn)擊事件
- (void)changeBackgroundMusic:(UIButton *)btn
{
 //更新選中狀態(tài)
 for (int i = 0; i < 3; i++) {
 UIButton *button = (UIButton *)[self.view viewWithTag:100 + i];
 button.selected = NO;
 }
 btn.selected = YES;
 
 //移除之前配樂(lè)
 if (_backgroundPlayer) {
 [_audioController removeChannels:@[_backgroundPlayer]];
 _backgroundPlayer = nil;
 }
 
 NSURL *url;
 if (btn.tag == 100) {
 return;
 }else if (btn.tag == 101) {
 url = [[NSBundle mainBundle]URLForResource:@"夏天.mp3" withExtension:nil];
 }else if (btn.tag == 102) {
 url = [[NSBundle mainBundle]URLForResource:@"陽(yáng)光海灣.mp3" withExtension:nil];
 }
 [self.audioController start:NULL];
 
 NSError *AVerror = NULL;
 _backgroundPlayer = [AEAudioFilePlayer audioFilePlayerWithURL:url error:&AVerror];
 _backgroundPlayer.volume = _slider.value;
 _backgroundPlayer.loop = YES;
 if (!_backgroundPlayer) {
 [[[UIAlertView alloc] initWithTitle:@"Error"
     message:[NSString stringWithFormat:@"Couldn't start playback: %@", [AVerror localizedDescription]]
     delegate:nil
    cancelButtonTitle:nil
    otherButtonTitles:@"OK", nil] show];
 return;
 }
 
 //放完移除
 _backgroundPlayer.removeUponFinish = YES;
 __weak ViewController *weakSelf = self;
 _backgroundPlayer.completionBlock = ^{
 weakSelf.backgroundPlayer = nil;
 };
 [_audioController addChannels:@[_backgroundPlayer]];
}
 
//配樂(lè)音量slider滑動(dòng)事件
- (void)sliderValueChanged:(UISlider *)slider
{
 if (_backgroundPlayer) _backgroundPlayer.volume = slider.value;
}
 
//錄音按鈕點(diǎn)擊事件
- (void)recordBtnOnClick:(UIButton *)btn
{
 [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
 if (granted) {
  //用戶同意獲取麥克風(fēng)
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  btn.selected = !btn.selected;
  
  if (btn.selected) {
   [self startRecord];
  }else {
   [self finishRecord];
  }
  });
  
 }else {
  //用戶不同意獲取麥克風(fēng)
  [HWProgressHUD showMessage:@"需要訪問(wèn)您的麥克風(fēng),請(qǐng)?jiān)凇霸O(shè)置-隱私-麥克風(fēng)”中允許訪問(wèn)。" duration:3.f];
 }
 }];
}
 
//開(kāi)始錄音
- (void)startRecord
{
 _auditionBtn.hidden = YES;
 [self.audioController start:NULL];
 _recorder = [[AERecorder alloc] initWithAudioController:_audioController];
 _path = [self getPath];
 
 NSError *error = NULL;
 if ( ![_recorder beginRecordingToFileAtPath:_path fileType:kAudioFileM4AType error:&error] ) {
 [[[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Couldn't start recording: %@", [error localizedDescription]] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
 _recorder = nil;
 return;
 }
 
 [self.soundSource removeAllObjects];
 [self removeTimer];
 [self addRecordTimer];
 
 [_audioController addOutputReceiver:_recorder];
 [_audioController addInputReceiver:_recorder];
}
 
//結(jié)束錄音
- (void)finishRecord
{
 _auditionBtn.hidden = NO;
 _recLabel.hidden = NO;
 [self removeTimer];
 
 [_recorder finishRecording];
 [_audioController removeOutputReceiver:_recorder];
 [_audioController removeInputReceiver:_recorder];
 _recorder = nil;
}
 
//添加錄音定時(shí)器
- (void)addRecordTimer
{
 self.timer = [NSTimer scheduledTimerWithTimeInterval:.2f target:self selector:@selector(recordTimerAction) userInfo:nil repeats:YES];
 [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
 
//錄音定時(shí)器事件
- (void)recordTimerAction
{
 //獲取音頻
 [CATransaction begin];
 [CATransaction setDisableActions:YES];
 Float32 inputAvg, inputPeak, outputAvg, outputPeak;
 [_audioController inputAveragePowerLevel:&inputAvg peakHoldLevel:&inputPeak];
 [_audioController outputAveragePowerLevel:&outputAvg peakHoldLevel:&outputPeak];
 [self.soundSource insertObject:[NSNumber numberWithFloat:(inputPeak + 18) * 2.8] atIndex:0];
 [CATransaction commit];
 _recordingDrawView.pointArray = _soundSource;
 
 //REC閃動(dòng)
 _recLabel.hidden = (int)[self.recorder currentTime] % 2 == 1 ? YES : NO;
 
 //錄音時(shí)間
 NSString *str = [self strWithTime:[self.recorder currentTime] interval:0.5f];
 if ([str intValue] < 0) str = @"錄制時(shí)長(zhǎng):00:00";
 [self.recordTimeLabel setText:[NSString stringWithFormat:@"錄制時(shí)長(zhǎng):%@", str]];
}
 
//移除定時(shí)器
- (void)removeTimer
{
 [self.timer invalidate];
 self.timer = nil;
}
 
//試聽(tīng)按鈕點(diǎn)擊事件
- (void)auditionBtnOnClick:(UIButton *)btn
{
 btn.selected = !btn.selected;
 
 if (btn.selected) {
 [self playRecord];
 }else {
 [self stopPlayRecord];
 }
}
 
//播放錄音
- (void)playRecord
{
 //更新界面
 _recordBtn.hidden = YES;
 [_playTimeLabel setText:@"播放時(shí)長(zhǎng):00:00"];
 _playTimeLabel.hidden = NO;
 
 //取消背景音樂(lè)
 [self changeBackgroundMusic:(UIButton *)[self.view viewWithTag:100]];
 
 if (![[NSFileManager defaultManager] fileExistsAtPath:_path]) return;
 
 NSError *error = nil;
 _player = [AEAudioFilePlayer audioFilePlayerWithURL:[NSURL fileURLWithPath:_path] error:&error];
 if (!_player) {
 [[[UIAlertView alloc] initWithTitle:@"Error"
     message:[NSString stringWithFormat:@"Couldn't start playback: %@", [error localizedDescription]]
     delegate:nil
    cancelButtonTitle:nil
    otherButtonTitles:@"OK", nil] show];
 return;
 }
 
 [self addPlayTimer];
 _player.removeUponFinish = YES;
 
 __weak ViewController *weakSelf = self;
 _player.completionBlock = ^{
 weakSelf.player = nil;
 weakSelf.auditionBtn.selected = NO;
 [weakSelf stopPlayRecord];
 };
 [self.audioController start:NULL];
 [self.audioController addChannels:@[_player]];
}
 
//停止播放錄音
- (void)stopPlayRecord
{
 _recordBtn.hidden = NO;
 _playTimeLabel.hidden = YES;
 [self removeTimer];
 if (_player) [_audioController removeChannels:@[_player]];
}
 
//添加播放定時(shí)器
- (void)addPlayTimer
{
 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(playTimerAction) userInfo:nil repeats:YES];
 [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
 
//播放定時(shí)器事件
- (void)playTimerAction
{
 //播放時(shí)間
 NSString *str = [self strWithTime:[_player currentTime] interval:1.f];
 if ([str intValue] < 0) str = @"播放時(shí)長(zhǎng):00:00";
 [_playTimeLabel setText:[NSString stringWithFormat:@"播放時(shí)長(zhǎng):%@", str]];
}
 
//錄制音頻沙盒路徑
- (NSString *)getPath
{
 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 [formatter setDateFormat:@"YYYYMMddhhmmss"];
 NSString *recordName = [NSString stringWithFormat:@"%@.wav", [formatter stringFromDate:[NSDate date]]];
 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:recordName];
 
 return path;
}
 
//時(shí)長(zhǎng)長(zhǎng)度轉(zhuǎn)時(shí)間字符串
- (NSString *)strWithTime:(double)time interval:(CGFloat)interval
{
 int minute = (time * interval) / 60;
 int second = (int)(time * interval) % 60;
 
 return [NSString stringWithFormat:@"%02d:%02d", minute, second];
}
 
@end

看完了這篇文章,相信你對(duì)“iOS如何使用音頻處理框架The Amazing Audio Engine實(shí)現(xiàn)音頻錄制播放”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI