簡體   English   中英

如何獲取使用MPMusicPlayerController播放的正在播放的歌曲的剩余時間?

[英]how to get the remaining time of the Playing song which is played using MPMusicPlayerController?

我想在我的應用程序中顯示播放歌曲的剩余時間,我的代碼為:

NSTimeInterval currentTime= musicPlayer.currentPlaybackTime; 
NSNumber *duration = [musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration]; 
NSLog(@"Current Playback Time: %f",currentTime);

如果要重現“音樂”應用中顯示的時間:

double nowPlayingItemDuration = [[[musicPlayer nowPlayingItem] valueForProperty:MPMediaItemPropertyPlaybackDuration]doubleValue];
double currentTime = (double) [musicPlayer currentPlaybackTime];
double remainingTime = nowPlayingItemDuration - currentTime;

NSString *timeElapsed;
NSString *timeRemaining;

if (nowPlayingItemDuration >= 3600.0) {
    timeElapsed = [NSString stringWithFormat: @"%02d:%02d:%02d",
                           (int)currentTime/3600,
                           (int) (currentTime/60)%60,
                           (int) currentTime%60];
    timeRemaining = [NSString stringWithFormat:@"- %02d:%02d:%02d",
                             (int)remainingTime/3600,
                             (int) (remainingTime/60)%60,
                             (int) remainingTime%60];

} else {
    timeElapsed = [NSString stringWithFormat: @"%02d:%02d",
                           (int) currentTime/60,
                           (int) currentTime%60];
    timeRemaining = [NSString stringWithFormat:@"- %02d:%02d",
                             (int) remainingTime/60,
                             (int) remainingTime%60];
}
  • 如果要在進度視圖中顯示該代碼,請遵循以下代碼,它將為您提供幫助。
  MPMediaItem *temp; NSTimeInterval trackLength = [[temp valueForProperty:MPMediaItemPropertyPlaybackDuration] doubleValue]; song.songDuration = [temp valueForProperty:MPMediaItemPropertyPlaybackDuration]; 
NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:1
                                                target:self
                                              selector:@selector(updateProgress)
                                              userInfo:nil
                                               repeats:YES];

-(void) updateProgress{
itemDur++;
        WASong *song = [self.songObjects objectAtIndex:self.cellIndexPath.row];

        CGFloat progress =itemDur/[song.songDuration doubleValue];

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM