AVPlayer - 快速后向/前向流

问题描述 投票:3回答:3

这是我在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];

我的问题: 我怎样才能创建一个按钮,当按下它时,它会快速向前/快退5秒。

谢谢您的回答... :)

编辑:如何添加到我当前的时间...

CMTime currentTime = music.currentTime;

...... 5秒钟?

iphone ios xcode avplayer forward
3个回答
1
投票

使用AVPlayer方法seekToTime

AVPlayer *player=..;
 [player seekToTime:time toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];

这是一个reference

希望能帮助到你


5
投票

在斯威夫特,

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

        let time2: CMTime = CMTimeMake(Int64(newTime * 1000 as Float64), 1000)
        player.seek(to: time2)
    }
}
@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)

}

在Objective-C中,

#define seekDuration (float)5

- (IBAction)backwardButtonAction:(UIButton *)sender {
    float playerCurrentTime = [self getCurrentTime];
    float newTime = playerCurrentTime - seekDuration;

    if (newTime < 0) {
        newTime = 0;
    }
    CMTime time = CMTimeMake(newTime*1000, 1000);
    [self.player seekToTime:time completionHandler:^(BOOL finished) {
        dispatch_async(dispatch_get_main_queue(), ^{
            playerSliderisScrubbing = NO;
        });
    }];
}
- (IBAction)forwardButtonAction:(UIButton *)sender {
    float duration = [self getPlayerDuration];
    float playerCurrentTime = [self getCurrentTime];
    float newTime = playerCurrentTime + seekDuration;

    if (newTime < duration) {
        CMTime time = CMTimeMake(newTime*1000, 1000);
        [self.player seekToTime:time completionHandler:^(BOOL finished) {
            dispatch_async(dispatch_get_main_queue(), ^{
                playerSliderisScrubbing = NO;
            });
        }];

    }
}
- (float)getCurrentTime {
    float seconds = 0;
    if (_player) {
        seconds = CMTimeGetSeconds([_player currentTime]);
    }
    return seconds;
}
- (float)getPlayerDuration {
    float seconds = 0;
    if (_player) {
        seconds = CMTimeGetSeconds([[_player currentItem] duration]);
    }
    return seconds;
}

1
投票

您可以使用avPlayer.rate快进/快进

费率:1.0正常0.0暂停

但首先,您应该检查avPlayerItem是否可以执行您的操作之一

欲了解更多信息:https://developer.apple.com/LIBRARY/ios/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/instp/AVPlayer/rate

© www.soinside.com 2019 - 2024. All rights reserved.