2016-04-20 11 views
0

Ich weiß, dass es viele Themen gibt, aber ich kann keine Lösung für mein Problem finden.NSDate und AVPlayerItem.currentTime

Ich habe ein AVPlayerItem und möchte die currentTime-Eigenschaft (CMTime) in ein lesbares Format für meinen Musikplayer konvertieren.

dies ist mein Code:

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]]; 

es funktioniert, aber es fügt 1 Stunde auf die Spielzeit. Wie kann ich diese Stunde abziehen?

thx!

Antwort

3

diesen Code versuchen,

SWIFT-Code:

delclare

var playerVal = AVPlayer() 

dann unter Methode aufrufen, wo Sie wollen,

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-Code:

delclare

AVPlayer playerVal; 

dann unter Methode aufrufen, wo Sie wollen,

- (void)updateTime { 
    Float currentTime = CMTimeGetSeconds([playerVal currentTime]); 
    Float minutes = currentTime/60; 
    Float seconds = (currentTime - minutes) * 60; 
    startValue.text = [NSString stringWithFormat:@"%.2f:%.2f", minutes,seconds]; 
} 

seine für mich arbeiten, ihre hilfreich hoffen

0

thx, ich es verfeinert ein wenig:

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]; 

funktioniert!