2011-11-18 33 views

Trả lời

115

Bạn có thể truy cập vào mục hiện chơi bằng cách sử dụng currentItem tài sản:

AVPlayerItem *currentItem = yourAVPlayer.currentItem; 

Sau đó, bạn có thể dễ dàng có được thời gian yêu cầu đánh giá cao

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

sự re cũng là phương thức thuận tiện CMTimeGetSeconds để nhận NSTimeInterval: Thời lượng NSTimeInterval = CMTimeGetSeconds (currentItem.duration); NSTimeInterval currentTime = CMTimeGetSeconds (currentItem.currentTime); – slamor

+0

tôi đã phải thiết lập gọi nó trong hàng đợi chính cho nó để trả về giá trị đúng (hoặc bất kỳ hàng đợi avplayer của bạn đang chạy trong). –

+0

cách gửi khuyến khích này cho 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

tôi đang phát bài hát từ url. nhưng tại thời gian CMTime và thời gian hiện tại CMTime, tôi nhận được 0. vấn đề ở đây cho 0 giá trị là gì? Có ai biết cách giải quyết không ? – Moxarth

3

Swift:

let currentItem = yourAVPlayer.currentItem 

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

lấy CMTime từ .currentItem.asset.duration mất 3-4 giây? Cách giải quyết vấn đề này? Tôi vẫn còn sử dụng thread nhưng không được giải quyết? –

6

Với Swift 2.0, hãy sử dụng tính năng này;

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

Tôi đã cố gắng sử dụng AVPlayer.currentTime(), nhưng không may mắn, nhận được lỗi truy cập kém – KavyaKavita

+0

lấy CMTime từ .currentItem.asset.duration mất 3-4 giây? Cách giải quyết vấn đề này? Tôi vẫn còn sử dụng thread nhưng không được giải quyết? –

6

Swift 3

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

Bạn có thể lấy giây của thời gian hiện tại của bạn bằng cách truy cập seconds tài sản của currentTime(). Điều này sẽ trả về một số Double đại diện cho giây trong thời gian. Sau đó, bạn có thể sử dụng giá trị này để tạo thời gian có thể đọc được để trình bày cho người dùng của mình.

Thứ nhất, bao gồm một phương pháp để trả lại các biến thời gian cho H:mm:ss rằng bạn sẽ hiển thị cho người dùng:

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) 
} 

Tiếp theo, một phương pháp mà sẽ chuyển đổi các giá trị bạn lấy ở trên vào một chuỗi có thể đọc được:

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 
} 

Bây giờ, cập nhật giao diện người dùng với các tính toán theo thời gian:

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) 
    } 
} 
Các vấn đề liên quan