2011-04-10 461 views
0

我是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 

回答

0

我建议你禁止播放声音具有可变和调度一个动作一遍允许它后文件的持续时间。例如:

boolean allowed = true 

-(IBAction)playnow:(id)sender 
{ 
    if (allowed) { // <--- ADD THIS 
    allowed = false; 
    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
            pathForResource:@"winning" 
            ofType:@"mp3"]]; 
    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil]; 
    sound.delegate = self; 
    [sound play]; 

    NSInvocation *invocation = [[NSInvocation alloc] init]; 
    [invocation setTarget:self]; 
    [invocation setSelector:@selector(allowPlaying)]; 
    [NSTimer scheduledTimerWithTimeInterval:[sound duration] invocation:invocation repeats:NO]; 
    } // <-- AND THIS 
} 
-(void)allowPlaying 
{ 
    allow = true; 
} 

没有测试它,因为只是写上飞,但你已经得到了普遍的想法..

+0

感谢杰克,我代替我原来的PlayNow按钮代码更新的代码,但我收到一些错误信息。在线 - (IBAction)playnow:(id)发件人我收到错误消息:“预期的表达式”,“playnow”undecred here(不在函数中),Exprected“,”或“;”在“:”标记之前并在行上:allow = true;我收到错误:“允许”undecalred(在此函数中首次使用) – Dominick 2011-04-10 13:45:49

+0

当然,因为我写了'boolean allowed = true'没有分号(;),只需添加它即可。你用Obj-C语法看起来不太实际.. :)不关心'allow',因为你必须选择存储它(如果在C文件中或者在对象内部作为属性)。 – Jack 2011-04-10 13:48:01

+0

杰克,你的权利,我试图学习目标C. :) – Dominick 2011-04-10 14:00:00

0

我是新来objC还,所以我不知道这是正确的,但试试这个:

#import "Sound3ViewController.h" 

@implementation Sound3ViewController 

-(IBAction)playnow:(id)sender;{ 
    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
            pathForResource:@"winning" 
            ofType:@"mp3"]]; 

    // NEW STUFF 
    if (sound) { 
     if ([sound playing]) { 
      [sound stop]; 
     } 
     [sound release]; 
    } 

    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"]]; 

    if (sound) { 
     if ([sound playing]) { 
      [sound stop]; 
     } 
     [sound release]; 
    } 

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil]; 
    sound.delegate = self; 
    [sound play]; 


} 

- (void)dealloc 
{ 
    [super dealloc]; 
    if (sound) { 
     [sound release]; 
    } 
} 

- (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 
+0

谢谢,我收到了一些错误:if([sound playing])语义问题:找不到方法'-playing'(返回类型默认为'id')并且在线:if([sound playing])' AVAudioPlayer'可能不会响应'-playing' – Dominick 2011-04-10 13:57:11

+0

我认为它应该是'if([sound isPlaying])'。 – MusiGenesis 2011-04-10 19:49:40

+0

...或者我想'如果(sound.playing)'也可以。无论出于何种原因,objC对于某些其他语言对待差不多相同的构造(属性或方法)都是挑剔的,所以这类事情会出现很多。 – MusiGenesis 2011-04-10 19:51:39

0
// in .h file 

#import "AVFoundation/AVAudioPlayer.h" 
//set delegate 
@interface SamplesoundViewController : UIViewController <AVAudioPlayerDelegate>{ 
AVAudioPlayer *player1; 
    AVAudioPlayer *player2; 
... 
... 
} 
@property (nonatomic, retain) AVAudioPlayer *player1; 
@property (nonatomic, retain) AVAudioPlayer *player2; 
-(IBAction)soundfirst; 
-(IBAction)soundsecond; 
-(IBAction)Newmethodname; 



// in .m file 
@implementation SamplesoundViewController 

@synthesize player1,player2; 

-(IBAction)soundfirst{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"soundfilefirst" ofType:@"mp3"]; 
    player1=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

    player1.delegate=self; 
    [player1 play]; 
    [self performSelector:@selector(soundsecond) withObject:nil afterDelay:0.90]; 
} 



-(IBAction)soundsecond{ 
    [player1 stop]; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Soundfilesecond" ofType:@"mp3"]; 

    player2=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

    player2.delegate=self; 
    [player2 play]; 
    [self performSelector:@selector(Newmethodname) withObject:nil afterDelay:1]; 


} 


-(IBAction)Newmethodname{ 
    [player2 stop]; 
    //write your code here 
}