How to implement sound into an existent app, without Cocos2D - ios

For example, on the iOS SDK download page, there's sample code; I'm using the calculator app (iPhoneUnitTests). I would like to know if it is possible to add sounds to the button presses on the app that's already built, easily.

It is actually very simple to play short sounds, like button sounds. Here is a quick example.
You must link the AudioToolbox.framework binary
SoundExampleViewController.h;
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface SoundExampleViewController : UIViewController{
SystemSoundID mySoundID;
}
#end
SoundExampleViewController.m:
#import "SoundExampleViewController.h"
#implementation SoundExampleViewController
- (void)viewDidLoad{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"SoundFileName" ofType:#"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&mySoundID);
}
- (void)viewDidUnload{
[super viewDidUnload];
AudioServicesDisposeSystemSoundID(mySoundID);
}
#end
Then to play you simply call: AudioServicesPlaySystemSound(mySoundID);

Related

iOS MPMoviePlayerViewController only works on debug not in final

I implemented a MPMoviePlayerViewControllerlike this:
ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UITableViewController
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
/* SOME CODE AND A TAPGESTURERECOGNIZER */
/* CALLS [self playVideo] */
-(void) playVideo{
#try {
NSURL *URL = [NSURL URLWithString:#"myGeneratedURL"];
NSLog(#"URL: %#",URL);
MPMoviePlayerViewController *video = [[MPMoviePlayerViewController alloc]
initWithContentURL:URL];
[self.navigationController presentMoviePlayerViewControllerAnimated:video];
}
#catch (NSException *exception) {
}
}
So now my problem is that it works just fine on both simulator and on my iphone (if i debug it), but when i upload a build to itunesconnect and download it via TestFlight the MPMoviePlayerViewController does not show up. The URL in NSLog is definitely the same in all three cases ( sim , iphone debug, testflight ).
I don't know where i should start searching the error...
I would suggest using strong reference rather than an local variable.
You can follow this tutorial

Trying to create a button in Xcode Objective-C

I am just trying something completely new and simple in Xcode. I was looking a tutorial to create a button that plays music or a sound. All seemed to be okay until preview time. There seems to be an expected identifier issue at IBAction as well as it expected something after (void). But each time I do as the prompt says the prompts multiply. I've tried to do as all of them said but then it was no longer the same function. Any ideas?
#import "ViewController.h"
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface StrackerViewController : ViewController
//#end
//#implementation StrackerViewController: ViewController
- (IBAction) epic; {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) # "epic", CFSTR ("mp3"),NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
- (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
Your IBAction is not a properly formed function. It needs to be in the form:
-(IBAction) epic:(id)sender {
Once you have done that you should probably delete and remake the link with your storyboard as that is likely to be broken too.

Audio playback code broke with new version of Xcode

first I have to say, I'm a beginner with iOS development. I would appreciate any help.
I've made a code that plays sounds when certain buttons are touched, it worked fine in previous Xcode version, but in this new one, no sounds are played. During the building of code no errors are reported and everything else works fine when I turn on simulator for iPhone 4s, that one is target for this code. Please help.
Here is my code:
file.h
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <CoreLocation/CoreLocation.h>
#interface DataViewController : UIViewController <CLLocationManagerDelegate> {
int nob, nof, nod;
CLLocationManager *locationManager;
}
#property (strong, nonatomic) IBOutlet UILabel *maxDistanceLabel;
#property (nonatomic,strong) NSArray *fetchedSettingsArray;
#property (nonatomic,retain) NSNumber *numB;
- (IBAction)forwardB:(id)sender;
- (IBAction)backB:(id)sender;
#property (strong, nonatomic) IBOutlet UILabel *numberB;
#end
file.m (just part where the audio needs to play)
- (IBAction)forwardB:(id)sender {
nob ++;
NSString *nobStr=[NSString stringWithFormat:#"%d",nob];
[numberB setText:nobStr];
numB=[NSNumber numberWithInt:nob];
//sound at button touch
NSString *effectTitle;
effectTitle =#"Buckklick";
SystemSoundID soundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:effectTitle ofType:#"wav"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID ((__bridge CFURLRef)soundUrl, &soundID);
AudioServicesPlaySystemSound(soundID);
}
I found explanation in some other discussion. Apparently simulator is not working properly.
And I found solution, simply downloading Xcode 5.1.1 to finish my project.

xcode5 - playing sound after pushing button is not working

OBJECTIVE :
I am trying to get a sound to play when a button is clicked. This sound will be preformed by an action called "play". The sound is in my resource folder and is called SoftClick.mp3
PROBLEM:
There is no sound at all being played in either simulator or the real device. The code does not give any errors.
CONNECTIONS:
The button, that needs to give sound, is connected to the action play and is used as a Modal Segue to show another view controller. Both viewcontrollers use the same .h and .m script.
CODE FOR .H
#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController : UIViewController
{
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (IBAction) play;
#end
Action and audioplayer are defined as i thought was necessary (?)
THE CODE FOR .M
#import "ViewController.h"
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController ()
#end
#implementation ViewController
#synthesize audioPlayer;
-(IBAction)play
{
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"SoftClick"ofType:#"mp3"]] error:NULL];
[theAudio play];
}
Can someone please help me with correcting my coding so this could work in xcode5?
Ive spend some hours trying to figure out why this will not work an tried combinations like without segue etc. I do not want to steal the code from someone else and am trying to build this on my own.
Thanks in advanced,
Lien
The issue, as I'm assuming you're using ARC, is that you aren't retaining the AVAudioPlayer object. You create the object, call play, but then it is instantly released and dealloc'd. You need to add self.audioPlayer = theAudio;

iOS: Playing a video on application launch

I am new to Xcode so I've been trying to follow along to tutorials but haven't come across any that explain what I'm trying to achieve. I just want to play a video automatically when you open an application. I have it somewhat working in that the audio plays, but I cannot see any video. Am I missing something? I am getting this Warning in my output window:
Warning: Attempt to present MPMoviePlayerViewController: 0x831d7a0 on ViewController: 0x9d10540 whose view is not in the window hierarchy!
In my ViewController.h:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Placeholder" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Instead of
[playercontroller.moviePlayer play];
do
[playercontroller play];
Side note:
Also check up on your naming convention. In Objective-C variable names "should" have capital letter on each new word in the variable. E.g. instead of playercontroller use playerController.
I know your status.
it's warning from Xcode.
So, you just change the code.
like this,
[playercontroller performSelector:#selector(player)];

Resources