解决了:我是一个完全白痴。我以前使用两个按钮(播放和暂停)并隐藏它们而不是更改一个按钮的图像。我愚蠢地忽略了这行代码:playButton.hidden = YES
。
我正在构建自定义MPMoviePlayerController
控件。我想在pause.png
时将按钮的图像设置为MPMoviePlaybackState == MPMoviePlaybackStatePlaying
。否则,它应设置为play.png
。此语句在同一方法中每半秒运行一次currentPlaybackTime
设置为UILabel
。
由于某种原因,它不会设置为pause.png
。按钮就消失了。虽然,条件正确记录Playing
和not 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];
}
}
答案 0 :(得分:0)
一种可能性是你没有将按钮连接到文件所有者如果你是从xib创建按钮试试这个并玩得开心:
- (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];
}
}
答案 1 :(得分:0)
您应该尝试使用setBackground:
方法。
[playButton setBackgroundImage:pauseBtnBG forState:UIControlStateNormal];