gpt4 book ai didi

iphone - AVPlayer - 快退/快进流

转载 作者:可可西里 更新时间:2023-11-01 06:23:30 24 4
gpt4 key购买 nike

这是我在 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 秒?

最佳答案

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

}

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

关于iphone - AVPlayer - 快退/快进流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15094948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com