0

设置

我目前正在开发一个音乐应用程序,但进度条滑块出现问题。

self.musicPlayer是个[MPMusicPlayerController applicationMusicPlayer]

self.currentlyPlayingTotalDuration是一个NSNumber

self.currentlyPlayingTimeSlider是一个UISlider


概述

当在MPMusicPlayerController. 在这种方法中(未显示全部),我将我的音乐播放器设置为播放挑选的歌曲(工作)并播放它(工作)。然后我将歌曲的总持续时间设置为滑块的最大值(工作

self.currentlyPlayingTotalDuration = [[NSNumber alloc] initWithFloat:(int)[NSValue valueWithPointer:[self.musicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration]]];    
[self.currentlyPlayingTimeSlider setMaximumValue:self.currentlyPlayingTotalDuration.floatValue];

我已经记录了这些值以查看这些值是否设置正确(它们是

NSLog(@"Number:%@", [NSNumber numberWithFloat:(int)[NSValue valueWithPointer:[self.musicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration]]]);
NSLog(@"Max:%f", self.currentlyPlayingTimeSlider.maximumValue);

日志输出如下,看起来应该

2011-08-24 13:38:25.004 App [1241:707] Number:1107392
2011-08-24 13:38:25.009 App [1241:707] Max:1400544.000000

然后我将我的currentlyPlayingTimeSlider值设置为 0 开始(工作

self.currentlyPlayingTimeSlider.value = 0.0;

接下来,我将我的updateSliderTime方法设置为每秒调用一次以将进度条进一步移动一秒(工作

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSliderTime:) userInfo:nil repeats:YES];

问题

该方法的完整方法代码如下,即使它每秒被调用一次updateSliderTime,它也不起作用

- (void)updateSliderTime:(NSTimer *)timer {
    [self.currentlyPlayingTimeSlider setValue:[[NSNumber numberWithDouble:self.musicPlayer.currentPlaybackTime] floatValue]];
}

valueChanged此外,我在 Interface Builder 中设置的事件调用的方法如下。它也不起作用

- (IBAction)sliderChanged:(id)sender {
    [self.musicPlayer setCurrentPlaybackTime:[[NSNumber numberWithFloat:self.currentlyPlayingTimeSlider.value] doubleValue]];
}

谁能帮我弄清楚我做错了什么?它必须在最后两种方法中有所体现,因为第一种方法很好。我在其他任何地方都没有对滑块进行其他自定义/弄乱。

4

2 回答 2

3

您是否检查过您IBAction的呼叫(可能与NSLog)?如果没有,请确保您设置了 Slider 的委托。

否则,请检查我对这个非常相似的问题的回答。

于 2011-08-24T21:40:49.720 回答
1

弄清楚了。除了设置滑块的最大值之外,一切实际上都在按应有的方式工作。我奇怪地转换了它。代替:

self.currentlyPlayingTotalDuration = [[NSNumber alloc] initWithFloat:(int)[NSValue valueWithPointer:[self.musicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration]]];

我应该使用:

self.currentlyPlayingTotalDuration = [self.musicPlayer.nowPlayingItem valueForKey:MPMediaItemPropertyPlaybackDuration];

该值已经是一个数字。感谢 Mundi 激励我记录价值。当我这样做时,我意识到它已经是一个浮动。傻我:)

于 2011-08-24T23:58:41.853 回答