ok lets start
first we will do the sound on the first view
start a new project (view based) call it i will soundSwitchView
first we need to add the audio player framework.
so under FrameWorks (in xcode) right or control click on UIKit.frameWork
then click reveal in finder.
scroll to the top and find "AVFoundation.framework"
drag that folder into the frameworks folder in xcode.
When you drag it in you should get a message in xcode at the top there should be a check box and it says "Copy items to destination group folder(if needed)"
un check the box so there is NO check mark on it.
No go into your soundSwitchViewViewControler.h
make a IBAction and call it playSound (you can call it what ever you want to i would name it what the audio is).
you make the Action by typing this AFTER THE Brackets
{
}
so
put this code without the
@interface tutorial_for_DudeViewController : UIViewController {
}
Code:
@interface tutorial_for_DudeViewController : UIViewController {
}
- (IBAction)playSound;
Now hit command B then save all then go to your viewController .m
below
#import "tutorial_for_DudeViewController.h"
type
Code:
#import<AVFoundation/AVAudioPlayer.h>
so the top of you .m file should look like this
Code:
#import "tutorial_for_DudeViewController.h"
#import<AVFoundation/AVAudioPlayer.h>
@implementation tutorial_for_DudeViewController
Good.
Now go to finder and find the image you want the button to be drag that into the you xcode project check the box that says copy if needed at the top
do the same thing with your audio file
Now we can make the sound play.
below
@implementation tutorial_for_DudeViewController
type
Code:
- (IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"SOUND FILE" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
}
NOTE at the top the IBAction should be the same ibaction that we made in the .h
NOTE the SOUND FILE on line 2 should be the name of your audio file that you dragged in. And the mp3 should be change to your audio file extension.
if you dont know how to find out what audio the it is tel me and i will tell you how to figure it out
Now hit Command B and it looks like we have 0 errors. NOw time to make it switch views. (We will make it play a sound on the other view to).
First in you viewController.h file make another IBAction and call it next vide so now you .h should look ilk this
Code:
#import <UIKit/UIKit.h>
@interface tutorial_for_DudeViewController : UIViewController {
}
- (IBAction)playSound;
- (IBAction)nextView;
@end
hit Command B then save all
now on the Classes folder Right or control click Add-New File.
then click UIVIewControllersubClass.
Make sure it at the bottom check box it says with XIB for user interface.
And if you program it made for ipad check targeted for ipad.
Now click next. Type in a name for it i will call it viewTwo.
Click Finish now under classes drag the ViewTwo.xib to the Resourses folder.
Now go to your soundSwitchViewViewController.h
under
#Import <UIKit/UIKit.h>
type #import "viewTwo.h"
so it should look like this
Code:
#import <UIKit/UIKit.h>
#import "viewTwo.h"
now in your ViewController.m (not the viewTwo.m)
and type
Code:
- (IBAction)back{
viewTwo *secondView = [[viewTwo alloc] initWithNibName:@"viewTwo" bundle:nil];
secondView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:secondView animated:UIModalTransitionStyleCoverVertical];
[secondView release];
}
Now we have the code to switch the views….
on the second line when it says
UIModalTransitionStyleCoverVertical;
if you type
UIModalTransition
then hit esc you get more transition styles that are animated when you switch views
it also works for the third line to
now hit Command B the save all
YAY! no errors so far…
How you doing? Hopefully Good!
now if you go into your viewTwo.h and make a IBAction call it goBack
so you viewTwo.h should look like this
Code:
#import <UIKit/UIKit.h>
@interface viewTwo : UIViewController {
}
- (IBAction)goBack;
create another IBAction call it
sound
so now it should look like this
Code:
#import <UIKit/UIKit.h>
@interface viewTwo : UIViewController {
}
- (IBAction)goBack;
- (IBAction)sound;
@end
now go to your ViewController.m (not viewTwo.m)
and copy the code
Code:
- (IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"B high" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
}
paste it under the
@implementation viewTwo
in viewTwo.m
also in viewTwo.m below #import "viewTwo.h"
type
#import<AVFoundation/AVAudioPlayer.h>
so the code we added should look like this
Code:
#import "viewTwo.h"
#import<AVFoundation/AVAudioPlayer.h>
@implementation viewTwo
- (IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"B high" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
}
now to create the goBack action to go back to the first view
on your viewTwo.m type this under
Code:
- (IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"B high" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
}
Code:
- (IBAction)goBack{
[self dismissModalViewControllerAnimated:UIModalTransitionStyleCrossDissolve];
}
so your viewTwo.m should look like this
Code:
#import "viewTwo.h"
#import<AVFoundation/AVAudioPlayer.h>
@implementation viewTwo
- (IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"B high" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
}
- (IBAction)goBack{
[self dismissModalViewControllerAnimated:UIModalTransitionStyleCrossDissolve];
}
hit Command B then save all now go to your resources and open the viewController.xib (not viewTwo.xib your soundSwitchViewViewController.xib)
double click it
when Interface Builder opens drag in a round rect button place it any where on the screen
now click the files owner.
now go to your connection inspector (tools connection inspector) and lclick the play sound (the little circle next to it)
drag it to you button then a pop up menu should appear touch up inside is where when you finger touches off the button it will play touch down is when your finger hits it it will play.
im going to do touchDown
now click the button
no under the "Button Attributes"
change type to custom then under image change it to the name of your image or hit the down button and choose your image.
resize the button to fit the image
now add in a navigation bar it to the top and change the title to what ever you want it to be
now drag in a bar button item to the navigation bar and change the text to "next"
Now click the files owner then the attributes and drag nextView to the bar Button
save it and close
now open viewTwo.xib
drag in a round rect button place it any where on the screen
now click the button
no under the "Button Attributes"
change type to custom then under image change it to the name of your image or hit the down button and choose your image.
now click the files owner.
now go to your connection inspector (tools-connection inspector) and click the sound (the little circle next to it)
drag it to you button then a pop up menu should appear touch up inside is where when you finger touches off the button it will play touch down is when your finger hits it it will play.
now add in a navigation bar it to the top and change the title to what ever you want it to be
now drag in a bar button item to the navigation bar and change the text to "back"
now click files owner then drag goBack to the bar button
save it then go to xcode and build and go!
and your done! you can repeat the steps at play even more sounds