Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 07-17-2010, 01:11 PM   #1 (permalink)
Nerd.
 
Join Date: Jun 2010
Location: In a house
Posts: 458
BillyGiggles is on a distinguished road
Default Play sound with multiple views (beginner)

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
BillyGiggles is offline   Reply With Quote
Old 07-17-2010, 01:19 PM   #2 (permalink)
Nerd.
 
Join Date: Jun 2010
Location: In a house
Posts: 458
BillyGiggles is on a distinguished road
Default

Sorry! When we made the IBAction "sound" when we made the code to play the sound (in .m) on viewTwo.m change -(IBAction)playSound{

to

-(IBAction)sound{
BillyGiggles is offline   Reply With Quote
Old 02-08-2011, 01:11 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 80
Chris1979 is on a distinguished road
Default

Quote:
Originally Posted by BillyGiggles View Post
Sorry! When we made the IBAction "sound" when we made the code to play the sound (in .m) on viewTwo.m change -(IBAction)playSound{

to

-(IBAction)sound{
Hey!

Nice tutorial!

One question though... I´m not sure if I understood it correctly but does this mean that the sound on the second view can be controlled from the first view?

like turning it off or playing it? (i´ve actually been loiking for a tutorial regarding that specific thing)

/chris
Chris1979 is offline   Reply With Quote
Old 02-21-2011, 05:03 AM   #4 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 19
Pebs85 is on a distinguished road
Default Error Problem with this tutorial

Im trying to do ur tutorial...
the soundviewcontroller one

Im up to part

- (IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"UptownGirl" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;

}

but i get an error...... "_ONJC_CLASS_$_AVAudioPlayer", referenced from:
Objc-class-ref-to-AVAudioPlayer in soundSwitchViewViewController.o
Symbol(s) not found
Collect2LId returned 1 exit status

what does that even mean??

What am i doing wrong??

my code so far is.....


//
// soundSwitchViewViewController.h
// soundSwitchView
//

#import <UIKit/UIKit.h>

@interface soundSwitchViewViewController : UIViewController {

}

-(IBAction)playSound;

@end


AND

//
// soundSwitchViewViewController.m
// soundSwitchView
//
//

#import "soundSwitchViewViewController.h"
#import<AVFoundation/AVFoundation.h>

@implementation soundSwitchViewViewController

- (IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"UptownGirl" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;

}

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

@end


Thanks
Pebs85 is offline   Reply With Quote
Old 02-21-2011, 11:49 AM   #5 (permalink)
Nerd.
 
Join Date: Jun 2010
Location: In a house
Posts: 458
BillyGiggles is on a distinguished road
Default

Hey thnks form your reply I'll see if I know how to fix it, but first will you paste you code like this this easier to read...but in the top and bottom part leave out the ! In each one

[!code]

Insert your code here....

[!/code]




That way it won't show Smily faces where you code it.

Thanks,

Lane
__________________
_______________________________________________
Thanks for the help!
BillyGiggles is offline   Reply With Quote
Old 02-21-2011, 02:54 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 19
Pebs85 is on a distinguished road
Default

Quote:
Originally Posted by BillyGiggles View Post
Hey thnks form your reply I'll see if I know how to fix it, but first will you paste you code like this this easier to read...but in the top and bottom part leave out the ! In each one

[!code]

Insert your code here....

[!/code]




That way it won't show Smily faces where you code it.

Thanks,

Lane

Quote:
[!code]
//
// soundSwitchViewViewController.h
// soundSwitchView
//
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface soundSwitchViewViewController : UIViewController {

}

-(IBAction)playSound;

@end

[!/code]
And

Quote:

[!code]

//
// soundSwitchViewViewController.m
// soundSwitchView
//
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "soundSwitchViewViewController.h"
#import<AVFoundation/AVFoundation.h>

@implementation soundSwitchViewViewController

- (IBAction)playSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"UptownGirl" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;

}

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

@end


[!/code]
The controller m is where my errors are coming up

Thanks

Last edited by Pebs85; 02-21-2011 at 02:59 PM. Reason: Still Smileys...
Pebs85 is offline   Reply With Quote
Old 02-21-2011, 04:42 PM   #7 (permalink)
Nerd.
 
Join Date: Jun 2010
Location: In a house
Posts: 458
BillyGiggles is on a distinguished road
Default

Hmm which line is the error one? Also you forgot to remove the ! In the [!code]
__________________
_______________________________________________
Thanks for the help!
BillyGiggles is offline   Reply With Quote
Old 02-21-2011, 04:43 PM   #8 (permalink)
Nerd.
 
Join Date: Jun 2010
Location: In a house
Posts: 458
BillyGiggles is on a distinguished road
Default

Try putting the #import <AVaudioPlayer line in the .h as well
__________________
_______________________________________________
Thanks for the help!
BillyGiggles is offline   Reply With Quote
Old 02-21-2011, 04:46 PM   #9 (permalink)
Nerd.
 
Join Date: Jun 2010
Location: In a house
Posts: 458
BillyGiggles is on a distinguished road
Default

Sorry for all the post but I keep thinking of things you may have left out.

Did you add the framework,
Also if or when you added the framework did you make sire the box was UNCHECKED
__________________
_______________________________________________
Thanks for the help!
BillyGiggles is offline   Reply With Quote
Old 02-21-2011, 05:29 PM   #10 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 19
Pebs85 is on a distinguished road
Default

Quote:
Originally Posted by BillyGiggles View Post
Sorry for all the post but I keep thinking of things you may have left out.

Did you add the framework,
Also if or when you added the framework did you make sire the box was UNCHECKED
Yeh I added the framework like u said at the start and made sure the tick wasn't there.....

Il try and post a screen shot of the error messages....
Im pretty sure I followed it all! It's prob somethin simple but I'm not experienced enough to be able to pick it up yet...
Is there somewhere I can post the source code? Maybe that will help??
Pebs85 is offline   Reply With Quote
Old 02-21-2011, 06:21 PM   #11 (permalink)
Nerd.
 
Join Date: Jun 2010
Location: In a house
Posts: 458
BillyGiggles is on a distinguished road
Default

I senting you a PM
__________________
_______________________________________________
Thanks for the help!
BillyGiggles is offline   Reply With Quote
Old 02-22-2011, 08:06 AM   #12 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 19
Pebs85 is on a distinguished road
Default

Instructions followed... i think..
Thanks for your help Billy...
i really appreciate it!
Pebs85 is offline   Reply With Quote
Old 05-12-2011, 03:28 AM   #13 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 6
Mstealth is on a distinguished road
Default

Looking really good tutorial and thanks for this nice tutorial.
Mstealth is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,959
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0