Xcode 4 Tutorial: Play Sound File (Beginner-Friendly)

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
7,152
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Jun 9, 2011

Very simple, easy, and understandable tutorial on how to play a sound in Xcode.

NOTE: I do not show source code in the description because it takes away from the learning experience. Pay attention so that you learn some stuff ;)

=== How to reset the button automatically after the audio stops ===

Dear DeltaClanOfficial,

I am glad you asked! This is a very easy process.

Go to your .h file and right under "IBOutlet UIButton *start;", create a new line and add the following before the ' } ':
NSTimer *timer;

Now go to your .m file. Look for "audioPlayer.numberOfLoops = X;" (X being whatever you set). Create a line BELOW that and add:

int timeTillStop = audioPlayer.duration;

timer = [NSTimer scheduledTimerWithTimeInterval:timeTillStop target:self selector:@selector(stop) userInfo:nil repeats:FALSE];

Ok, now we have a timer set to run the selector "stop" after waiting the length of the audio.

Right below "[audioPlayer release];", on a separate line add:
[timer release];

Right above "- (void)dealloc", add:

- (void)stop {
[start setTitle:@"Start" forState:UIControlStateNormal];
clicked = 0;
}

There you go! Just press RUN and it should work.

If you have any problems, just tell me.

Category:

Howto & Style

Tags:

License:

Standard YouTube License

  • likes, 1 dislikes

Link to this comment:

Share to:

Uploader Comments (GTOTechnology)

  • are you using 4.21? mine was filled with errors and i even double checked it... what do i do?

  • @trekstenen1 This was just 4.0.

  • 2:29 - 'In my penis' LOL!!!!!!!

  • @trickyart WOW! I never noticed that. Oops O.o

Top Comments

  • an error pops up on release

    why?

see all

All Comments (41)

Sign In or Sign Up now to post a comment!
  • Great. I looked at a lot of other tutorials that didn't work or the XCode was out of date. The coding on this is much clearer and more logical. The one error I had is that ARC didn't allow explicit release. I replaced [audioPlay release] with [audioPlay stop]. Works fine and I'll work out the release later.

  • Thanks for your video!!!

    What if the mp3 or wav file that I want to play is in my webpage server?

  • NICE JOB good tutorial keep it up...

  • @GTOTechnology

    ok. because i am new at this and it´s really hard to figure out how to use it when all the tutorials are 4.2 or lower...

    thanks for you're answer :D

  • the .xib are not in my Xcode Why???

    Please help my.

  • @TheKencats i get the same error.... have you fixed it?

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more