2016-04-20 8 views
0

多くのトピックがあることは知っていますが、私の問題の解決策を見つけることはできません。NSDateと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時間を追加します。どのように私はその時間を引くことができますか?

thx!

答えて

3

SWIFTコード、このコードを試してみてください。

delclare

var playerVal = AVPlayer() 

その後、Objective-Cのコード

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 

    } 

、あなたが望む方法を下回る呼び出します

delclare

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

はその作業は、必要なメソッドの下に呼び出して、その有用

0

THXを願って、私はそれを少し洗練:

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

作品!