EZAudio是一款兼容iOS及OSX的,非常强大的第三方波形绘制第三方库
传送门-<a href='https://github.com/syedhali/EZAudio#EZAudioPlayer'>https://github.com/syedhali/EZAudio#EZAudioPlayer</a>
该库的主要组件如下
EZAudioDevice
一个有用的类获取所有可用的电流和输入/输出任何苹果设备。EZMicrophone和EZOutput用它来直接声音/从不同的硬件组件。
EZMicrophone
接管设备麦克风,通过代理来传输你想要的数据
EZOutput
接管设备扬声器,通过代理来传输你想要的数据
EZAudioFile
读取音频文件会用到这个类
EZAudioPlayer
播放音频会用到这个
EZRecorder
录制音频会用到这个
EZAudioPlot
基于图形核心的视图基类
EZAudioPlotGL
基于OpenGL的视图基类
波形图的View,使用EZAudioPlot和AudioPlotGL这两个类
EZAudioPlot继承自UIView
EZAudioPlotGL继承自GLKView
<b>Tips:这两个都是基类,一定要写一个类继承自这俩个类的其中一个才能使用</b>
//错误写法,因为EZAudioPlotGL是基类不能直接使用
@property (nonatomic, strong) EZAudioPlotGL *audioPlot;
//正确写法
//1.写一个类继承自EZAudioPlot或者EZAudioPlotGL
#import "EZAudio.h"
@interface WaveView : EZAudioPlot
@end
//2.用新的类定义对象
#import "WaveView.h"
@interface ViewController ()<EZMicrophoneDelegate>
@property (nonatomic, strong) WaveView *waveView;
@end
1、懒加载生成视图
- (WaveView *)waveView {
if(!_waveView) {
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if (error) {
NSLog(@"Error setting up audio session category: %@", error.localizedDescription);
}
[session setActive:YES error:&error];
if (error) {
NSLog(@"Error setting up audio session active: %@", error.localizedDescription);
}
_waveView = [[WaveView alloc] initWithFrame:CGRectMake(0, 100, kScreenWidth, 200)];
_waveView.backgroundColor = [UIColor colorWithRed:0.569 green:0.82 blue:0.478 alpha:1.0];
//声波颜色
_waveView.color = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
_waveView.plotType = EZPlotTypeRolling;//声波类型
_waveView.shouldFill = YES;
_waveView.shouldMirror = YES;
//创建麦克风
self.microphone = [EZMicrophone microphoneWithDelegate:self];
//设置输入设备
[self.microphone setDevice:[[EZAudioDevice inputDevices] firstObject]];
}
return _waveView;
}
2、创建按钮来控制
- (UIButton *)startBtn {
if(!_startBtn) {
_startBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth*0.5-50, kScreenHeight-200, 100, 100)];
_startBtn.layer.cornerRadius = 50;
_startBtn.layer.masksToBounds = true;
_startBtn.backgroundColor = [UIColor blueColor];
[_startBtn setTitle:@"点击录音" forState:UIControlStateNormal];
[_startBtn setTitle:@"松开停止" forState:UIControlStateHighlighted];
[_startBtn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchDown];
[_startBtn addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
}
return _startBtn;
}
3、按钮相关方法
- (void)start{
NSLog(@"开始");
[self.microphone startFetchingAudio];
}
- (void)stop{
NSLog(@"结束");
[self.microphone stopFetchingAudio];
}
4、麦克风相关代理
#pragma mark - 麦克风代理
//获取buffer流的音频数据信息
- (void)microphone:(EZMicrophone *)microphone hasAudioReceived:(float **)buffer withBufferSize:(UInt32)bufferSize
withNumberOfChannels:(UInt32)numberOfChannels {
//线程安全的
//buffer[0]是左声道 。buffer[1]是右声道
__weak typeof (self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.waveView updateBuffer:buffer[0] withBufferSize:bufferSize];
});
}
//------------------------------------------------------------------------------
//麦克风的AudioStreamBasicDescription流。这是非常有用的
//当配置EZRecorder或告诉另一个组件/ /音频格式类型。
- (void)microphone:(EZMicrophone *)microphone hasAudioStreamBasicDescription:(AudioStreamBasicDescription)audioStreamBasicDescription {
[EZAudioUtilities printASBD:audioStreamBasicDescription];
}
5、ViewDidLoad中初始化
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.waveView];
[self.view addSubview:self.startBtn];
}
以上,欢迎指正
有空再细化
have fun :)