Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Play sound with multiple views (beginner)

BillyGigglesBillyGiggles Posts: 458Registered Users
edited May 2011 in Tutorial Discussion
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 {

}
@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
#import<AVFoundation/AVAudioPlayer.h>


so the top of you .m file should look like this

#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
- (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

#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
#import &lt;UIKit/UIKit.h&gt;
#import &quot;viewTwo.h&quot;


now in your ViewController.m (not the viewTwo.m)

and type
- (IBAction)back{
	viewTwo *secondView = [[viewTwo alloc] initWithNibName:@&quot;viewTwo&quot; 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

#import &lt;UIKit/UIKit.h&gt;


@interface viewTwo : UIViewController {

}
- (IBAction)goBack;

create another IBAction call it
sound

so now it should look like this
#import &lt;UIKit/UIKit.h&gt;


@interface viewTwo : UIViewController {

}
- (IBAction)goBack;
- (IBAction)sound;

@end


now go to your ViewController.m (not viewTwo.m)

and copy the code
- (IBAction)playSound {
	NSString *path = [[NSBundle mainBundle] pathForResource:@&quot;B high&quot; ofType:@&quot;mp3&quot;];
	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


#import &quot;viewTwo.h&quot;

#import&lt;AVFoundation/AVAudioPlayer.h&gt;


@implementation viewTwo

- (IBAction)playSound {
	NSString *path = [[NSBundle mainBundle] pathForResource:@&quot;B high&quot; ofType:@&quot;mp3&quot;];
	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
- (IBAction)playSound {
	NSString *path = [[NSBundle mainBundle] pathForResource:@&quot;B high&quot; ofType:@&quot;mp3&quot;];
	AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
	theAudio.delegate = self;
	
}
- (IBAction)goBack{
	
	[self dismissModalViewControllerAnimated:UIModalTransitionStyleCrossDissolve];
	
	
}

so your viewTwo.m should look like this


#import &quot;viewTwo.h&quot;

#import&lt;AVFoundation/AVAudioPlayer.h&gt;


@implementation viewTwo

- (IBAction)playSound {
	NSString *path = [[NSBundle mainBundle] pathForResource:@&quot;B high&quot; ofType:@&quot;mp3&quot;];
	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
Post edited by BillyGiggles on
_______________________________________________<br />
Thanks for the help!

Replies

  • BillyGigglesBillyGiggles Posts: 458Registered Users
    edited July 2010
    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{
    _______________________________________________<br />
    Thanks for the help!
  • Chris1979Chris1979 Posts: 81Registered Users @
    edited February 2011
    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
  • Pebs85Pebs85 Posts: 26Registered Users @
    edited February 2011
    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 fileURLWithPath:path] 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 fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;

    }

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

    @end


    Thanks
  • BillyGigglesBillyGiggles Posts: 458Registered Users
    edited February 2011
    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
    _______________________________________________<br />
    Thanks for the help!
  • Pebs85Pebs85 Posts: 26Registered Users @
    edited February 2011
    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

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

    #import <UIKit/UIKit.h>

    @interface soundSwitchViewViewController : UIViewController {

    }

    -(IBAction)playSound;

    @end

    [!/code]

    And

    [!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
  • BillyGigglesBillyGiggles Posts: 458Registered Users
    edited February 2011
    Hmm which line is the error one? Also you forgot to remove the ! In the [!code] :D
    _______________________________________________<br />
    Thanks for the help!
  • BillyGigglesBillyGiggles Posts: 458Registered Users
    edited February 2011
    Try putting the #import <AVaudioPlayer line in the .h as well
    _______________________________________________<br />
    Thanks for the help!
  • BillyGigglesBillyGiggles Posts: 458Registered Users
    edited February 2011
    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
    _______________________________________________<br />
    Thanks for the help!
  • Pebs85Pebs85 Posts: 26Registered Users @
    edited February 2011
    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??
  • BillyGigglesBillyGiggles Posts: 458Registered Users
    edited February 2011
    I senting you a PM
    _______________________________________________<br />
    Thanks for the help!
  • Pebs85Pebs85 Posts: 26Registered Users @
    edited February 2011
    Instructions followed... i think..
    Thanks for your help Billy...
    i really appreciate it!
  • MstealthMstealth Posts: 6New Users
    edited May 2011
    Looking really good tutorial and thanks for this nice tutorial.
Sign In or Register to comment.