04-23-2009, 10:15 PM
#1 (permalink )
New Member
Join Date: Apr 2009
Posts: 12
Help with playing wav sound
Hello, I'm developing a utilitie for iPhones, and I want to make this app play a wav sound. I've created a SystemSound soundID*, and I've done everything that apple documentation tells me to do. Can somebody help me ? The main code is:
Code:
#import "ShakeAndBreakViewController.h"
@implementation ShakeAndBreakViewController
@synthesize imageView,brokenShow,fixed,broken,takePictureButton,imageShow,showButton;
#import <AudioToolbox/AudioToolbox.h>
- (void)viewDidLoad {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
takePictureButton.hidden = YES;
}
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = kUpdateInterval;
NSString *path = [[NSBundle mainBundle] pathForResource:@"glass" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
self.fixed = [UIImage imageNamed:@"home.png"];
self.broken = [UIImage imageNamed:@"vidro.png"];
imageView.image = fixed;
brokenShow.hidden = YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[showButton release];
[imageView release];
[imageShow release];
[takePictureButton release];
[brokenShow release];
[fixed release];
[broken release];
[super dealloc];
}
#pragma mark -
- (IBAction)getCameraPicture:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = (sender == takePictureButton) ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.allowsImageEditing = YES;
[self presentModalViewController:picker animated:YES];
[picker release];
showButton.hidden = YES;
}
- (IBAction)selectExistingPicture {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
showButton.hidden = YES;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing photo library" message:@"Device does not support a photo library" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
[alert show];
[alert release];
showButton.hidden = YES;
}
}
#pragma mark -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
imageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
showButton.hidden = YES;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration {
if(! brokenScreenShowing) {
if (acceleration.x > kAccelerationThreshold
|| acceleration.y > kAccelerationThreshold
|| acceleration.z > kAccelerationThreshold) {
brokenShow.hidden = NO;
AudioServicesPlaySystemSound (soundID);
showButton.hidden = YES;
}
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
imageView.image = fixed;
brokenShow.hidden = YES;
showButton.hidden = NO;
}
@end
OBS: I've already added the AudioToolbox.framework, and the sound file to the resources folder.
Thanks !
Last edited by Negativi; 04-23-2009 at 10:15 PM .
Reason: Fixed the code
04-23-2009, 11:17 PM
#2 (permalink )
New Member
Join Date: Apr 2009
Posts: 12
Help please !
04-24-2009, 12:17 AM
#3 (permalink )
New Member
Join Date: Apr 2009
Posts: 12
Help !!
01-29-2010, 05:44 AM
#4 (permalink )
Registered Member
Join Date: Jan 2010
Posts: 1
Idea for you to try
Sorry for the delay but I have just started developing on the iPhone and have just stumbled upon the same issue. Have you tried adding the Frameworks in the groups and files section. Basically where all the folders and resources are, there is one named Frameworks, in there add an existing framework and add the Audio toolbox one.
01-29-2010, 08:25 AM
#5 (permalink )
Registered Member
Join Date: Jul 2009
Posts: 172
This is a simple code to play a wav file. Add AVFoundation and AudioToolbox framework to your project.
Code:
NSString *pathForSilentFile = [[NSBundle mainBundle] pathForResource:@"noSound" ofType:@"wav"];
NSURL *soundFile = [[NSURL alloc] initFileURLWithPath:pathForSilentFile];
AVAudioPlayer *sPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:NULL];
[soundFile release];
[sPlayer prepareToPlay];
[sPlayer play];
note that i have a noSound.wav file in my project.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 356
21 members and 335 guests
ADY , Dani77 , Duncan C , e2applets , HemiMG , Herbie , JasonR , keeshux , linkmx , macquitzon216 , mer10 , Monstertaco , prchn4christ , Promo Dispenser , Robiwan , sebasx , sly24 , Touchmint , twerner , zulfishah
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,881
Threads: 89,228
Posts: 380,760
Top Poster: BrianSlick (7,129)
Welcome to our newest member, macquitzon216