簡(jiǎn)概:
- 本次文章分別講述根據(jù)播放器播放的音樂(lè)和麥克風(fēng)采集的實(shí)時(shí)CMSampleBufferRef展示波形趣斤。
- CMSampleBufferRef 計(jì)算分貝的工具使用的是Objective-C版:GetVolumeLevelsFromeSampleBuffer偷溺,這里補(bǔ)充Swift版:SampleBufferToVolumeLevels
- 音樂(lè)播放器使用的是:KSYMediaPlayer_iOS,該播放器有實(shí)時(shí)的buffer數(shù)據(jù)回調(diào)接口,這里用來(lái)做demo很方便沟饥。
- 波形繪制使用的是基于:kevinzhow/Waver 的修改版DYwaver
- 如果你有問(wèn)題珠叔,或者對(duì)下述文字有任何意見(jiàn)與建議,除了在文章最后留言虎眨,還可以在微博陽(yáng)眼的熊1993上給我留言蟋软,或者聯(lián)系我的郵箱hu-yangyang@qq.com镶摘,以及訪問(wèn)我的Github。
- 文章某尾會(huì)給到Demo岳守。
- 完整音樂(lè)視頻效果:鏈接??鏈接_2??
波形效果
代碼簡(jiǎn)單介紹
- pod 相關(guān)工具
pod 'GetVolumeLevelsFromeSampleBuffer', :git => 'https://github.com/doubleYang1020/GetVolumeLevelsFromeSampleBuffer.git'
pod 'KSYMediaPlayer_iOS'
pod 'DYwaver', :git => 'https://github.com/doubleYang1020/DYwaver.git'
- import相關(guān)頭文件
#import <KSYMediaPlayer/KSYMediaPlayer.h>
#import "SampleBufferToVolumeLevelsEngine.h"
#import "DYwaver.h"
- 初始化播放器
+(float)getVolumeLevelsFromeSampleBuffer:(CMSampleBufferRef)sampleBuffer;
是根據(jù)buffer轉(zhuǎn)換音量volume
-(void)creatMediaPlayer{
__weak typeof(self) weakSelf = self;
_player = [[KSYMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://music.fffffive.com/1500458888991.mp3"]];
[_player setShouldAutoplay:true];
[_player setShouldLoop:true];
_player.audioDataBlock= ^(CMSampleBufferRef buf){
float volume = [SampleBufferToVolumeLevelsEngine getVolumeLevelsFromeSampleBuffer:buf];
weakSelf.audioVolume = volume;
};
[_player prepareToPlay];
}
- 初始化DYWaverView
-(void)creatWaverView{
__weak typeof(self) weakSelf = self;
UIColor* redColor = [UIColor colorWithRed:255.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0];
UIColor* orangeColor = [UIColor colorWithRed:255.0/255.0 green:165.0/255.0 blue:0.0/255.0 alpha:1.0];
UIColor* yellowColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:0.0/255.0 alpha:1.0];
UIColor* greenColor = [UIColor colorWithRed:0.0/255.0 green:255.0/255.0 blue:0.0/255.0 alpha:1.0];
UIColor* cyanColor = [UIColor colorWithRed:0.0/255.0 green:127.0/255.0 blue:255.0/255.0 alpha:1.0];
UIColor* blueColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:255.0/255.0 alpha:1.0];
UIColor* purpleColor = [UIColor colorWithRed:139.0/255.0 green:0.0/255.0 blue:255.0/255.0 alpha:1.0];
NSArray* colorsAry = [NSArray arrayWithObjects:redColor,orangeColor,yellowColor,greenColor,cyanColor,blueColor,purpleColor, nil];
_waver = [[Waver alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 2.0 - 40.0, [UIScreen mainScreen].bounds.size.width, 80.0) andNumberOfWaves:7 andWavesColors:colorsAry andDecorativeWavesWidth:1.0];
[_waver setUserInteractionEnabled:false];
[self.view addSubview:_waver];
__weak Waver * weakWaver = _waver;
_waver.waverLevelCallback = ^(Waver * waver) {
if (weakSelf.audioVolume) {
float normalizedValue = pow(10, weakSelf.audioVolume/40000) - 1.0;
weakWaver.level = normalizedValue * 1.0;
}
};
}
關(guān)于錄音實(shí)時(shí)顯示波形圖思路流程
- 在AVCaptureAudioDataOutputSampleBufferDelegate 回調(diào)中獲取audio 的buffer
optional public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)
這里需要注意的是AVCaptureVideoDataOutputSampleBufferDelegate 回調(diào)也是上面方法,所以這里要判斷出是否是音頻的SampleBuffer
let isAudio = output is AVCaptureAudioDataOutput
- 按上述步驟獲取到AudioSampleBuffer凄敢,在通過(guò)SampleBufferToVolumeLevels轉(zhuǎn)換成音量Volume,下面思路就和上面差不多了湿痢。
self.waver.waverLevelCallback = { [weak self] waver_1 in
guard let volume = self?.audioVolume else {
return
}
let normalizedValue = pow(10, volume/40000) - 1.0
waver_1!.level = CGFloat(normalizedValue * 1.0)
}