0

已解决:我是个彻头彻尾的白痴。我以前使用两个按钮(播放和暂停)并隐藏它们而不是更改一个按钮的图像。我愚蠢地忽略了这行代码:playButton.hidden = YES.

我正在构建自定义控件MPMoviePlayerController。我正在尝试将按钮的图像设置为pause.pngwhen MPMoviePlaybackState == MPMoviePlaybackStatePlaying。否则,它应该设置为play.png。这条语句每半秒运行一次,同样的方法 currentPlaybackTime设置为 a UILabel

由于某种原因,它不会设置为pause.png。按钮就消失了。虽然,条件记录Playingnot playing正确。

。H

@property (retain) UIImage *playBtnBG;
@property (retain) UIImage *pauseBtnBG;

.m

- (void)viewDidLoad
{
playBtnBG = [UIImage imageNamed:@"play.png"];
pauseBtnBG = [UIImage imageNamed:@"pause.png"];
}


- (void)updatePlaybackTime:(NSTimer*)theTimer 
{
if (!sliding) {
    int playbackTime = moviePlayer.currentPlaybackTime; 

    timeLabel.text = [NSString stringWithFormat:@"%d:%02d", (playbackTime / 60), (playbackTime % 60)];
    playbackSlider.value = playbackTime;
}

if (moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {
    NSLog(@"Playing");
[playButton setImage:pauseBtnBG forState:UIControlStateNormal];
}
else {
    NSLog(@"not playing");
    [playButton setImage:playBtnBG forState:UIControlStateNormal];

}
}
4

2 回答 2

0

一种可能性是,如果您从 xibs 创建按钮,则您没有将按钮连接到文件所有者,请尝试此操作并玩得开心:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self 


                                        selector:@selector(moviePlayerPlaybackStateDidChange:) 
                                             name:MPMoviePlayerPlaybackStateDidChangeNotification 
                                           object:nil];

//myPlayer is MPMoviePlayerController declared in header
myPlayer =  [[MPMoviePlayerController alloc] initWithContentURL:myaudioUrl];
myPlayer.controlStyle = MPMovieControlStyleNone; 
[myPlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayerLoadStateChanged:) 
                                             name:MPMoviePlayerLoadStateDidChangeNotification 
                                           object:nil];
}

- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
     if ([myPlayer loadState] != MPMovieLoadStateUnknown) {
    [[NSNotificationCenter  defaultCenter] removeObserver:self 
                                                     name:MPMoviePlayerLoadStateDidChangeNotification 
                                                   object:nil];
    [myPlayer play];
}

}

-(void) moviePlayerPlaybackStateDidChange:(NSNotification*)notification {

NSLog(@"playbackDidChanged");
MPMoviePlayerController *moviePlayer = notification.object;
MPMoviePlaybackState playbackState = moviePlayer.playbackState;
if(playbackState == MPMoviePlaybackStateStopped) {
    NSLog(@"MPMoviePlaybackStateStopped");
} else if(playbackState == MPMoviePlaybackStatePlaying) {
    NSLog(@"MPMoviePlaybackStatePlaying");
 [playButton setImage:pauseBtnBG forState:UIControlStateNormal];
} else if(playbackState == MPMoviePlaybackStatePaused) {
    NSLog(@"MPMoviePlaybackStatePaused");
[playButton setImage:playBtnBG forState:UIControlStateNormal];
} 
}
于 2012-06-14T17:18:52.260 回答
0

You should probably try using the setBackground: method.

[playButton setBackgroundImage:pauseBtnBG forState:UIControlStateNormal];
于 2012-06-14T17:19:50.853 回答