按下新的声音按钮时如何停止声音

时间:2011-04-10 13:20:14

标签: xcode button audio

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

3 个答案:

答案 0 :(得分: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;
}

自从刚刚写完之后就没有测试它,但你已经有了一般的想法..

答案 1 :(得分: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

答案 2 :(得分: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
}
相关问题