使用相同按钮播放/暂停[AVAudioPlayer] [英] Play/Pause with the same button [AVAudioPlayer]
本文介绍了使用相同按钮播放/暂停[AVAudioPlayer]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过按 UI按钮
一次播放 IBAction
的声音并按下按钮暂停按钮再次使用 AVAudioPlayer
?此外,我想在播放声音时更改 UIButton
的状态。
How can I play a sound with an IBAction
by pressing on a UIbutton
once and pause it by pressing the button again using AVAudioPlayer
? Also I want to change the state of that UIButton
when the sound is playing and when it's not.
这是我的代码:
- (IBAction)Beat
{
if ([Media2 isPlaying])
{
[Media2 pause];
[Button17 setSelected:NO];
}
else
{
Path = [[NSBundle mainBundle] pathForResource:@"Beat" ofType:@"mp3"];
AVAudioPlayer *Media2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media2 setDelegate:self];
[Media2 play];
[Button17 setSelected:YES];
}
}
推荐答案
这里是是使用 BOOL
变量的简单方法。
Here's is simple method using BOOL
Variable.
设置播放=否
in viewDidLoad
。
-(void)PlayStop{
if (playing==NO) {
// Init audio with playback capability
[play setBackgroundImage:[UIImage imageNamed:@"hmpause.png"] forState:UIControlStateNormal];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:______ error:&err];
[audioPlayer prepareToPlay];
audioPlayer.delegate=self;
[audioPlayer play];
playing=YES;
}
else if(playing==YES){
[play setBackgroundImage:[UIImage imageNamed:@"Audioplay.png"] forState:UIControlStateNormal];
[audioPlayer pause];
playing=NO;
}
}
这篇关于使用相同按钮播放/暂停[AVAudioPlayer]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文