我创建了两个UIButton,只有播放按钮出现在UIToolbar上。在我想要的播放方法中,当按下播放按钮时,它显示暂停按钮,如果用户按下暂停按钮,它会暂停音频文件,然后显示播放按钮。
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
playButton.frame = CGRectMake(0, 0, 50, 50);
UIImage *image = [UIImage imageNamed:@"play.png"];
[playButton setImage:image forState:UIControlStateNormal];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playButton];
UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pauseButton addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
pauseButton.frame = CGRectMake(0, 0, 50, 50);
UIImage *imge = [UIImage imageNamed:@"pause.png"];
[pauseButton setImage:imge forState:UIControlStateNormal];
-(void)play:(id)sender
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme"
ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[audioPlayer play];
[fileURL release];
}
我需要帮助来实现这个
在游戏方法中
我做了
[audioPlayer play];
UIImage *imge = [UIImage imageNamed:@"pause.png"];
[pauseButton setImage:imge forState:UIControlStateNormal];
[audioPlayer pause];
UIImage *image = [UIImage imageNamed:@"play.png"];
[playButton setImage:image forState:UIControlStateNormal];
[audioPlayer Play];
我认为我做错了方法
请建议。
感谢您的帮助。
由于
答案 0 :(得分:2)
每个州仅使用一个带有1个图像的按钮(选定和正常):
UIButton *mediaButton = [UIButton buttonWithType:UIButtonTypeCustom];
mediaButton.frame = CGRectMake(0, 0, 50, 50);
[mediaButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[mediaButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];
[playButton addTarget:self action:@selector(mediaAction:) forControlEvents:UIControlEventTouchUpInside];
-(void)mediaAction:(UIButton *)sender
{
if(sender.selected){
// pause action
} else {
// play action
}
sender.selected = !sender.selected;
}
答案 1 :(得分:1)
您不必创建两个单独的按钮。只需创建一个按钮和一个方法。只需将状态保存在布尔变量中或使用播放器状态即可。在方法中检查状态之后,做你需要做的事情。
但是,如果您坚持创建两个单独的按钮,则可以隐藏并显示其中一个按钮。
答案 2 :(得分:0)
if([audioPlayer isPlaying]) {
UIImage *imge = [UIImage imageNamed:@"pause.png"];
[pauseButton setImage:imge forState:UIControlStateNormal];
[audioPlayer pause];
}
else {
UIImage *image = [UIImage imageNamed:@"play.png"];
[playButton setImage:image forState:UIControlStateNormal];
[audioPlayer Play];
}
是对的吗?