123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- //
- // KSMergeEnginePlayer.m
- // MutiPlayDemo
- //
- // Created by 王智 on 2024/6/17.
- //
- #import "KSMergeEnginePlayer.h"
- #import <AVFoundation/AVFoundation.h>
- #import <Accelerate/Accelerate.h>
- #define READ_FILE_LENGTH (8192)
- #define BUFFER_SIZE (2048)
- @interface KSMergeEnginePlayer ()
- /** 定时器 */
- @property (nonatomic, strong) NSTimer *timer;
- @property (nonatomic, strong) AVAudioEngine *audioEngine;
- @property (nonatomic, strong) AVAudioPlayerNode *nodePlayer;
- @property (nonatomic, strong) AVAudioFile *audioFile;
- @property (nonatomic, strong) AVAudioFormat *audioFormat;
- @property (nonatomic, strong) AVAudioFile *bgAudioFile;
- @property (nonatomic, strong) AVAudioFormat *bgAudioFormat;
- @property (nonatomic, assign) NSTimeInterval totalDuration;
- @property (nonatomic, assign) NSInteger offsetTime; // 延迟时间
- @property (nonatomic, assign) AVAudioFramePosition startPosition; // 开始位置
- @property (nonatomic, strong) dispatch_queue_t sourceQueue;
- @property (nonatomic, strong) AVAudioPCMBuffer *mixBuffer;
- @property (nonatomic, assign) AVAudioFramePosition currentFrame;
- @property (nonatomic, assign) double sampleRate;
- @property (nonatomic, assign) NSInteger timeCount;
- @property (nonatomic, assign) BOOL isCanceled; // 是否在取消
- @end
- @implementation KSMergeEnginePlayer
- - (instancetype)init {
- self = [super init];
- if (self) {
- [self configDefault];
- }
- return self;
- }
- - (void)configDefault {
- self.recordVolume = 1.0f;
- self.bgVolume = 1.0f;
- }
- - (void)configEngine {
- [self setupAudioSession];
-
- self.audioEngine = [[AVAudioEngine alloc] init];
- self.nodePlayer = [[AVAudioPlayerNode alloc] init];
-
- // attach node
- [self.audioEngine attachNode:self.nodePlayer];
-
- }
- - (void)setupAudioSession {
- NSError *err = nil;
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
-
- @try {
- [audioSession setActive:YES error:&err];
- } @catch (NSException *exception) {
-
- } @finally {
-
- }
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:audioSession];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRouteChange:) name:AVAudioSessionRouteChangeNotification object:audioSession];
- }
- - (void)retryInitEngine {
- // 如果audio engine不存在
- self.audioEngine = [[AVAudioEngine alloc] init];
- [self.audioEngine attachNode:self.nodePlayer];
- AVAudioFormat *outputFormat = [self.audioEngine.mainMixerNode outputFormatForBus:0];
- [self.audioEngine connect:self.nodePlayer to:self.audioEngine.mainMixerNode format:outputFormat];
- }
- - (void)startEngine {
- if (!self.audioEngine) { // 如果audio engine 被释放
- [self retryInitEngine];
- }
- // 启动engine
- NSError *error = nil;
- @try {
- [self.audioEngine startAndReturnError:&error];
- } @catch (NSException *exception) {
- NSLog(@"--------Exception: %@", exception);
- } @finally {
- if (error) {
- self.audioEngine = nil;
- // 错误回调
- if (self.delegate && [self.delegate respondsToSelector:@selector(enginePlayerDidError:error:)]) {
- [self.delegate enginePlayerDidError:self error:error];
- }
- }
- }
- }
- - (void)prepareNativeSongWithUrl:(NSURL *)recordAudioUrl bgMusic:(NSURL *)bgMusicUrl {
- @weakObj(self);
- dispatch_async(self.sourceQueue, ^{
- @strongObj(self);
- if (!self || self.isCanceled) {
- return;
- }
- [self loadAuidoFile:recordAudioUrl isBgm:NO];
- [self loadAuidoFile:bgMusicUrl isBgm:YES];
- self.sampleRate = self.audioFile.fileFormat.sampleRate;
- [self configEngine];
-
- AVAudioFormat *outputFormat = [self.audioEngine.mainMixerNode outputFormatForBus:0];
- [self.audioEngine connect:self.nodePlayer to:self.audioEngine.mainMixerNode format:outputFormat];
- [self startEngine];
-
- if (self.audioEngine && self.audioEngine.isRunning) {
- [self prepareBufferFrame];
- }
- });
- }
- - (void)addTapBus {
- BOOL delegateRespondsToDidGenerateSpectrum = [self.delegate respondsToSelector:@selector(player:didGenerateSpectrum:)];
- self.analyzer = [[KSRealtimeAnalyzer alloc] initWithFFTSize:BUFFER_SIZE];
- AVAudioFormat *outputFormat = [self.audioEngine.mainMixerNode outputFormatForBus:0];
- @weakObj(self);
- [self.audioEngine.mainMixerNode removeTapOnBus:0];
- [self.audioEngine.mainMixerNode installTapOnBus:0 bufferSize:BUFFER_SIZE format:outputFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
- @strongObj(self);
- if (!self || !self.nodePlayer.isPlaying) {
- return;
- }
- // 将频谱分析任务提交到后台队列
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
- if (self.analyzer.isAnalise == NO) {
- // 分析音频缓冲区
- NSArray<NSArray<NSNumber *> *> *spectra = [self.analyzer analyseWithBuffer:buffer];
-
- // 回到主线程更新 UI 或调用委托方法
- dispatch_async(dispatch_get_main_queue(), ^{
- if (delegateRespondsToDidGenerateSpectrum) {
- [self.delegate player:self didGenerateSpectrum:spectra];
- }
- });
- }
- });
- }];
- }
- - (void)loadAuidoFile:(NSURL *)audioFileUrl isBgm:(BOOL)isBgm {
- NSError *error = nil;
- AVAudioFile *audioFile = nil;
- AVAudioFormat *audioFormat = nil;
- @try {
- audioFile = [[AVAudioFile alloc] initForReading:audioFileUrl error:&error];
- audioFormat = audioFile.processingFormat;
-
- } @catch (NSException *exception) {
- audioFile = nil;
- audioFormat = nil;
- } @finally {
- if (error) {
- // 错误回调
- }
- else { // 加载成功
- if (isBgm) {
- self.bgAudioFile = audioFile;
- self.bgAudioFormat = audioFormat;
- }
- else {
- self.audioFile = audioFile;
- self.audioFormat = audioFormat;
- }
- }
- }
- }
- - (void)resetMixBuffer {
- AVAudioFrameCount minFrameCount = (AVAudioFrameCount)MIN(self.bgAudioFile.length, self.audioFile.length);
- // mixBuffer
- AVAudioFormat *outputFormat = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:self.bgAudioFormat.sampleRate channels:2];
- self.mixBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:outputFormat frameCapacity:minFrameCount];
- self.mixBuffer.frameLength = minFrameCount;
- }
- - (void)prepareBufferFrame {
- [self resetMixBuffer];
- dispatch_main_async_safe(^{
- if (self.delegate && [self.delegate respondsToSelector:@selector(enginePlayerIsReadyPlay:)]) {
- self.isReady = YES;
- [self.delegate enginePlayerIsReadyPlay:self];
- }
- });
- }
- // 预计加载buffer
- - (void)prepareBuffer:(AVAudioFramePosition)startPosition offset:(NSInteger)offsetTime mixStart:(AVAudioFramePosition)mixStartPosition {
-
- @weakObj(self);
- dispatch_async(self.sourceQueue, ^{
- @strongObj(self);
- if (!self || self.isCanceled) {
- return;
- }
- if (!self.bgAudioFile || !self.audioFile) {
- return;
- }
- AVAudioFramePosition minFrameCount = (AVAudioFramePosition)MIN(self.bgAudioFile.length, self.audioFile.length);
- AVAudioFrameCount offsetFrame = labs(offsetTime)/1000.0 * self.audioFile.processingFormat.sampleRate;
- if (minFrameCount <= startPosition) {
- return;
- }
- AVAudioFrameCount frameToRead = minFrameCount - startPosition > READ_FILE_LENGTH ? READ_FILE_LENGTH : (AVAudioFrameCount)(minFrameCount - startPosition);
-
- self.bgAudioFile.framePosition = startPosition;
- AVAudioPCMBuffer *bgBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:self.bgAudioFile.processingFormat frameCapacity:frameToRead];
- bgBuffer.frameLength = frameToRead;
- BOOL readSuccess = [self.bgAudioFile readIntoBuffer:bgBuffer frameCount:frameToRead error:nil];
- if (!readSuccess) {
- return;
- }
- AVAudioPCMBuffer *recordBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:self.audioFile.processingFormat frameCapacity:frameToRead];
- recordBuffer.frameLength = frameToRead;
-
- if (offsetTime >= 0) { // 演奏需要提前
- self.audioFile.framePosition = startPosition + offsetFrame;
- AVAudioFrameCount audioReadFrame = frameToRead;
- if (startPosition + offsetFrame + frameToRead > minFrameCount) { // 如果超出
- audioReadFrame = (AVAudioFrameCount)(minFrameCount - startPosition - offsetFrame);
- }
- if (audioReadFrame <= frameToRead) {
- BOOL isSuccess = [self.audioFile readIntoBuffer:recordBuffer frameCount:audioReadFrame error:nil];
- if (!isSuccess) {
- return;
- }
- }
- }
- else { // 演奏需要延后
- AVAudioFramePosition audioFramePosition = startPosition - offsetFrame;
- if (audioFramePosition > 0) {
- self.audioFile.framePosition = audioFramePosition;
- AVAudioFrameCount audioReadFrame = frameToRead;
- if (audioFramePosition + frameToRead > minFrameCount) { // 如果超出
- audioReadFrame = (AVAudioFrameCount)(minFrameCount - audioFramePosition);
- }
- // AVAudioFrameCount 无符号整型 uint32_t
- if (audioReadFrame <= frameToRead) {
- BOOL isSuccess = [self.audioFile readIntoBuffer:recordBuffer frameCount:audioReadFrame error:nil];
- if (!isSuccess) {
- return;
- }
- }
- }
- else {
- self.audioFile.framePosition = 0;
- // 需要读取部分数据
- if (offsetFrame - startPosition < frameToRead) {
- AVAudioFrameCount readCount = (AVAudioFrameCount)(offsetFrame - startPosition);
- // NSLog(@"----need readCount --%u", readCount);
- AVAudioPCMBuffer *tempBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:self.audioFile.processingFormat frameCapacity:readCount];
- tempBuffer.frameLength = readCount;
- BOOL isSuccess = [self.audioFile readIntoBuffer:tempBuffer error:nil];
- if (!isSuccess) {
- return;
- }
- float *tempData = tempBuffer.floatChannelData[0];
- float *recordData = recordBuffer.floatChannelData[0];
- // 复制数据到 recordBuffer
- AVAudioFrameCount startFrame = frameToRead - readCount;
- for (AVAudioFrameCount i = 0; i < readCount; i++) {
- recordData[startFrame + i] = tempData[i];
- }
- }
- }
- }
-
- float *bgLeftChannel = bgBuffer.floatChannelData[0];
- float *bgRightChannel = bgBuffer.floatChannelData[1];
- if (bgBuffer.format.channelCount == 1) {
- bgRightChannel = bgBuffer.floatChannelData[0];
- }
- // 录音文件未单声道
- float *recordLeftChannel = recordBuffer.floatChannelData[0];
-
- float *mixLeftChannel = self.mixBuffer.floatChannelData[0];
- float *mixRightChannel = self.mixBuffer.floatChannelData[1];
-
- for (int frame = 0; frame < frameToRead; frame++) {
-
- AVAudioFramePosition mixIndex = frame + startPosition - mixStartPosition;
- float leftChannel = (frame < bgBuffer.frameLength) ? bgLeftChannel[frame] : 0;
- float rightChannel = (frame < bgBuffer.frameLength) ? bgRightChannel[frame] : 0;
-
- float recordData = (frame < recordBuffer.frameLength) ? recordLeftChannel[frame] : 0;
-
- float mixLeftData = [self mixChannelData:leftChannel bgVolume:self.bgVolume recordData:recordData recordVolume:self.recordVolume];
- float mixRightData = [self mixChannelData:rightChannel bgVolume:self.bgVolume recordData:recordData recordVolume:self.recordVolume];
-
- // 防止数组越界
- if (mixIndex >= 0 && mixIndex < self.mixBuffer.frameLength) {
- mixLeftChannel[mixIndex] = fminf(fmaxf(mixLeftData, -1.0f), 1.0f);
- mixRightChannel[mixIndex] = fminf(fmaxf(mixRightData, -1.0f), 1.0f);
- }
- }
- });
- }
- - (void)scheduleBufferFromPosition:(AVAudioFramePosition)startPosition {
- [self resetMixBuffer];
- self.startPosition = startPosition;
- [self prepareBuffer:startPosition offset:self.offsetTime mixStart:startPosition];
- // 加载缓冲区
- [self.nodePlayer scheduleBuffer:self.mixBuffer atTime:nil options:AVAudioPlayerNodeBufferInterruptsAtLoop completionHandler:^{
-
- }];
- }
- - (float)mixChannelData:(float)bgData bgVolume:(float)bgVolume recordData:(float)recordData recordVolume:(float)recordVolume {
- return (bgData * bgVolume + recordData * recordVolume) / 2;
- }
- - (void)changeVolume:(float)bgVolume recordVolume:(float)recordVolume {
- // NSLog(@"bg volume ---- %f, record volume ---- %f", bgVolume, recordVolume);
- self.bgVolume = bgVolume;
- self.recordVolume = recordVolume;
- }
- // 打断处理
- - (void)handleInterruption:(NSNotification *)notification {
- NSDictionary *info = notification.userInfo;
-
- AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
- if (type == AVAudioSessionInterruptionTypeBegan) {
- NSLog(@"---- 播放打断");
- //Handle InterruptionBegan
- // 停止播放
- if (self.nodePlayer.isPlaying) {
- [self.nodePlayer stop];
- [self sendInterruptError:nil];
- }
- }
- else if (type == AVAudioSessionInterruptionTypeEnded) {
- AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
- if (options == AVAudioSessionInterruptionOptionShouldResume) {
- //Handle Resume
- [self resumeAudioSession];
- NSLog(@"---- 播放恢复");
- }
- }
- }
- - (void)resumeAudioSession {
- NSError *error = nil;
- [[AVAudioSession sharedInstance] setActive:YES error:&error];
- if (error) {
- NSLog(@"------ error desc %@", error.description);
- }
- }
- - (void)handleRouteChange:(NSNotification *)notification {
- NSDictionary *info = notification.userInfo;
- AVAudioSessionRouteChangeReason reason = [info[AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue];
- if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
- // 耳机拔出时暂停音频
- if (self.nodePlayer.isPlaying) {
- NSError *error = nil;
- [self sendInterruptError:error];
- }
-
- } else if (reason == AVAudioSessionRouteChangeReasonNewDeviceAvailable) {
- // 耳机插入时恢复音频
- if (self.nodePlayer.isPlaying) {
- NSError *error = nil;
- [self sendInterruptError:error];
- }
- }
- }
- - (void)sendInterruptError:(NSError *)error {
- if (self.delegate && [self.delegate respondsToSelector:@selector(enginePlayerDidError:error:)]) {
- [self.delegate enginePlayerDidError:self error:error];
- }
- }
- #pragma mark ------ play action
- - (void)changeRecordDelay:(NSInteger)delayMs {
- self.offsetTime = delayMs;
- }
- - (void)seekToTimePlay:(NSInteger)time {
- if (self.audioEngine.isRunning == NO) {
- [self startEngine];
- }
- if (self.audioEngine.isRunning) {
- [self seekAudioWithStartTime:time needPlay:YES];
- }
- else {
- [self sendInterruptError:nil];
- }
- }
- - (void)stopPlay {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- if (self.nodePlayer.isPlaying) {
- [self.nodePlayer stop];
- }
- [self stopTimer];
- if (self.audioEngine.isRunning) {
- [self.audioEngine stop];
- }
- });
- }
- - (void)seekToTime:(NSInteger)time {
- if (self.audioEngine.isRunning == NO) {
- [self startEngine];
- }
- if (self.audioEngine.isRunning) {
- [self seekAudioWithStartTime:time needPlay:NO];
- }
- }
- - (void)seekAudioWithStartTime:(NSTimeInterval)startTime needPlay:(BOOL)needPlay {
- if (self.audioEngine.isRunning == NO) {
- [self startEngine];
- }
- if (self.audioEngine.isRunning) {
- if (self.nodePlayer.isPlaying) {
- [self.nodePlayer stop];
- }
- }
-
- AVAudioFramePosition startFrame = startTime / 1000.0 * self.audioFormat.sampleRate;
- // 跳转进度
- self.currentFrame = startFrame;
- [self scheduleBufferFromPosition:startFrame];
- if (needPlay) {
- [self.nodePlayer play];
- [self startTimer];
- }
- }
- // 调整偏移
- - (void)seekOffsetTime:(NSInteger)offsetTime {
- self.offsetTime = offsetTime;
- NSTimeInterval currentTime = [self getCurrentPlayTime];
- [self seekToTimePlay:currentTime];
- }
- - (void)freePlayer {
- self.isCanceled = YES;
- [self stopPlay];
- }
- - (void)startTimer {
- self.timeCount = 0;
- [self.timer setFireDate:[NSDate distantPast]];
- }
- - (void)stopTimer {
-
- [self.timer setFireDate:[NSDate distantFuture]];//暂停计时器
- }
- #pragma mark ---- lazying
- - (dispatch_queue_t)sourceQueue {
- if (!_sourceQueue) {
- _sourceQueue = dispatch_queue_create("ks_MutilSourceQueue", DISPATCH_QUEUE_SERIAL);
- }
- return _sourceQueue;
- }
- - (NSTimer *)timer {
-
- if (!_timer) {
- __weak typeof(self)weakSelf = self;
- _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 repeats:YES block:^(NSTimer * _Nonnull timer) {
- [weakSelf timeFunction];
- }];
- [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
- [_timer setFireDate:[NSDate distantFuture]];
- }
- return _timer;
- }
- - (void)timeFunction {
- if (self.isCanceled) {
- return;
- }
- self.totalDuration = [self getTotalTime];
- NSTimeInterval currentTime = [self getCurrentPlayTime];
- float progress = currentTime/self.totalDuration;
- NSDate *date = [NSDate date];
- NSTimeInterval inteveral = [date timeIntervalSince1970];
- if (currentTime >= self.totalDuration) {
- if (self.delegate && [self.delegate respondsToSelector:@selector(enginePlayFinished:)]) {
- [self.delegate enginePlayFinished:self];
- }
- }
- else {
- // 定时器10ms出触发一次 buffer每100ms执行一次
- if (self.timeCount % 10 == 0) {
- [self scheduleMixBuffer];
- }
- self.timeCount++;
- if (self.delegate && [self.delegate respondsToSelector:@selector(updatePlayProgress:andTotalTime:andProgress:currentInterval:inPlayer:)]) {
- [self.delegate updatePlayProgress:currentTime andTotalTime:self.totalDuration andProgress:progress currentInterval:inteveral*1000 inPlayer:self];
- }
- }
-
- }
- - (void)scheduleMixBuffer {
- if (self.nodePlayer.isPlaying) {
- [self prepareBuffer:self.currentFrame offset:self.offsetTime mixStart:self.startPosition];
- }
- }
- - (NSTimeInterval)getCurrentPlayTime {
- AVAudioTime *nodeTime = [self.nodePlayer lastRenderTime];
- if (nodeTime && self.bgAudioFile) {
- AVAudioTime *playerTime = [self.nodePlayer playerTimeForNodeTime:nodeTime];
- AVAudioFramePosition currentFrame = self.currentFrame;
-
- if (playerTime) {
- self.sampleRate = [playerTime sampleRate];
- AVAudioFramePosition currentFrame = [playerTime sampleTime];
- if (currentFrame <= 0) {
- currentFrame = 0;
- }
- currentFrame += self.startPosition;
- self.currentFrame = currentFrame;
- }
- else {
- NSLog(@"播放已停止");
- }
- double elapsedSamples = (double)currentFrame;
- NSTimeInterval currentTime = elapsedSamples / self.sampleRate;
- // NSLog(@"当前时间----- %f",currentTime*1000);
- return currentTime*1000;
- }
- else {
- return 0;
- }
- }
- - (NSTimeInterval)getTotalTime {
- NSTimeInterval recordTotalDuration = (AVAudioFramePosition)self.audioFile.length * 1000.0 / self.audioFormat.sampleRate;
- return recordTotalDuration;
- }
- - (BOOL)isPlaying {
- if (self.nodePlayer) {
- return self.nodePlayer.isPlaying;
- }
- return NO;
- }
- - (void)dealloc {
- NSLog(@"---- KSMergeEnginePlayer dealloc");
- if (_audioEngine) {
- [_audioEngine disconnectNodeInput:self.nodePlayer];
- [_audioEngine detachNode:self.nodePlayer];
- }
- // 停止并清理定时器
- if (_timer) {
- [_timer invalidate];
- _timer = nil;
- }
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- @end
|