11

如何将流倒带 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];

谢谢你的回答...

4

3 回答 3

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;
}
于 2013-03-24T18:29:29.070 回答
11

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


}
于 2017-06-19T08:12:47.200 回答
7

在 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)
}
于 2020-01-31T02:22:18.740 回答