2017-01-27 2 views
0

AVAudioPlayerの代わりにAVCueuePlayerを使用するようにオーディオプレーヤーアプリケーションを再調整しています。再生用にオーディオファイルをキューに入れる必要があることに感謝します。 AVQueuePlayerはAVPlayerのサブクラスなので、AVTimeIndeemの時間プロパティ(duration、currentTimeなど)を記録するためにCMTimeを使用しています。私は、AudioPlayerビューのスライダとUILabelsを更新するアニメーション関数を書き直さなければなりませんでした。AVPlayerItem CurrentTimeを秒単位で再生する矛盾した値を返します

私は、TimeIntervalsの代わりにCMTime構造体を使用しているので、私のcurrentSeconds変数は、オーディオファイルの再生中に奇妙で矛盾した値を返すことがわかりました。私が含まれているコードの下に印刷結果が表示されます。

これはまた、トラックのスライダーがさまざまなスポットに影響を与え、再生時間の半分が00:00を含む奇数値にジャンプします。

私はここで何が起こっているのか新しく、addTimeObserver関数で間隔値を変更しようとしましたが、何も変更されていないと考えました。これは0.1秒ごとにanimateFooterView()を起動するようにスケジュールしたTimerオブジェクトの問題かもしれませんが、どのように対処するかはわかりません。 currentSecondsの

func animateFooterView(){ 

    let track = audioQueuePlayer?.currentItem 
    let duration: CMTime = (track?.asset.duration)! 
    let durationSeconds: Float64 = CMTimeGetSeconds(duration) 
    let currentSeconds: Float64 = CMTimeGetSeconds(track!.currentTime()) 

    audioQueuePlayer?.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 10), queue: DispatchQueue.main, using: 
     { (CMTime) -> Void in 
      if track?.status == .readyToPlay{ 

       let trackProgress: Float = Float(currentSeconds/durationSeconds) //* (self.footerView.trackSlider.maximumValue) 

       print("Progress in seconds: " + String(currentSeconds)) 

       self.footerView.trackSlider.value = trackProgress 

       let minutesProgress = Int(currentSeconds)/60 % 60 
       let secondsProgress = Int(currentSeconds) % 60 

       let minutesDuration = Int(durationSeconds)/60 % 60 
       let secondsDuration = Int(durationSeconds) % 60 

       self.footerView.durationLabel.text = String(format: "%02d:%02d | %02d:%02d", minutesProgress, secondsProgress, minutesDuration, secondsDuration) 
      } 
     }) 
} 

印刷()出力:

Progress in seconds: 0.959150266 
Progress in seconds: 0.459939791 
Progress in seconds: 0.739364115 
Progress in seconds: 0.0 
Progress in seconds: 0.0 
Progress in seconds: 0.253904687 
Progress in seconds: 0.0 
Progress in seconds: 0.15722927 
Progress in seconds: 0.346783126 
Progress in seconds: 0.050562017 
Progress in seconds: 1.158813019 
Progress in seconds: 1.252108811 
Progress in seconds: 1.356793865 
Progress in seconds: 1.458337661 
Progress in seconds: 1.554249846 
Progress in seconds: 1.615820093 
Progress in seconds: 0.0 
Progress in seconds: 0.0 
Progress in seconds: 0.0 
Progress in seconds: 0.050562017 
Progress in seconds: 0.15722927 
Progress in seconds: 0.253904687 
Progress in seconds: 0.346783126 
Progress in seconds: 0.459939791 
Progress in seconds: 0.558436703 
Progress in seconds: 0.656613436 
Progress in seconds: 0.739364115 
Progress in seconds: 0.854647401 
Progress in seconds: 0.959150266 
Progress in seconds: 1.057932049 
Progress in seconds: 1.158813019 

ここfooterViewがプレイするたびごとに0.1秒をanimateFooterView呼び出し、私が言及したタイマー、のよう垣間見るです。

func play() { 

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

実は、私はちょうど私がperiodicTimeObserverタイマー焼成による0.1秒ごとに追加している...ここで間違って何が起こっているのか実現と思います。私はそのオブザーバーを一度追加してから、タイマーを一括して取り除く必要があると思いますか?

答えて

0

私の仮定では、AVPlayerにオブザーバを継続的に追加するタイマーが、返された現在の時刻を乱していたということは間違いありませんでした。私はタイマーを削除し、代わりに、オブザーバー関数が現在の時刻を更新するようにしました。それは今完璧に動作します。

関連する問題