Xcode 6 - Press button to play sound - objective-c

Looking for some help trying to get my button to play sound. I've looked up over 50 tutorials but all of them are outdated and do not work with the new Xcode. Does anyone have a good resource to learn? Fairly new to Objective-C. I've looked through the Apple documentation for the Audio frameworks and my brain cells committed suicide. Any help or pointers in the right direction would be greatly appreciated.

in viewController.h
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface viewController : UIViewController
#property (strong, nonatomic) AVAudioPlayer *player;
#end
and viewController.m
-(void)yourButtonDidTap {
NSString *soundFilePath = [NSString stringWithFormat:#"%#/%#.mp3",
[[NSBundle mainBundle] resourcePath], soundName];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
_player.numberOfLoops = 0;
[_player play];
}

Related

Sound not playing in iOS Simulator. No errors

I have constructed some code that is supposed to play audio when a button is clicked. There are no errors in the code, but when I open the iOS Simulator and press the button, no sound plays. The button does the tap animation, but nothing happens.
I have both the AVFoundation and AudioToolbox frameworks.
I also have my audio file in resources.
I am not using breakpoints.
The button is set to use the 'playAudio:' first responder upon Touch Up Inside.
Here is my 'ViewController.h' file:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (IBAction)playAudio:(id)sender;
#end
And here is my 'ViewController.m' file:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"AudioFile" ofType:#"wav"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
sleep(5);
};
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The audio segment is about 3 seconds.
Any help is much appreciated!
Thanks!
I am quite new to ios but I had the same problem and was able to fix it.
I simply declared my AVAudioPlayer variable as a global variable and called
[audioPlayer prepareToPlay];
before
[audioPlayer play];
Just calling prepareToPlay didnt fix it, I had to declare the global variable to for it to work.
Not sure why that makes a difference to the Simulator but it works now.
Play sound/music file in objective-c,
I had the same problem and was able to fix it with below code.
.h file
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController
- (IBAction)actionOnPlayButton:(id)sender;
- (IBAction)actionOnStopButton:(id)sender;
- (void)playSound ;
#end
.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
AVAudioPlayer *audioPlayer;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Play Music";
}
-(void)playSound{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sound10"
ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
- (IBAction)actionOnPlayButton:(id)sender {
[self playSound];
}
****Used above lines of code, it will work fine...
Note:- Don't forget to import following framework:-**
#import
#import

UIWebView reveal webpage upon launch of application?

I'm trying to launch a URL with the UIWebView upon launch of my Mac application.
I started out with this,
ViewController.h
#import <Cocoa/Cocoa.h>
#interface ViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
ViewController.m
#import "ViewController.h"
- (void)viewDidLoad {
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
With that, I get two errors that I can't figure out.
(ViewController.h) #interface ViewController : UIViewController {
Error - Cannot find interface decleration 'UIViewController', superclass of 'ViewController'
(ViewController.m) - (void)viewDidLoad {
Error - Missing context for method declaration
I'm not really familiar with is and can't find what I'm doing wrong.
For OSX app, you don't have UIViewController, instead, you have NSWindowController and NSViewController, check Mac Developer Library.
You are using IOS functions instead of OSX.
Try this
In youy .h file
#import <WebKit/WebKit.h>
WebView *myWebView;
In your .m file
- (void)windowDidLoad
{
NSString *urlAddress = #"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[myWebView mainFrame] loadRequest:requestObj];
}
Don't forget to include "webkit.framework"

handling events when app goes into the background

What I want is that my app should stop playing sound when it enters the background (when home button is pressed). I did this in appDelegate.m but it says use of undeclared identifier 'soundID'
- (void)applicationDidEnterBackground:(UIApplication *)application
{
AudioServicesDisposeSystemSoundID(soundID)
}
I have imported my VC into the delegate and have also exposed the soundID as a property. Where am I going wrong? Please help.
NSString *path = [[NSBundle mainBundle] pathForResource:#"something" ofType:#"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID);
AudioServicesPlaySystemSound(soundID);
If you want that functionality you should go to your app main plist file (probably named [your-app-name]-Info.plist) and there find a key:
Required background modes
and remove value
App plays audio
That should do the trick.
----------------EDIT:
Try this:
NSString* path = [[NSBundle mainBundle] pathForResource:#"myWavFile" ofType:#"wav" inDirectory:#"DirectoryName"];
self.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
[self.player play];
Where self player is:
#property (strong, nonatomic) AVPlayer *player;
You should also:
#import <AVFoundation/AVFoundation.h>
And add this framework to your project.

can't play sound on Xcode 4.3

I am using Xcode 4.3 and i have a problem with playing sound...
I downloaded this project from an answer i found here from but when i change his file with mine http://dl.dropbox.com/u/63377498/AudioText.zip i can't hear anything. Can be a problem of my simulator? Settings of my mac? I don't know...
That's the code i use:
#interface ViewController ()
#property (nonatomic, strong) AVAudioPlayer *theAudio;
#end
#implementation ViewController
#synthesize theAudio = _theAudio;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"sad" ofType:#"caf"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
}
- (IBAction)pushBell {
[self.theAudio play];
}
Finally i found the answer on my own. I was doing everything right but i used a sound file downloaded from youtube in mp3 format and i converted to .caf format by changing the extension....Thats the reason why i can't play that sound!

Use IBOutlet in Function in Header File

could you please tell me how i can use IBOutlets in a function stored in the header file:
.h file:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface MainView : UIView <AVAudioPlayerDelegate> {
IBOutlet UILabel *SoundDescriptionLabel;
IBOutlet UIBarButtonItem *StopSoundButton;
}
- (IBAction)PlayFatGuyWalking:(id)sender;
- (IBAction)PlayRoadRunner:(id)sender;
- (IBAction)ShowInfoPopup:(id)sender;
- (IBAction)PlayChaosRunning:(id)sender;
- (IBAction)PlaySadTrombone:(id)sender;
- (IBAction)PlayBadJokeSound:(id)sender;
- (IBAction)PlayHorrorSynth:(id)sender;
- (IBAction)PlayLKWPeep:(id)sender;
- (IBAction)PlayUiToll:(id)sender;
- (IBAction)StopSound:(id)sender;
AVAudioPlayer *theAudio;
#end
//PlaySound Function
void playSound(NSString *filename,NSString *type){
[theAudio stop];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:type];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//HERE I WOULD LIKE TO DO THIS:
StopSoundButton.enabled=1;
//END OF WHAT I'D LIKE TO DO
[theAudio play];
}
Please help me
thx in advance
An IBOutlet has no impact or effect on your code - it's #defined out!
It is used as a suggestion to Interface Builder, which can parse the header, so that it knows that that variable can become linked to some graphical control in some way.