0

我试图将滑块设置为正在播放的音频文件的当前值。但是在设置滑块的值时我遇到了问题。

当我使用 ViewDidLoad 中的计时器时:

self.timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(PlayerViewController.audioSliderUpdate), userInfo: nil, repeats: true)

它调用我的函数:

func audioSliderUpdate() {

 var currentTime : CMTime = (self.audioPlayerItem?.currentTime())!
 var seconds : Float64 = CMTimeGetSeconds(currentTime)
 var time : Float = Float(seconds)

    self.audioSlider.value = time

}

音轨开始跳跃,滑块在跳跃,并将其返回到控制台:

[aqme] 255: AQDefaultDevice (173): skipping input stream 0 0 0x0

当我尝试使用此代码设置最大值时:

   let duration : CMTime = (self.audioPlayerItem?.duration)!
   let seconds : Float64 = CMTimeGetSeconds(duration)
   let maxTime : Float = Float(seconds)

   self.audioSlider.valueMaximum = maxTime

我收到此错误:

<Error>: Error: this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API and this value is being ignored. Please fix this problem.
Jan 20 07:24:23  ParseStarterProject-Swift[2441] <Error>: If you want to see the backtrace, please set CG_NUMERICS_SHOW_BACKTRACE environmental variable.

我已经阅读了所有内容,他们都有简单的解决方案,比如

let floatTime = Float(player.currentTime)

这是行不通的。因为你不能直接将 CMT 转换为在 swift 3 中浮动????

所以我的问题是如何设置滑块的值?

如果我的解释不清楚,这就是整个课程的亮点:

import UIKit
import Parse
import AVFoundation
import AVKit


class PlayerViewController: UIViewController, AVAudioPlayerDelegate {

@IBOutlet var audioSlider: MTCircularSlider!
// passed objectID from PackViewController
var selectedAudio: String!

var audioPlayer: AVPlayer?
var audioPlayerItem: AVPlayerItem?

fileprivate var timer: Timer?
fileprivate var direction: Float = 0.01


// query parse to get the file and stream it
func getAudio() {

    let query = PFQuery(className: "Part")
        query.whereKey("objectId", equalTo: selectedAudio)
        query.getFirstObjectInBackground { (object, error) in

            if error != nil || object == nil {

                print("The getFirstObject request failed.")

            } else {

                print("There is an object now get the Audio. ")

                if let audioFileURL = (object?.object(forKey: "partAudio") as! PFFile).url {

                        // create an item for use by notification center
                        self.audioPlayerItem = AVPlayerItem(url: NSURL(string: audioFileURL) as! URL)

                            self.audioPlayer = AVPlayer(playerItem: self.audioPlayerItem)

                        self.audioPlayer?.play()


                    var duration : CMTime = (self.audioPlayerItem?.duration)!
                    var seconds : Float64 = CMTimeGetSeconds(duration)
                    var maxTime : Float = Float(seconds)

                    self.audioSlider.valueMaximum = maxTime


                    self.timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(PlayerViewController.audioSliderUpdate), userInfo: nil, repeats: true)
                }

            }
        }
}


@IBOutlet var playerButton: UIButton!


func playerButtonTapped() {

    // if the player is not playing
    if audioPlayer?.rate == 0 {

        audioPlayer?.play()
            self.playerButton.setImage(UIImage(named: "play"), for: UIControlState.normal)

    } else {

        audioPlayer?.pause()
            self.playerButton.setImage(UIImage(named: "pause"), for: UIControlState.normal)

    }

}

func finishedPlaying (myNotification: NSNotification) {

    self.playerButton.setImage(UIImage(named: "play"), for: UIControlState.normal)

    let stoppedPlayerItem: AVPlayerItem = myNotification.object as! AVPlayerItem

        //create a CMTime for zero seconds so we can go back to the beginning
        let seconds : Int64 = 0
            let preferredTimeScale : Int32 = 1
                let seekTime : CMTime = CMTimeMake(seconds, preferredTimeScale)

        stoppedPlayerItem.seek(to: seekTime)
}


override func viewDidLoad() {
    super.viewDidLoad()

    self.playerButton.addTarget(self, action: #selector(PlayerViewController.playerButtonTapped), for: UIControlEvents.touchUpInside)

    getAudio()

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(audioPlayerItem as Any, selector: #selector(PlayerViewController.finishedPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: audioPlayer)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // remove the observer when leaving page
    NotificationCenter.default.removeObserver(audioPlayer?.currentItem! as Any)
}

@IBAction func audioSliderActioned(_ sender: Any) {

    audioPlayer?.pause()
    //audioPlayer.currentTime() = TimeInterval(audioSlider.value)
    //audioPlayer.prepareToPlay()
    audioPlayer?.play()

}

func audioSliderUpdate() {



    var currentTime : CMTime = (self.audioPlayerItem?.currentTime())!
    var seconds : Float64 = CMTimeGetSeconds(currentTime)
    var time : Float = Float(seconds)

    self.audioSlider.value = time

}

}
4

0 回答 0