为什么我的 UISlider 更新功能无法在 MPMusicPlayerController 中设置当前播放时间?

发布于 2024-12-01 09:43:42 字数 2402 浏览 0 评论 0原文

设置

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

self.musicPlayer[MPMusicPlayerController applicationMusicPlayer]

self.currentlyPlayingTotalDurationNSNumber

self.currentlyPlayingTimeSlider< /code> 是一个 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]];
}

另外,我在 Interface Builder 中设置的 valueChanged 事件调用的方法如下。它也无法正常工作

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

任何人都可以帮我找出我做错了什么吗?它必须是最后两种方法中的某些内容,因为第一个方法调用很好。我没有在其他地方对滑块进行其他自定义/混乱。

Setup

I'm currently developing a music app and I'm having problems with the progress bar slider.

self.musicPlayer is the [MPMusicPlayerController applicationMusicPlayer]

self.currentlyPlayingTotalDuration is an NSNumber

self.currentlyPlayingTimeSlider is a UISlider


Overview

The below code is executed when a new song is picked in the MPMusicPlayerController. In this method (not all of which is shown), I set my music player to play the picked song (working) and play it (working). Then I set the total duration of the song to the maximum value of the slider (working)

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

I've logged the values to see if the values are setting correctly (they are)

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

Log output is below, looking as it should

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

And then I set my currentlyPlayingTimeSlider value to 0 to start (working)

self.currentlyPlayingTimeSlider.value = 0.0;

Next, I set my updateSliderTime method to call once per second to move the progress bar farther by one second (working)

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

Problems

The full method code is below for the updateSliderTime method, which is not working, even though it is being called every second

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

Also, the method I call for a valueChanged event that I've setup in Interface Builder is below. It also is not working

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

Can anyone help me figure out what I'm doing wrong? It has to be something in these last two methods, as the first one calls fine. I do no other customization/messing with the slider anywhere else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云巢 2024-12-08 09:43:42

您是否检查过您的 IBAction 正在被调用(可能使用 NSLog)?如果没有,请确保设置滑块的委托。

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

Did you check your IBAction is being called (perhaps with NSLog)? If not, make sure you set the delegate of the Slider.

Otherwise, check my answer to this question which is very similar.

心作怪 2024-12-08 09:43:42

想通了。除了设置滑块的最大值之外,一切实际上都按其应有的方式工作。我奇怪地转换了它。而不是:

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

我应该使用:

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

该值已经是一个数字。感谢 Mundi 激励我记录这个值。当我这样做时,我意识到它已经是一个漂浮物了。愚蠢的我:)

Figured it out. Everything was actually working how it should except setting the maximum value for the slider. I converted it oddly. Instead of:

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

I should be using:

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

The value is already a number. Thanks to Mundi for motivating me to log the value. When I did, I realized it was already a float. Silly me :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文