本文介绍了在AVPlayer中获取averagePowerForChannel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 AVPlayer 中获取 averagePowerForChannel ,以便在我的音乐应用上制作音频可视化效果!
我已经完成了可视化部分,但我已经停留在它的引擎(实时音量通道)。

how can i get the averagePowerForChannel in AVPlayer in order to make an audio visualization on my music app! ive already done the visualization part but im stuck in its engine (realtime volume channel).

我知道通过使用 AVAudioPlayer 可以使用 .meteringEnabled 属性轻松完成,但出于某些已知原因 AVPlayer 是一个必须在我的应用程序!
我实际上想到使用 AVAudioPlayer 除了 AVPlayer 以获得理想的结果但听起来有点凌乱解决方法,
如何影响性能和稳定性?
提前感谢

i know that by using AVAudioPlayer it can be done easily using the .meteringEnabled Property but for some known reason AVPlayer is a must in my app!im actualy thinking of using AVAudioPlayer Alongside with AVPlayer to get the desired result but it sounds kind of messy workaround,how can that affect performance and stability?thanks in advance

推荐答案

您需要一个音频处理器类与AV Foundation结合使用,以便可视化音频样本将Core Audio音频单元效果(带通滤波器)应用于音频数据。你可以在这里找到

You will need an audio processor class in combination with AV Foundation to visualize audio samples as well as applying a Core Audio audio unit effect (Bandpass Filter) to the audio data. You can find a sample by Apple here

基本上你需要为AVPlayer添加一个观察者,如下所示:

Essentially you will need to add an observer to you AVPlayer like the below:

// Notifications
let playerItem: AVPlayerItem!  = videoPlayer.currentItem
playerItem.addObserver(self, forKeyPath: "tracks", options: NSKeyValueObservingOptions.New, context:  nil);

NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: videoPlayer.currentItem, queue: NSOperationQueue.mainQueue(), usingBlock: { (notif: NSNotification) -> Void in
   self.videoPlayer.seekToTime(kCMTimeZero)
   self.videoPlayer.play()
   print("replay")
})

然后在下面的overriden方法中处理通知:

Then handle the notification in the overriden method below:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if (object === videoPlayer.currentItem && keyPath == "tracks"){
        if let playerItem: AVPlayerItem = videoPlayer.currentItem {
            if let tracks = playerItem.asset.tracks as? [AVAssetTrack] {
                tapProcessor = MYAudioTapProcessor(AVPlayerItem: playerItem)
                playerItem.audioMix = tapProcessor.audioMix
                tapProcessor.delegate = self
            }
        }
    }
}

这是

这篇关于在AVPlayer中获取averagePowerForChannel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 15:33