Search code examples
iosios6

My buttons play all the same sound?


Here is my MainViewController.m

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController 

@synthesize audioPlayer;
@synthesize soundsArray;

-(void)prepareSounds
{
    NSString *filepath= [[NSBundle mainBundle] pathForResource:@"Sounds" ofType:@"plist"];

    self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];

}

- (IBAction)playSound:(id)sender {

    UIButton *buttonPressed = (UIButton *)sender;

    NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -1)];

    NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
    NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

    AVAudioPlayer *p = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:file error:nil];

    self.audioPlayer = p;

    [self.audioPlayer play];
}

- (IBAction)playSound2:(id)sender {

    UIButton *buttonPressed = (UIButton *)sender;

    NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -2)];

    NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
    NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

    AVAudioPlayer *p = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:file error:nil];

    self.audioPlayer = p;

    [self.audioPlayer play];
}

- (IBAction)playSound3:(id)sender {

    UIButton *buttonPressed = (UIButton *)sender;

    NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -3)];

    NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
    NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

    AVAudioPlayer *p = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:file error:nil];

    self.audioPlayer = p;

    [self.audioPlayer play];
}

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)showInfo:(id)sender
{    
    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:controller animated:YES completion:nil];
}

@end

My MainViewController.h

    #import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>


@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {

    AVAudioPlayer *audioPlayer;
    NSArray *soundsArray;
}

@property(nonatomic, retain) AVAudioPlayer *audioPlayer;
@property(nonatomic, retain) NSArray *soundsArray;

-(void)prepareSounds;
- (IBAction)playSound:(id)sender;
- (IBAction)playSound2:(id)sender;
- (IBAction)playSound3:(id)sender;

@end

In the 'Supporting Files' folder I have an array of strings with the name of the sound files I want to play, and in the 'Supporting Files' folder I have a folder named 'Sounds', which contains the sound files.

All of my buttons play the same sound. Can someone please provide some insight. Thanks!


Solution

  • SOLVED: prepareSounds() was never called. Here is the working code:

        #import "MainViewController.h"
    
    @interface MainViewController ()
    
    @end
    
    @implementation MainViewController 
    
    @synthesize audioPlayer;
    @synthesize soundsArray;
    
    -(void)prepareSounds
    {    
        NSString *filepath= [[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"plist"];
    
        self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];
    
    }
    
    - (void)stopAudio
    {
        if (audioPlayer!= nil) {
            [audioPlayer stop];
            //do some task for changing the Image i.e setting the default image
        }
    
    }
    
    - (IBAction)playSound:(UIButton *)sender
    {
        UIButton *btn = (UIButton*)sender;
    
        NSString *soundName = [soundsArray objectAtIndex:(btn.tag - 1)];
    
        NSString *path      = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
        NSURL *file         = [[NSURL alloc] initFileURLWithPath:path];
    
        AVAudioPlayer *p    = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
    
        self.audioPlayer    = p;
    
        if([audioPlayer isPlaying])
        {
            [self stopAudio];
        }
    
        [self.audioPlayer play];
    
    
    }
    
    
    - (void)viewDidLoad
    {
        [self prepareSounds];
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Flipside View
    
    - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    - (IBAction)showInfo:(id)sender
    {    
        FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
        controller.delegate = self;
        controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:controller animated:YES completion:nil];
    }
    
    @end