Play Button is showing and when i hit play button it is playing audio file and turning the play button into pause button but when i hit pause button to pause it. It is not pausing but playing again the audiofile from the begining. After pause button is pressed not showing play button again
播放按钮正在显示,当我点击播放按钮时,它正在播放音频文件并将播放按钮变为暂停按钮,但是当我按下暂停按钮暂停播放按钮时。这不是暂停,而是从一开始就再次播放音频。按下暂停按钮后不再显示播放按钮
UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:@selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 50, 50);
[playpauseButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *playpause = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];
-(void)playpauseAction:(id)sender
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme"
ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
[fileURL release];
if
([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[audioPlayer pause];
} else {
[sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
}
}
How to make pause button work and show play button again once pause is pressed.
按下暂停后,如何使暂停按钮工作并再次显示播放按钮。
Thanks for help.
感谢帮助。
1 个解决方案
#1
1
You are creating a new instance of AVAudioPlayer for each time you press the button. What you should do is work with a shared instance and save the pointer in audioPlayer.
每按一次按钮,您就会创建一个AVAudioPlayer的新实例。你应该做的是使用共享实例并将指针保存在audioPlayer中。
#1
1
You are creating a new instance of AVAudioPlayer for each time you press the button. What you should do is work with a shared instance and save the pointer in audioPlayer.
每按一次按钮,您就会创建一个AVAudioPlayer的新实例。你应该做的是使用共享实例并将指针保存在audioPlayer中。