2013-02-26 27 views
13

Làm cách nào để thêm 5 giây vào Thời gian phát hiện tại?
Trên thực tế đây là mã của tôi:AVPlayer - Thêm giây vào CMTime

CMTime currentTime = music.currentTime; 

tôi không thể sử dụng CMTimeGetSeconds(), vì tôi cần định dạng CMTime.

Cảm ơn câu trả lời của bạn ...

EDIT: Làm thế nào tôi có thể đặt biến cho CMTime?

Trả lời

21

Dưới đây là một cách:

CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) + 5, music.currentTime.timescale); 
+0

Hoàn hảo !!! Cảm ơn bạn :) –

+0

Khi tôi thay thế '+ 5' bằng '- 5' nó không hoạt động ... Bạn có biết sai không? –

+0

Thời gian hiện tại tính bằng giây? Có lẽ con số này đi tiêu cực? – BlueVoodoo

18

cách thanh lịch đang sử dụng CMTimeAdd

CMTime currentTime = music.currentTime; 
CMTime timeToAdd = CMTimeMakeWithSeconds(5,1); 

CMTime resultTime = CMTimeAdd(currentTime,timeToAdd); 

//then hopefully 
[music seekToTime:resultTime]; 

để chỉnh sửa của bạn: bạn có thể tạo CMTime struct bằng những cách

CMTimeMake 
CMTimeMakeFromDictionary 
CMTimeMakeWithEpoch 
CMTimeMakeWithSeconds 

hơn @ : https://developer.apple.com/library/mac/#documentation/CoreMedia/Reference/CMTime/Reference/reference.html

+0

'AVPlayer' có thể không phản hồi 'setCurrentTime' –

+0

Gửi 'CMTime' đến tham số của loại không tương thích 'NSTimeInterval' (aka 'double') –

+0

có thể bạn nên sử dụng [music seekToTime: resultTime] – tomasgatial

2

Trong Swift:

private extension CMTime { 

    func timeWithOffset(offset: NSTimeInterval) -> CMTime { 

     let seconds = CMTimeGetSeconds(self) 
     let secondsWithOffset = seconds + offset 

     return CMTimeMakeWithSeconds(secondsWithOffset, timescale) 

    } 

} 
Các vấn đề liên quan