averagePowerForChannel

averagePowerForChannel

我正在尝试获取averagePowerForChannelAVAudioPlayer以便绘制一些图形。

播放器正在工作并播放歌曲,但是averagePowerForChannel从未更新,并返回-160

我还启用了计量功能,并更新了计量表,但仍无法正常工作。

我究竟做错了什么?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

       timerAudio = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "checkTime", userInfo: nil, repeats: true)

self.audioPlayer?.meteringEnabled
       playAudio( false, SongNumber: 2)
}

     func checkTime() {

                self.audioPlayer?.updateMeters()
                self.audioPlayer?.numberOfChannels
                let normalizedValue = self.audioPlayer?.averagePowerForChannel(0)

                print(normalizedValue)
                self.waveformView.updateWithLevel(CGFloat(normalizedValue!))
        }

    func playAudio( stopAudio:Bool, SongNumber: Int) {

            let songNumber = SongsArray
            let songNumberMediaPlayer = SongsArray

            nameSong(songNumber[SongNumber])

        do {
                    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                    //print("AVAudioSession Category Playback OK")
                    do {
                        try AVAudioSession.sharedInstance().setActive(true)
                        //print("AVAudioSession is Active")
                    } catch let error as NSError {
                        print(error.localizedDescription)
                    }
                } catch let error as NSError {
                    print(error.localizedDescription)
                }

                let dispatchQueue =
                dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

                dispatch_async(dispatchQueue, {
                    let mainBundle = NSBundle.mainBundle()


                    /* Find the location of our file to feed to the audio player */
                    let filePath = mainBundle.pathForResource(songNumber[SongNumber], ofType:"mp3")

                    if let path = filePath{
                        let fileData = NSData(contentsOfFile: path)

                        do {
                            /* Start the audio player */
                            self.audioPlayer = try AVAudioPlayer(data: fileData!)

                            guard let player = self.audioPlayer else{
                                return
                            }

                            /* Set the delegate and start playing */
                            player.delegate = self
                            if player.prepareToPlay() && player.play(){
                                /* Successfully started playing */




                            } else {
                                /* Failed to play */
                            }

                        } catch{
                            self.audioPlayer = nil
                            return
                        }



                        if stopAudio == true {
                            self.audioPlayer?.pause()
                            self.timerAudio.invalidate()

                        }else {

                            self.audioPlayer?.play()

                        }


                    }

                })

            }

最佳答案

self.audioPlayer?.meteringEnabled = true

您需要激活meteringEnabled

关于audio - Swift 2-AVAudioPlayer averagePowerForChannel始终返回-160,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34011297/

10-09 09:52