Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The storyboard doesn't allow you to assign a 'audio' to a button. What you can do however is use the fairly simple <a href="https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html" rel="noreferrer">AVAudioPlayer</a> in a function; </p> <h2>Add Libraries</h2> <p>To use the AVAudioPlayer you will first need to add 2 libraries/frameworks to your project. These Frameworks contains the basecode for the actual player and allows us (the app writers) to use it.</p> <p>Select your project in xCode and go to <strong>Build Phases</strong>. Select <strong>Link Binary with Libraries</strong>. Press the little <strong>[+]</strong>-icon and add the frameworks:</p> <ul> <li>AudioToolbox</li> <li>AVFoundation</li> </ul> <h2>Frameworks</h2> <p>Now we have to tell our code we will actually be using these two frameworks. Open the .H file of your controller and, at the top, add:</p> <pre><code>#import &lt;AVFoundation/AVFoundation.h&gt; #import &lt;AudioToolbox/AudioToolbox.h&gt; </code></pre> <h2>Event + Player</h2> <p>Bind one of the events of a UIButton (via the storyboard), eg Touch Up Inside, to a new IBAction function (eg playSound).</p> <p>By binding the event, your telling your application that if the users presses a button -> run this function/code.</p> <p>Now, within your newly created IBAction function (eg playSound) add some code to create a player with an MP3 and play it;</p> <pre><code>- (IBAction)playSound:(id)sender { // Load in a audio file from your bundle (all the files you have added to your app) NSString *audioFile = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"mp3"]; // Convert string to NSURL NSURL *audioURL = [NSURL fileURLWithPath:audioFile]; // Initialize a audio player with the URL (the mp3) AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil]; // Start playing [player play]; } </code></pre> <p>I hope this will help you along in the right direction?</p> <p>Check the <a href="https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html" rel="noreferrer">doc</a> of Apple for al the functions/methods the AVAudioPlayer has got besides "play" (eg buffering, pause etc)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload