15 Oct 2015 0 comments

Unite is a conference that is run by Unity Technologies to promote their key product, the Unity gaming engine. It isn’t so much as a conference, but rather an experience. With a keynote being pre...

Read More
15 Oct 2015 0 comments

While I worked on creating the iOS Animations by Tutorials video series and the three editions of the iOS Animations by Tutorials book I got a lot of ideas how the existing anim...

Read More
15 Oct 2015 0 comments

Happy Wednesday – it’s time for our 3rd new book release as part of the iOS 9 Feast! Today, Aaron, Saul, Matthew, Pietro and I are happy to announce that Core Data by Tutorials Second E...

Read More
15 Oct 2015 0 comments

In this getting started with Core Data tutorial, you’ll write your very first Core Data app using Swift 2.0. You’ll see how easy it is to get started with all the resources provided in Xcode, fro...

Read More
15 Oct 2015 0 comments

When you create a Core Data app, you design an initial data model for your app. However, after you ship your app inevitably you’ll want to make changes to your data model. What do you do then – you...

Read More
15 Oct 2015 0 comments

As part of this year’s iOS 9 Feast, we are releasing a new video tutorial series every Thursday. This week, we are happy to release a brand new video tutorial series – Introducing Custom Cont...

Read More
15 Oct 2015 0 comments

There’s been a big hole in Spotlight on iOS for a long time. Although users can use it to find you app, they can’t see inside it — to all the content that they really care about. Currentl...

Read More
15 Oct 2015 0 comments

Storyboards have been around since iOS 5 and have received lots of upgrades and new features since then, including unwind segues for reverse navigation, universal storyboards for both iPhone and iP...

Read More

Latest Posts:

Playing Sound with button clicks - App



In this article, we will doing an app that plays sounds when you click on button. Many hots apps that have been downloaded just leverage upon this simple idea. Get a funny/interesting sound, press a button (helps if you have an image for the button) and play the sound.

In this article, let's have an app that plays cute baby sound whenever a user clicks on a button. Create a new view-based application in xcode, then design a UI similar to the below in InterfaceBuilder. Take note that the images are actually buttons, where you just have to specify the image property of the button. The rest of the controls are basically labels.


In my app, I have four buttons. Of course one will do if that is a really cool sound, for eg. vuvuzela. Copy the below code as your ViewController.h file

#import <UIKit/UIKit.h>
#include <AudioToolbox/AudioToolbox.h>

@interface SysSoundViewController : UIViewController {
CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;
}

@property (readwrite) CFURLRef soundFileURLRef;
@property (readonly) SystemSoundID soundFileObject;

- (IBAction) playASound;
- (IBAction) vibrate;
@end


Explanation:
We declare a CFURLRef which is a reference to a object representing access to a URL. It is useful as long as the resource you are accessing can be done via a URL. This CSURL object will later hold the URL to our sound files.
CFURLRef soundFileURLRef;

SystemSoundID represents a System Sound object.

SystemSoundID soundFileObject;

We next declare two Actions for linking to our buttons later on. One to play a sound, the other, to vibrate the phone.
- (IBAction) playASound;
- (IBAction) vibrate;

Copy the following code next for the ViewController.m file.

#import "SysSoundViewController.h"
@implementation SysSoundViewController

@synthesize soundFileURLRef;
@synthesize soundFileObject;

CFBundleRef mainBundle;

- (void) viewDidLoad {
[super viewDidLoad];
// Get the main bundle for the app
mainBundle = CFBundleGetMainBundle ();
}

// Respond to a tap on the System Sound button
- (IBAction) playASound {
// Get the URL to the sound file to play
soundFileURLRef  = CFBundleCopyResourceURL (mainBundle,CFSTR ("a"),CFSTR("wav"),NULL);
// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);
AudioServicesPlaySystemSound (self.soundFileObject);
}

// Respond to a tap on the Vibrate button
- (IBAction) vibrate {
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}

- (void) dealloc {
[super dealloc];
AudioServicesDisposeSystemSoundID (self.soundFileObject);
CFRelease (soundFileURLRef);
}
@end


Explanation:

The CFBundle allows you to use a folder hierachy called a bundle to organize and locate serveral kinds of resources like images, sounds or executable code. In our case, it represents the folder hierachy we have in the 'Resources' folder. Essentially, it provides us access to our Resources folder.
CFBundleRef mainBundle;

The viewDidLoad is the method that is called whenever the View loads. In it, we call the viewDidLoad of the parent class i.e. UIView.
- (void) viewDidLoad {
[super viewDidLoad];
Returns the application's main bundle. Or folder.
// Get the main bundle for the app
mainBundle = CFBundleGetMainBundle ();
}

Action linked to button.

// Respond to a tap on the System Sound button
- (IBAction) playASound {

CFBundleCopyResourceURL returns the location of the resource of name "a" and type "wav" from the mainbundle (Resource folder). Of course ensure that you have a “a.wav” audio file under ‘Resources’.

// Get the URL to the sound file to play
soundFileURLRef  = CFBundleCopyResourceURL(mainBundle,CFSTR ("a"),CFSTR("wav"),NULL);
AudioServicesCreateSystemSoundID creates a system sound object from the specified URL (first argument) and outputs it to the &soundFileObject. Notice that there is a & before soundFileObject. What that means is that we are passing the soundFileObject object reference to the method, such that when the method returns, it actually refers to a initialized soundFileObject.
// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
Finally, AudioServicesPlaySystemSound plays a short system sound of 30 secs or less. 'self' simply means 'this'. The current object's soundFileObject.
AudioServicesPlaySystemSound(self.soundFileObject);
}

For this particular Action, we can the same methodAudioServicesPlaySystemSound but the argument passed is different. When we provide the kSystemSoundID_Vibrate argument to it, it will invoke a brief vibration.

// Respond to a tap on the Vibrate button
- (IBAction) vibrate {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

Releases and disposes of the soundFile and soundFileURL objects.
- (void) dealloc {
[super dealloc];
AudioServicesDisposeSystemSoundID(self.soundFileObject);
CFRelease (soundFileURLRef);
}
 
Ensure that you link the buttons to the Actions as described in earlier tutorials and soon, you are on your way to doing your own vuvuzela app!
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment