Sound overlapping with multiple button presses

前端 未结 6 1452
野的像风
野的像风 2020-11-29 13:56

When I press a button, then press another one, the sounds overlap. How can I fix that so the first sound stops when another one is pressed?

 - (void)playOnce         


        
相关标签:
6条回答
  • 2020-11-29 14:32

    The code you've posted will only play one sound, the first sound sent to the method.

    If you want to only play one changeable sound, after stoping the player, release theAudio and then set the new sound.

    0 讨论(0)
  • 2020-11-29 14:39

    Add a check to see if the player is already playing at the beginning of each method:

    if (theAudio.playing == YES) {
        [theAudio stop];
    }
    

    AVAudioPlayer Class Reference

    0 讨论(0)
  • 2020-11-29 14:42

    In your playOnce method the 'path' variable is unused -- remove that to get rid of your warning message. Your playOnce does not set anything up to play so I'm not sure how that is supposed to work -- unless you call playLooped first? You also need to be calling prepareToPlay after the initWithContentsOfUrl.

     - (void)playOnce:(NSString *)aSound {
    
      NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
      if (theAudio && [theAudio isPlaying]) {
        [theAudio stop]
      } else {
             theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
             [theAudio prepareToPlay];
    
             [theAudio setDelegate: self];
        [theAudio setNumberOfLoops: 1];
        [theAudio setVolume: 1.0];
        [theAudio play];
      }
    
    }
    
    0 讨论(0)
  • 2020-11-29 14:44

    You can probably do something like the following:

        (void) playLooped: (NSString * ) aSound {
            NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: @"caf"];
    
        //stop audio player from playing so the sound don't overlap
        if([theAudio isPlaying])
        {
            [theAudio stop]; //try this instead of stopAudio
    
        }
    
        if (!theAudio) {
            theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
        } 
        [theAudio setDelegate: self];
        // loop indefinitely
        [theAudio setNumberOfLoops: -1];
        [theAudio setVolume: 1.0];
        [theAudio play];
    }
    
    0 讨论(0)
  • 2020-11-29 14:50

    You will need to utizilize a BOOL value to get this to work properly.

    in your .m file BEFORE @implementation put this:

    static BOOL soundIsPlaying = NO;
    

    Then your IBAction needs to look something like this:

    - (IBAction)play {
        if (soundIsPlaying == YES) {
            [theAudio release];
            soundIsPlaying = NO;
    
        }
    
        else if (soundIsPlaying == NO) {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"SOUNDFILENAME" ofType:@"wav"];
            theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
            theAudio.delegate = self;
            theAudio.volume = 1.0;
            theAudio.numberOfLoops = 0;
            [theAudio play];
            soundIsPlaying = YES;
    
        }   
    }
    

    thats honestly about it. It will stop sounds when another button is pressed.

    0 讨论(0)
  • 2020-11-29 14:59

    Declare your AVAudioPlayer in the header the viewController ( don't alloc a new one each time you play a sound). That way you will have a pointer you can use in a StopAudio method.

    @interface myViewController : UIViewController <AVAudioPlayerDelegate> { 
        AVAudioPlayer *theAudio;
    }
    @property (nonatomic, retain) AVAudioPlayer *theAudio;
    @end
    
    
    @implementation myViewController
    @synthesize theAudio;
    - (void)dealloc {
        [theAudio release];
    }
    @end
    
    
    
    - (void)playOnce:(NSString *)aSound {
        NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
        if(!theAudio){
            theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
        }
        [theAudio setDelegate: self];
        [theAudio setNumberOfLoops:0];
        [theAudio setVolume:1.0];
        [theAudio play];
    }
    
    - (void)playLooped:(NSString *)aSound {
        NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"];
        if(!theAudio){
            theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
        }
        [theAudio setDelegate: self];
        // loop indefinitely
        [theAudio setNumberOfLoops:-1];
        [theAudio setVolume:1.0];
        [theAudio play];
    }
    
    - (void)stopAudio {
        [theAudio stop];
        [theAudio setCurrentTime:0];
    }
    

    also be sure to read the Apple Docs

    0 讨论(0)
提交回复
热议问题