xcode AVPlayer - 快退/快进流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15094948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:51:38  来源:igfitidea点击:

AVPlayer - Fast Backward / Forward Stream

iphoneiosxcodeavplayerforward

提问by Lorenz W?hr

This is my code in the viewDidLoad :

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

My question:
How can I create a button, that fast-forwards / fast-backwards the stream 5 seconds, when it′s pressed?

我的问题:
如何创建一个按钮,在按下时快进/快退流 5 秒?

Thank you for your answers... :)

谢谢您的回答... :)

EDIT: How can I add to my current time...

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

CMTime currentTime = music.currentTime;

...the 5 seconds?

……5秒?

采纳答案by Daniel

Use AVPlayer method seekToTime

使用 AVPlayer 方法 seekToTime

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

here is a reference

这是一个参考

Hope it helps

希望能帮助到你

回答by abhimuralidharan

In Swift,

在 Swift 中

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)

}

In Objective-C,

在 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;
}

回答by Rares Mihai Preda

You can use avPlayer.rate to fast backward/forward

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

Rates: 1.0 normal 0.0 pause

费率:1.0 正常 0.0 暂停

But first, you should check if avPlayerItem can do one of your action

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

For more info: https://developer.apple.com/LIBRARY/ios/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/instp/AVPlayer/rate

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

回答by Akbar Khan

Swift 4, 4.2 & 5

斯威夫特 4、4.2 和 5

var player : AVPlayer!

@IBAction func fastForwardBtn(_ sender: UIButton) {
        let moveForword : Float64 = 5

        if player == nil { return }
        if let duration  = player!.currentItem?.duration {
        let playerCurrentTime = CMTimeGetSeconds(player!.currentTime())
        let newTime = playerCurrentTime + moveForword
        if newTime < CMTimeGetSeconds(duration)
        {
            let selectedTime: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
            player!.seek(to: selectedTime)
        }
        player?.pause()
        player?.play()
        }

    }

@IBAction func rewindBtn(_ sender: UIButton) {
            let moveBackword: Float64 = 5
            if player == nil
            {
                return
            }
            let playerCurrenTime = CMTimeGetSeconds(player!.currentTime())
            var newTime = playerCurrenTime - moveBackword
            if newTime < 0
            {
                newTime = 0
            }
            player?.pause()
            let selectedTime: CMTime = CMTimeMake(value: Int64(newTime * 1000 as Float64), timescale: 1000)
            player?.seek(to: selectedTime)
            player?.play()
        }