AVPlayer Live流如何获得音频电平计量的功率
我试图在我的应用程序中显示一个米图表,它使用AVPlayer来传输实时音频流。
我知道AVAudioPlayer有一种方法: 尝试理解AVAudioPlayer和音频电平测量
它使用peakPowerForChannel
但AVAudioPlayer不适用于音频流。
AVPlayer有类似的东西吗? 或者还有其他方法可以从AVPlayer获得功率值吗?
码:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; if (self.avplayer) { [self.avplayer replaceCurrentItemWithPlayerItem:nil]; } AVURLAsset *avAsset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:@"http://broadcast.infomaniak.net/radionova-high.mp3"] options:nil]; NSArray *keys = @[@"playable"]; [avAsset loadValuesAsynchronouslyForKeys:keys completionHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ AVPlayerItem *newItem = [[AVPlayerItem alloc] initWithAsset:avAsset]; if (!self.avplayer) { self.avplayer = [[AVPlayer alloc] initWithPlayerItem:newItem]; } else { [self.avplayer replaceCurrentItemWithPlayerItem:newItem]; } [self.avplayer addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil]; [self.avplayer addObserver:self forKeyPath:@"rate" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil]; }); }]; - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"%@ called",keyPath); if ( [keyPath isEqualToString:@"status"]) { [self.avplayer play]; } else if ( [keyPath isEqualToString:@"rate"]) { if (self.avplayer.rate) { NSLog(@"Playing..."); [[NSNotificationCenter defaultCenter] postNotificationName:@"currentPlayingChangedToPlay" object:nil]; } else { NSLog(@"Not playing..."); [[NSNotificationCenter defaultCenter] postNotificationName:@"currentPlayingChangedToPause" object:nil]; } } }
通常,您可以通过播放器项目上的MTAudioProcessingTap
AVPlayer
的音频样本,但是您的资产可以通过
-
AVURLAsset
上的tracks
属性永远不可用 - 当你观察并将水龙头连接到你的
playerItem.tracks
的assetTrack
时没有回assetTrack
。
我从来没有看到为什么这对远程mp3
或m3u8
流不起作用的好解释,而且令人讨厌的是,它确实适用于其他远程资产类型,如aac
。 不公平地,在video方面, AVPlayerItemVideoOutput似乎适用于所有类型的资产(除了DRM问题)!
那么这会让你离开? 一种方法是从您可以使用AudioQueue
进行解码和播放的位置自己流式传输mp3数据,并使用前面提到的MTAudioProcessingTap
计算峰值功率。 我们AVAudioEngine
想到涉及AudioConverter
/ AVAudioConverter
和AVAudioConverter
/ AVAudioEngine
解决方案。
如果这听起来只是替换单个属性太多工作,您可以查看预先烘焙的解决方案,如StreamingKit或FreeStreamer ,然后计算功率。 您可能会找到一个为您计算能量的库。 如果没有,您可以使用此公式计算peakPowerForChannel