1 Reply
      Latest reply on Nov 26, 2019 1:51 AM by xinxing
      xinxing Level 1 Level 1 (0 points)

        Hi, I need to do a function to convert the audio in the video into a sound animation. I use AVPlayer to play the video, but I don't find the spectrum data that the api can extract the audio. I know that AVAudioEngine can do this, but can AVPlayer be? Please give me a hint if you can, thank you.

         

         

        /// AVAudioEngine can get amplitudes, but use AVPlayer how to get?
        class AudioSpectrumPlayer {
            private let engine = AVAudioEngine()
            private let player = AVAudioPlayerNode()
            
            private var fftSize: Int = 2048
            
            init() {
                engine.attach(player)
                engine.connect(player, to: engine.mainMixerNode, format: nil)
                engine.mainMixerNode.installTap(onBus: 0, bufferSize: AVAudioFrameCount(fftSize), format: nil, block: { [weak self](buffer, when) in
                    guard let strongSelf = self else { return }
                    if !strongSelf.player.isPlaying { return }
                    buffer.frameLength = AVAudioFrameCount(strongSelf.fftSize)
                    let amplitudes = strongSelf.fft(buffer)
                    /// get spectrum data at here
                })
                engine.prepare()
                try! engine.start()
            }
        }