Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何获取使用MPMusicLayerController播放歌曲的剩余时间?_Ios_Mpmusicplayercontroller - Fatal编程技术网

Ios 如何获取使用MPMusicLayerController播放歌曲的剩余时间?

Ios 如何获取使用MPMusicLayerController播放歌曲的剩余时间?,ios,mpmusicplayercontroller,Ios,Mpmusicplayercontroller,我想在我的应用程序中显示播放歌曲的剩余时间,我的代码: NSTimeInterval currentTime= musicPlayer.currentPlaybackTime; NSNumber *duration = [musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration]; NSLog(@"Current Playback Time: %f",currentTime); 如果要重现

我想在我的应用程序中显示播放歌曲的剩余时间,我的代码:

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];
}
  • 如果您想在进度视图中显示,请遵循以下代码。这将对您有所帮助

持续时间-[currentTime integerValue]
 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];

}