-1

Hello I am building an app which plays a sound when the user shakes it. Here is my code:

#import "ViewController.h"
#define accelerationThreshold  0.5

@interface ViewController ()

@property (strong, nonatomic) CMMotionManager *motionManager;

@end

@implementation ViewController
AVAudioPlayer *myAudio;


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"sound"
                                     ofType:@"wav"]];

NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]
                initWithContentsOfURL:url
                error:&error];
if (error)
{
    NSLog(@"Error in audioPlayer: %@",
          [error localizedDescription]);
} else {
    _audioPlayer.delegate = self;
    [_audioPlayer prepareToPlay];
}

////
_motionManager = [[CMMotionManager alloc] init];
_motionManager.deviceMotionUpdateInterval = 0.5;

[_motionManager startDeviceMotionUpdatesToQueue: [NSOperationQueue currentQueue]  withHandler:^(CMDeviceMotion *motion, NSError *error)
{
    [self motionMethod:motion];
}];

}



- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:@"sound"
                                     ofType:@"wav"]];

NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]
                initWithContentsOfURL:url
                error:&error];
if (error)
{
    NSLog(@"Error in audioPlayer: %@",
          [error localizedDescription]);
} else {
    _audioPlayer.delegate = self;
    [_audioPlayer prepareToPlay];
}

////
_motionManager = [[CMMotionManager alloc] init];
_motionManager.deviceMotionUpdateInterval = 0.5;

[_motionManager startDeviceMotionUpdatesToQueue: [NSOperationQueue currentQueue]  withHandler:^(CMDeviceMotion *motion, NSError *error)
{
    [self motionMethod:motion];
}];

}

- (IBAction)playAudio:(id)sender {
[_audioPlayer play];
}

-(void)motionMethod:(CMDeviceMotion *)deviceMotion
{
CMAcceleration userAcceleration = deviceMotion.userAcceleration;
if (fabs(userAcceleration.x) > accelerationThreshold
    || fabs(userAcceleration.y) > accelerationThreshold
    || fabs(userAcceleration.z) > accelerationThreshold)
{
    //Motion detected, handle it with method calls or additional
    //logic here.
    [self playAudio:self];
}
}

However I want the sound to overlay the current sound every time a user shakes their device, how do I do this? Because currently it play eery time the sound finishes, how do I fix?

user3647176
  • 201
  • 1
  • 2
  • 6

1 Answers1

0

To play more than one sound simultaneously with AVAudioPlayer, you need more than one AVAudioPlayer. You've only got one, so every time you send it play it is still the only one playing.

matt
  • 515,959
  • 87
  • 875
  • 1,141