NSDate 和 AVPlayerItem.currentTime
NSDate and AVPlayerItem.currentTime
我知道有很多话题,但我找不到解决问题的方法。
我有一个 AVPlayerItem,我希望将 currentTime-属性 (CMTime) 转换为我的音乐播放器可读的格式。
这是我的代码:
NSDate *seconds = [[NSDate alloc] initWithTimeIntervalSinceNow:CMTimeGetSeconds(self.playerItem.currentTime)];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"HH:mm:ss"];
self.currentTimeLabel.text = [NSString stringWithFormat:@"%@", [timeFormatter stringFromDate:seconds]];
它有效,但它会增加 1 小时的播放时间。我怎样才能减去那个小时?
谢谢!
试试这个代码,
swift代码:
声明
var playerVal = AVPlayer()
然后在你想要的地方调用下面的方法,
func updateTime() {
let currentTime = Float(CMTimeGetSeconds(playerVal.currentTime()))
let minutes = currentTime/60
let seconds = currentTime - minutes * 60
startValue.text = NSString(format: "%.2f:%.2f", minutes,seconds) as String
}
objective-C代码:
声明
AVPlayer playerVal;
然后在你想要的地方调用下面的方法,
- (void)updateTime {
Float currentTime = CMTimeGetSeconds([playerVal currentTime]);
Float minutes = currentTime/60;
Float seconds = (currentTime - minutes) * 60;
startValue.text = [NSString stringWithFormat:@"%.2f:%.2f", minutes,seconds];
}
对我有用,希望对你有帮助
谢谢,我稍微细化了一下:
float currentTime = CMTimeGetSeconds([self.audioStreamPlayer currentTime]);
int seconds = (int) currentTime;
int minutes = (int) currentTime/60;
int hours = (int) ((currentTime/60)/60);
NSString *hoursString = [NSString stringWithFormat:@"%d",hours];
NSString *minutesString = [NSString stringWithFormat:@"%d",minutes];
NSString *secondsString = [NSString stringWithFormat:@"%d",seconds % 60];
if (hoursString.length == 1) {
hoursString = [NSString stringWithFormat:@"0%d", hours];
}
if (minutesString.length == 1) {
minutesString = [NSString stringWithFormat:@"0%d",minutes];
}
if (secondsString.length == 1) {
secondsString = [NSString stringWithFormat:@"0%d", seconds % 60];
}
self.currentTimeLabel.text = [NSString stringWithFormat:@"%@:%@:%@", hoursString, minutesString, secondsString];
有效!
我知道有很多话题,但我找不到解决问题的方法。
我有一个 AVPlayerItem,我希望将 currentTime-属性 (CMTime) 转换为我的音乐播放器可读的格式。
这是我的代码:
NSDate *seconds = [[NSDate alloc] initWithTimeIntervalSinceNow:CMTimeGetSeconds(self.playerItem.currentTime)];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"HH:mm:ss"];
self.currentTimeLabel.text = [NSString stringWithFormat:@"%@", [timeFormatter stringFromDate:seconds]];
它有效,但它会增加 1 小时的播放时间。我怎样才能减去那个小时?
谢谢!
试试这个代码,
swift代码:
声明
var playerVal = AVPlayer()
然后在你想要的地方调用下面的方法,
func updateTime() {
let currentTime = Float(CMTimeGetSeconds(playerVal.currentTime()))
let minutes = currentTime/60
let seconds = currentTime - minutes * 60
startValue.text = NSString(format: "%.2f:%.2f", minutes,seconds) as String
}
objective-C代码:
声明
AVPlayer playerVal;
然后在你想要的地方调用下面的方法,
- (void)updateTime {
Float currentTime = CMTimeGetSeconds([playerVal currentTime]);
Float minutes = currentTime/60;
Float seconds = (currentTime - minutes) * 60;
startValue.text = [NSString stringWithFormat:@"%.2f:%.2f", minutes,seconds];
}
对我有用,希望对你有帮助
谢谢,我稍微细化了一下:
float currentTime = CMTimeGetSeconds([self.audioStreamPlayer currentTime]);
int seconds = (int) currentTime;
int minutes = (int) currentTime/60;
int hours = (int) ((currentTime/60)/60);
NSString *hoursString = [NSString stringWithFormat:@"%d",hours];
NSString *minutesString = [NSString stringWithFormat:@"%d",minutes];
NSString *secondsString = [NSString stringWithFormat:@"%d",seconds % 60];
if (hoursString.length == 1) {
hoursString = [NSString stringWithFormat:@"0%d", hours];
}
if (minutesString.length == 1) {
minutesString = [NSString stringWithFormat:@"0%d",minutes];
}
if (secondsString.length == 1) {
secondsString = [NSString stringWithFormat:@"0%d", seconds % 60];
}
self.currentTimeLabel.text = [NSString stringWithFormat:@"%@:%@:%@", hoursString, minutesString, secondsString];
有效!