Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App (iOS7) Animating and Intercepting Events Adding Sound

Shaun Kelly
Shaun Kelly
5,648 Points

I'm trying to create a simple app to test my skills but having trouble with the sounds?

I'm creating an app that will play an animation and a sound when you touch the screen, then after the animation has played, randomly select and play a different sound. Here's my code so far!

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) IBOutlet UIImageView *backgroundImageView;
@end

ViewController.m

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()

@end

@implementation ViewController {
    AVAudioPlayer *introSoundFX;
    AVAudioPlayer *baconSFX;
    AVAudioPlayer *bananaSFX;

}
- (void)viewDidLoad {
    [super viewDidLoad];



    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:soundPath];
    introSoundFX = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

    NSString *soundPathTwo = [[NSBundle mainBundle] pathForResource:@"Bacon" ofType:@"mp3"];
    NSURL *urlTwo = [NSURL fileURLWithPath:soundPathTwo];
    baconSFX = [[AVAudioPlayer alloc] initWithContentsOfURL:urlTwo error:nil];




    NSString *soundPathThree = [[NSBundle mainBundle] pathForResource:@"Banana" ofType:@"mp3"];
    NSURL *urlThree = [NSURL fileURLWithPath:soundPathThree];
    bananaSFX = [[AVAudioPlayer alloc] initWithContentsOfURL:urlThree error:nil];


    self.backgroundImageView.animationImages = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1"],
                                                [UIImage imageNamed:@"2"],
                                                [UIImage imageNamed:@"3"],
                                                [UIImage imageNamed:@"4"],
                                                [UIImage imageNamed:@"5"],
                                                [UIImage imageNamed:@"6"],
                                                [UIImage imageNamed:@"7"],
                                                [UIImage imageNamed:@"8"],
                                                [UIImage imageNamed:@"9"],
                                                [UIImage imageNamed:@"10"],
                                                [UIImage imageNamed:@"11"],
                                                [UIImage imageNamed:@"12"],
                                                [UIImage imageNamed:@"13"],
                                                [UIImage imageNamed:@"14"],
                                                [UIImage imageNamed:@"15"],
                                                [UIImage imageNamed:@"16"],
                                                [UIImage imageNamed:@"17"],
                                                [UIImage imageNamed:@"18"],
                                                [UIImage imageNamed:@"19"],
                                                [UIImage imageNamed:@"20"],
                                                [UIImage imageNamed:@"21"],
                                                [UIImage imageNamed:@"22"],
                                                [UIImage imageNamed:@"23"],
                                                [UIImage imageNamed:@"24"],
                                                [UIImage imageNamed:@"25"],
                                                [UIImage imageNamed:@"26"],
                                                [UIImage imageNamed:@"27"],
                                                [UIImage imageNamed:@"28"],
                                                [UIImage imageNamed:@"29"],
                                                [UIImage imageNamed:@"30"],
                                                [UIImage imageNamed:@"31"],
                                                [UIImage imageNamed:@"32"],
                                                [UIImage imageNamed:@"33"],
                                                [UIImage imageNamed:@"34"],
                                                [UIImage imageNamed:@"35"],
                                                [UIImage imageNamed:@"36"],
                                                [UIImage imageNamed:@"37"],
                                                [UIImage imageNamed:@"38"],
                                                [UIImage imageNamed:@"39"],
                                                [UIImage imageNamed:@"40"],
                                                [UIImage imageNamed:@"41"],
                                                [UIImage imageNamed:@"42"],
                                                [UIImage imageNamed:@"43"],
                                                [UIImage imageNamed:@"44"],
                                                [UIImage imageNamed:@"45"],
                                                [UIImage imageNamed:@"46"],
                                                [UIImage imageNamed:@"47"],
                                                [UIImage imageNamed:@"48"],
                                                [UIImage imageNamed:@"49"],
                                                [UIImage imageNamed:@"50"],
                                                [UIImage imageNamed:@"51"],
                                                [UIImage imageNamed:@"52"],
                                                [UIImage imageNamed:@"53"],
                                                [UIImage imageNamed:@"54"],
                                                [UIImage imageNamed:@"55"],
                                                [UIImage imageNamed:@"56"],
                                                [UIImage imageNamed:@"57"],
                                                [UIImage imageNamed:@"58"],
                                                [UIImage imageNamed:@"59"],
                                                [UIImage imageNamed:@"60"],
                                                [UIImage imageNamed:@"61"],
                                                [UIImage imageNamed:@"62"],
                                                [UIImage imageNamed:@"63"],
                                                [UIImage imageNamed:@"64"],
                                                [UIImage imageNamed:@"65"],
                                                [UIImage imageNamed:@"66"],
                                                [UIImage imageNamed:@"67"],
                                                [UIImage imageNamed:@"68"],
                                                [UIImage imageNamed:@"69"],
                                                [UIImage imageNamed:@"70"],
                                                [UIImage imageNamed:@"71"],
                                                  nil];
    self.backgroundImageView.animationDuration = 1.5f;
    self.backgroundImageView.animationRepeatCount = 1;





}

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





-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [UIView animateWithDuration:0.5 animations:^{
        self.label.alpha = 0.0;
    }];
     [self.backgroundImageView startAnimating];

    [introSoundFX play];

}


@end
Stone Preston
Stone Preston
42,016 Points

what exactly is not working with your code right now

2 Answers

Shaun Kelly
Shaun Kelly
5,648 Points

right now it's saying there was a Thread 1: breakpoint 1.2 on like [introSFX play];