2013-02-27 87 views
4

如何倒帶5秒流?將AVPlayer倒帶5秒

這是我在viewDidLoad中的流碼:

AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://groove.wavestreamer.com:7321/listen.pls?sid=1"]]; 

[playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil]; 

music = [[AVPlayer playerWithPlayerItem:playerItem] retain]; 
[music play]; 

music = [[AVPlayer playerWithPlayerItem:playerItem] retain]; 
[music play]; 

謝謝您的回答...

+0

的可能的複製[ AVPlayer - 快速向後/向前流](http://stackoverflow.com/questions/15094948/avplayer-fast-backward-forward-stream) – Pangu 2016-03-11 00:19:56

回答

10
- (NSTimeInterval)currentPlaybackTime 
{ 
    return CMTimeGetSeconds(self.playerView.player.currentItem.currentTime); 
} 


- (void)setCurrentPlaybackTime:(NSTimeInterval)time 
{ 
    CMTime cmTime = CMTimeMakeWithSeconds(time, NSEC_PER_SEC); 
    [self.playerView.player.currentItem seekToTime:cmTime]; 
} 


- (IBAction)scanBackward 
{ 
    self.currentPlaybackTime -= 5.0f; 
} 
+0

+1,nice and cl EAN。介紹NSEC_PER_SEC和「f」單元的意義嗎? – mkc842 2014-02-13 01:46:52

+0

關於NSEC_PER_SEC https://developer.apple.com/reference/dispatch/nsec_per_sec?language=objc和5.0上的f將它強制浮動 – norman784 2016-11-01 20:21:11

1

在斯威夫特3+,

fileprivate let seekDuration: Float64 = 5 


@IBAction func doForwardJump(_ sender: Any) { 
    guard let duration = player.currentItem?.duration else{ 
     return 
    } 
    let playerCurrentTime = CMTimeGetSeconds(player.currentTime()) 
    let newTime = playerCurrentTime + seekDuration 

    if newTime < (CMTimeGetSeconds(duration) - seekDuration) { 

     let time2: CMTime = CMTimeMake(Int64(newTime * 1000 as Float64), 1000) 
     player.seek(to: time2, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) 

    } 
} 
@IBAction func doBackwardJump(_ sender: Any) { 

    let playerCurrentTime = CMTimeGetSeconds(player.currentTime()) 
    var newTime = playerCurrentTime - seekDuration 

    if newTime < 0 { 
     newTime = 0 
    } 
    let time2: CMTime = CMTimeMake(Int64(newTime * 1000 as Float64), 1000) 
    player.seek(to: time2, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) 


}