基本的に、アプリにはオーディオファイルの再生ボタンがあり、別の UIButton を追加したい 再生ボタンのほかに、それが押されるたびにオーディオファイルが何度か繰り返されるか、オーディオ再生の繰り返しを停止するためにもう一度押されるまで無限になる可能性がありますファイル。どうすればこれを達成できますか。
以下は、再生/一時停止トグルボタンのコードです
_playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
_playButton.frame = CGRectMake(80, 40, 25, 25);
[_playButton setImage:[UIImage imageNamed:@"1play.png"] forState:UIControlStateNormal];
[_playButton setImage:[UIImage imageNamed:@"audiopause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:_playButton];
// Get the file path to the song to play.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Theme"ofType:@"mp3"];
// Convert the file path to a URL.
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
//Initialize the AVAudioPlayer.
player = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
player.delegate = self;
- (void)playAction:(id)sender
{
if([player isPlaying])
{
[sender setImage:[UIImage imageNamed:@"1play.png"] forState:UIControlStateSelected];
[player pause];
//[timer invalidate];
//timer = nil;
//[self pauseTimer];
}else{
[sender setImage:[UIImage imageNamed:@"audiopause.png"] forState:UIControlStateNormal];
[player play];
slidertimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateProgressBar:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:slidertimer forMode:NSRunLoopCommonModes];
timer = slidertimer;
}
}