Rewind AVPlayer by 5 seconds
Asked Answered
D

3

16

How can I rewind a stream by 5 second?

This is my code for streaming in the 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];

Thanks for your answers...

De answered 27/2, 2013 at 13:25 Comment(1)
Possible duplicate of AVPlayer - Fast Backward / Forward StreamHalland
C
15

In Swift 3+,

fileprivate let seekDuration: Float64 = 5


@IBAction func seekForward(_ 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 seekBackward(_ 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)


}
Cm answered 19/6, 2017 at 8:12 Comment(0)
L
12

In Swift 5:

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

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

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

    }
}

@IBAction func seekBackward(_ sender: Any) {
    let playerCurrentTime = CMTimeGetSeconds(player!.currentTime())
        var newTime = playerCurrentTime - 5

        if newTime < 0 {
            newTime = 0
        }
    let time2: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
    player!.seek(to: time2, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero)
}
Lovel answered 31/1, 2020 at 2:22 Comment(0)
P
11
- (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;
}
Plantaineater answered 24/3, 2013 at 18:29 Comment(2)
+1, nice and clean. Mind explaining the significance of NSEC_PER_SEC and the "f" units?Orcein
about NSEC_PER_SEC developer.apple.com/reference/dispatch/… and the f on 5.0 its for cast it to floatProvincialism

© 2022 - 2024 — McMap. All rights reserved.