簡體   English   中英

AVPlayer Live 流如何獲得用於音頻電平計量的電源

[英]AVPlayer Live stream how to get power for audio level metering

我試圖在我的應用程序中顯示一個儀表圖,它使用 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的音頻樣本,但是您的資產通過

  1. tracks上你的財產AVURLAsset從來沒有變得可用
  2. 當您觀察並將點擊附加到您的playerItem.tracksassetTrack時,不會給您assetTrack

對於為什么這對遠程mp3 s 或m3u8流不起作用,我從未見過很好的解釋,而且令人討厭的是,它確實適用於其他遠程資產類型,例如aac 不公平的是,在視頻方面, AVPlayerItemVideoOutput似乎適用於所有類型的資產(除非 DRM 問題)!

那么這會讓你在哪里呢? 一種方法是自己從那里你可以解碼和使用播放流的MP3數據AudioQueue ,並且計算使用上述峰值功率MTAudioProcessingTap 涉及解決方案AudioConverter / AudioUnitAVAudioConverter / AVAudioEngine腦海里浮現了。

如果僅僅替換單個屬性聽起來工作量太大,您可以查看預烘焙的解決方案,例如StreamingKitFreeStreamer ,然后計算功率。 您可能會找到一個可以為您計算功率的庫。 如果沒有,您可以使用此公式計算peakPowerForChannel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM