我是 Xcode 的新手,我需要一些掌握 xcode 的经验。我正在尝试使用 UIActionSheet 播放、暂停、停止音乐,但是当我点击暂停和停止按钮时,它们不会停止甚至暂停,只有播放按钮有效。有人可以帮我吗?这是我的代码。
#import <AVFoundation/AVFoundation.h>
@interface MainViewController () <UIActionSheetDelegate,AVAudioPlayerDelegate>
@property(nonatomic,retain)AVAudioPlayer *playAudio;
@end
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle =[actionSheet buttonTitleAtIndex:buttonIndex];
NSURL *url =[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/MusicName.mp3",[[NSBundle mainBundle] resourcePath]]];
NSError *error;
self.playAudio=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
if (buttonIndex==0) {
[self.playAudio play];
}
if ([buttonTitle isEqualToString:@"Pause"]) {
[self.playAudio pause];
}
if (buttonIndex==2) {
[self.playAudio stop];
}
}
- (IBAction)Music:(id)sender {
UIActionSheet *musicSheet =[[UIActionSheet alloc]initWithTitle:@"Choose" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Play",@"Pause",@"Stop", nil];
[musicSheet showInView:self.view];
}
. 我是越南人,所以我的英语技能会让你很难理解我说的话。感谢您的时间 。我期待看到你的回复