我是Xcode的新手,如果您单击声音按钮多次重叠,我遇到问题。我将如何设置我的代码,以便在播放声音时单击按钮将停止播放当前声音并启动新声音。如何在按下新声音按钮时停止声音
如果您可以发布代码,将不胜感激。
感谢, 多米尼克
这里是我当前的代码副本:
#import "Sound3ViewController.h"
@implementation Sound3ViewController
-(IBAction)playnow:(id)sender;{
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"winning"
ofType:@"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)play2:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"onadrug"
ofType:@"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (void)dealloc
{
[super dealloc];
[sound release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
感谢杰克,我代替我原来的PlayNow按钮代码更新的代码,但我收到一些错误信息。在线 - (IBAction)playnow:(id)发件人我收到错误消息:“预期的表达式”,“playnow”undecred here(不在函数中),Exprected“,”或“;”在“:”标记之前并在行上:allow = true;我收到错误:“允许”undecalred(在此函数中首次使用) – Dominick 2011-04-10 13:45:49
当然,因为我写了'boolean allowed = true'没有分号(;),只需添加它即可。你用Obj-C语法看起来不太实际.. :)不关心'allow',因为你必须选择存储它(如果在C文件中或者在对象内部作为属性)。 – Jack 2011-04-10 13:48:01
杰克,你的权利,我试图学习目标C. :) – Dominick 2011-04-10 14:00:00