2011-11-18 95 views

回答

115

您可以通過使用currentItem屬性訪問當前播放的項目:

AVPlayerItem *currentItem = yourAVPlayer.currentItem; 

然後你就可以很容易地得到所要求的時間值

CMTime duration = currentItem.duration; //total time 
CMTime currentTime = currentItem.currentTime; //playing time 
+9

的重新也方便的方法CMTimeGetSeconds來獲得NSTimeInterval: NSTimeInterval duration = CMTimeGetSeconds(currentItem.duration); NSTimeInterval currentTime = CMTimeGetSeconds(currentItem.currentTime); – slamor

+0

我不得不在主隊列中調用它來返回正確的值(或者你的avplayer運行的任何隊列)。 –

+0

如何把這個推薦給LABEL? –

20
_audioPlayer = [self playerWithAudio:_audio]; 
_observer = 
[_audioPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 2) 
              queue:dispatch_get_main_queue() 
             usingBlock:^(CMTime time) 
             { 
              _progress = CMTimeGetSeconds(time); 
             }]; 
5
 AVPlayerItem *currentItem = player.currentItem; 
    NSTimeInterval currentTime = CMTimeGetSeconds(currentItem.currentTime); 
    NSLog(@" Capturing Time :%f ",currentTime); 
+0

我正在從URL播放歌曲。但在CMTime持續時間和CMTime currentTime我變得0。這裏的0值有什麼問題?有誰知道解決方案? – Moxarth

3

斯威夫特:

let currentItem = yourAVPlayer.currentItem 

let duration = currentItem.asset.duration 
var currentTime = currentItem.asset.currentTime 
+0

從.currentItem.asset.duration獲取CMTime需要3-4秒?如何解決這個問題?我仍然使用線程,但沒有解決? –

6

用Swift 2.0,用這個;

let currentPlayerItem = AVPlayer.currentItem 
let duration = currentPlayerItem?.asset.duration 
var currentTime = AVPlayer.currentTime() 
+0

我試圖使用AVPlayer.currentTime(),但沒有運氣,得到錯誤訪問錯誤 – KavyaKavita

+0

從.currentItem.asset.duration獲取CMTime需要3-4秒?如何解決這個問題?我仍然使用線程,但沒有解決? –

6

斯威夫特3

let currentTime:Double = player.currentItem.currentTime().seconds 

您可以通過訪問currentTime()seconds財產得到您的當前時間的秒數。這將返回代表時間秒數的Double。然後你可以使用這個值來構建一個可讀的時間來呈現給你的用戶。

首先,包括對返回的時間變量H:mm:ss,你將顯示給用戶的方法:

func getHoursMinutesSecondsFrom(seconds: Double) -> (hours: Int, minutes: Int, seconds: Int) { 
    let secs = Int(seconds) 
    let hours = secs/3600 
    let minutes = (secs % 3600)/60 
    let seconds = (secs % 3600) % 60 
    return (hours, minutes, seconds) 
} 

接下來,將你上述檢索的值轉換成可讀的字符串方法:

func formatTimeFor(seconds: Double) -> String { 
    let result = getHoursMinutesSecondsFrom(seconds: seconds) 
    let hoursString = "\(result.hours)" 
    var minutesString = "\(result.minutes)" 
    if minutesString.characters.count == 1 { 
     minutesString = "0\(result.minutes)" 
    } 
    var secondsString = "\(result.seconds)" 
    if secondsString.characters.count == 1 { 
     secondsString = "0\(result.seconds)" 
    } 
    var time = "\(hoursString):" 
    if result.hours >= 1 { 
     time.append("\(minutesString):\(secondsString)") 
    } 
    else { 
     time = "\(minutesString):\(secondsString)" 
    } 
    return time 
} 

現在,更新與之前計算的UI:

func updateTime() { 
    // Access current item 
    if let currentItem = player.currentItem { 
     // Get the current time in seconds 
     let playhead = currentItem.currentTime().seconds 
     let duration = currentItem.duration.seconds 
     // Format seconds for human readable string 
     playheadLabel.text = formatTimeFor(seconds: playhead) 
     durationLabel.text = formatTimeFor(seconds: duration) 
    } 
} 
相關問題