Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-24-2008, 01:42 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 50
Exclamation Help Please, Playing A Short Sound Tutorial Not Working

I've looked at the "Playing A Short Sound" tutorial many times, making sure all my code is correct. My code compiles, but I hear no sound. I think something may have changed with the new SDK..

anyways, here is my code, please help.

MainView.H

Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@class SoundEffect;

@interface MainView : UIView {
    IBOutlet UIImageView *thunderImage;
	SoundEffect *soundEffect;
}

@end
MainView.M

Code:
#import "MainView.h"
#import "SoundEffect.h"

@implementation MainView 

-(void) awakeFromNib {


	NSBundle *mainBundle = [NSBundle mainBundle];
	soundEffect = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"cat" ofType:@"wav"]];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

	[soundEffect play];
}
SoundEffect.H

Code:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>


@interface SoundEffect : NSObject {
	SystemSoundID soundID;
}
- (id) initWithContentsOfFile:(NSString *)path; 
-(void)play;

@end
SoundEffect.M

Code:
#import "SoundEffect.h"


@implementation SoundEffect

- (id) initWithContentsOfFile:(NSString *)path;
{
	self = [super init];
	if (self != nil) {
		NSURL *filePath = [NSURL fileURLWithPath: path isDirectory:NO];
		
		AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
		}
		return self;
}

		
-(void)play { 
			
			AudioServicesPlaySystemSound(soundID); 
		}
		
		
		
		@end
Any help would be greatly appreciated. Thank you

Last edited by GabeJacobs; 08-24-2008 at 01:50 PM.
GabeJacobs is offline   Reply With Quote
Old 08-24-2008, 01:59 PM   #2 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 125
Default

This code works for me:


Code:
SystemSoundID train;

AudioServicesCreateSystemSoundID(CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("train2"), CFSTR("wav"), NULL), &train);

AudioServicesPlaySystemSound(train);
It did take a fair amount of fiddling and googling to get it to work the first time!
bhearn is offline   Reply With Quote
Old 08-24-2008, 02:04 PM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 3
Default

Quote:
Originally Posted by GabeJacobs View Post
I've looked at the "Playing A Short Sound" tutorial many times, making sure all my code is correct. My code compiles, but I hear no sound. I think something may have changed with the new SDK..

anyways, here is my code, please help.

MainView.H

Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@class SoundEffect;

@interface MainView : UIView {
    IBOutlet UIImageView *thunderImage;
	SoundEffect *soundEffect;
}

@end
MainView.M

Code:
#import "MainView.h"
#import "SoundEffect.h"

@implementation MainView 

-(void) awakeFromNib {


	NSBundle *mainBundle = [NSBundle mainBundle];
	soundEffect = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"cat" ofType:@"wav"]];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

	[soundEffect play];
}
SoundEffect.H

Code:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>


@interface SoundEffect : NSObject {
	SystemSoundID soundID;
}
- (id) initWithContentsOfFile:(NSString *)path; 
-(void)play;

@end
SoundEffect.M

Code:
#import "SoundEffect.h"


@implementation SoundEffect

- (id) initWithContentsOfFile:(NSString *)path;
{
	self = [super init];
	if (self != nil) {
		NSURL *filePath = [NSURL fileURLWithPath: path isDirectory:NO];
		
		AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
		}
		return self;
}

		
-(void)play { 
			
			AudioServicesPlaySystemSound(soundID); 
		}
		
		
		
		@end
Any help would be greatly appreciated. Thank you

make sure that u only tab once within braces (statements in your method)
see highlighted red
11111111 is offline   Reply With Quote
Old 08-24-2008, 03:02 PM   #4 (permalink)
I live @ iDevKit.com
 
Join Date: Jul 2008
Posts: 142
Default

I haven't looked at the code, but if you're testing on an iPhone make sure the silent ringer switch isn't set to vibrate.

Max
mxweas is offline   Reply With Quote
Old 08-24-2008, 03:20 PM   #5 (permalink)
mr.
 
refreshe's Avatar
 
Join Date: Jul 2008
Location: SF, California | Melbourne, Australia
Posts: 344
Default

Try using the SoundEffect class files from the metronome example from Apple. It has some console messages that are printed if there's a problem loading sound files etc. It could give you a better idea of what's going wrong.


SoundEffect.h
Code:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>

@interface SoundEffect : NSObject {
    SystemSoundID _soundID;
}

+ (id)soundEffectWithContentsOfFile:(NSString *)aPath;
- (id)initWithContentsOfFile:(NSString *)path;
- (void)play;

@end

SoundEffect.m
Code:
#import "SoundEffect.h"

@implementation SoundEffect
+ (id)soundEffectWithContentsOfFile:(NSString *)aPath {
    if (aPath) {
        return [[[SoundEffect alloc] initWithContentsOfFile:aPath] autorelease];
    }
    return nil;
}

- (id)initWithContentsOfFile:(NSString *)path {
    self = [super init];
    
    if (self != nil) {
        NSURL *aFileURL = [NSURL fileURLWithPath:path isDirectory:NO];
        
        if (aFileURL != nil)  {
            SystemSoundID aSoundID;
            OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &aSoundID);
            
            if (error == kAudioServicesNoError) { // success
                _soundID = aSoundID;
            } else {
                NSLog(@"Error %d loading sound at path: %@", error, path);
                [self release], self = nil;
            }
        } else {
            NSLog(@"NSURL is nil for path: %@", path);
            [self release], self = nil;
        }
    }
    return self;
}

-(void)dealloc {
    AudioServicesDisposeSystemSoundID(_soundID);
    [super dealloc];
}

-(void)play {
    AudioServicesPlaySystemSound(_soundID);
}

@end

Playing sound
Code:
SoundEffect *soundEffect = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"sound1" ofType:@"caf"]];

[soundEffect play];
__________________
appz
refreshe is offline   Reply With Quote
Old 08-24-2008, 05:56 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 50
Default Still not getting it...

@bhearn where would I put that code, and in place of what?

@11111111 the highlighted red does not seem to be different from what I already have.

@mxweas I am testing on the simulator

@refreshe I am not good enough at Objective-C to to see what I am doing wrong. The metronome app is too confusing for me to understand how that can relate to mine.

I am totally stuck....
GabeJacobs is offline   Reply With Quote
Old 08-24-2008, 06:35 PM   #7 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 125
Default

Quote:
Originally Posted by GabeJacobs View Post
@bhearn where would I put that code, and in place of what?
Well to start with, put it anywhere you want a sound played! If it works for you, then you can sort it out into your class structure. Start simple.

I load the sound in my view's initWithFrame: , and store it in a view member.
bhearn is offline   Reply With Quote
Old 08-24-2008, 08:08 PM   #8 (permalink)
mr.
 
refreshe's Avatar
 
Join Date: Jul 2008
Location: SF, California | Melbourne, Australia
Posts: 344
Default

Quote:
Originally Posted by GabeJacobs View Post
@refreshe I am not good enough at Objective-C to to see what I am doing wrong. The metronome app is too confusing for me to understand how that can relate to mine.
Well I posted the relevant code for SoundEffect.h & SoundEffect.m files from the metronome app. Just replace your SoundEffect.h/m code with those.
They're basically the same, except they'll spit out some error messages if they exist.
Open the console when you build your app and see if any error messages show up.

Apart from errors in loading the sound, is the sound file itself compatible?


It should be under 5 seconds or it won't play using this playback method. Also maybe try converting it to apple's preferred .caf format instead of .wav
Apple have instructions in the dev center to go about doing this using a Terminal command.
__________________
appz
refreshe is offline   Reply With Quote
Old 08-24-2008, 08:53 PM   #9 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 50
Default Sorry guys,

Sorry guys, this was totally my fault. The sound file was created wrong. Instead of exporting in wav, I simple renamed it from a mp3. It works now, sorry about that
GabeJacobs is offline   Reply With Quote
Old 08-24-2008, 08:58 PM   #10 (permalink)
Some guy
 
MattCairns's Avatar
 
Join Date: Jul 2008
Location: Canada
Posts: 103
Send a message via AIM to MattCairns
Default

By the way... if you compile it on your iphone make sure its not on vibrate otherwise you won't hear the sound lol.

When I tried playing sounds i seriously thought it was the code and spent like 2 hours trying to figure it out then it turned out my phone was muted lol.
MattCairns is offline   Reply With Quote
Old 08-25-2008, 07:52 PM   #11 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 27
Default

Quote:
Originally Posted by MattCairns View Post
By the way... if you compile it on your iphone make sure its not on vibrate otherwise you won't hear the sound lol.

When I tried playing sounds i seriously thought it was the code and spent like 2 hours trying to figure it out then it turned out my phone was muted lol.
At least your app responds to that mute. I hate it when apps don't!
robertpgg is offline   Reply With Quote
Old 08-30-2008, 10:23 PM   #12 (permalink)
Registered Member
 
bladeolson's Avatar
 
Join Date: Aug 2008
Posts: 31
Send a message via AIM to bladeolson
Default multiple plays?

any way to get it to play the sound in a loop? and stop playing when a button is pressed?
bladeolson is offline   Reply With Quote
Old 09-08-2008, 01:19 PM   #13 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
Default

so can you post the working code? Im still trying to get a simple sound to play on a button press) (3 days later)

tia
rob

Last edited by roberthuttinger; 09-08-2008 at 01:20 PM. Reason: forgot somethin'
roberthuttinger is offline   Reply With Quote
Old 09-08-2008, 04:00 PM   #14 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
Default

So after all this Ive followed the tutorial 2x now and after it goes thru the first wave of build, I get these errors:
HTML Code:
"Linking /Users/foobar/Document/xcode/Debug-iphonesimulator/Sounds.app/Sounds
_AudioServicesPlaySystemSound", referenced from:
-[SoundEffect play] in SoundEffect.o
"_AudioServicesCreateSystemSoundID", referenced from:
-[SoundEffect initWithContentsOfFile:] in SoundEffect.o
symbol(s) not found
:gun to my head: inevitably when I think Ive got it working I hit this wall? any suggestions?

tia rob
roberthuttinger is offline   Reply With Quote
Old 09-08-2008, 04:46 PM   #15 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
Default

So when I added the framework I had copy if needed checked, I deleted the framework, readded, then unchecked the copy if needed box after reimporting. the app works exactly as it should now.

roberthuttinger is offline   Reply With Quote
Old 03-31-2009, 03:52 PM   #16 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 101
Default

Quote:
Originally Posted by roberthuttinger View Post
So when I added the framework I had copy if needed checked, I deleted the framework, readded, then unchecked the copy if needed box after reimporting. the app works exactly as it should now.

glad you added this. i had the same problem and deleted and added without the copy and it worked for me too.

thanks!
MikeBlah is offline   Reply With Quote
Old 03-31-2009, 04:20 PM   #17 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 26
Default

I've found all my sound issues to be much easier using the AVAudioPlayer. It requires 2.2.1, but that's not such a big deal. I've got more information on it here.
__________________
Shablabla
kgodard is offline   Reply With Quote
Old 04-13-2009, 08:02 PM   #18 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 3
Smile

Quote:
Originally Posted by roberthuttinger View Post
So when I added the framework I had copy if needed checked, I deleted the framework, readded, then unchecked the copy if needed box after reimporting. the app works exactly as it should now.

MY! MY! MY!
Fought with this all day before I found this post. Delete and readd worked like a champ.
Thanks
sds65805 is offline   Reply With Quote
Old 08-01-2009, 12:47 AM   #19 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 13
Default Sounds dont work on device

I did everything perfectly like the tutorial said. It works fine, absolutely no errors. I checked the code multiple times and it's exactly the way it should be. The sound works perfectly on the iphone 3.0 SIMULATOR. But when i put it on the device the sounds do not come out. I also checked to make sure the vibrate was OFF.... Any ideas?

thanks!
SammySellers is offline   Reply With Quote
Old 08-14-2009, 07:11 PM   #20 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 267
Default

Quote:
Originally Posted by bhearn View Post
This code works for me:


Code:
SystemSoundID train;

AudioServicesCreateSystemSoundID(CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("train2"), CFSTR("wav"), NULL), &train);

AudioServicesPlaySystemSound(train);
It did take a fair amount of fiddling and googling to get it to work the first time!
I'm running this and I can't get any sound output from my simulator OR iphone. There are no errors or warning though. Maybe my wav file is bad? How can I check that?
ghost is offline   Reply With Quote
Old 08-26-2009, 03:13 AM   #21 (permalink)
shiva
 
shiva.0537's Avatar
 
Join Date: Jun 2009
Location: Hyderabad
Age: 25
Posts: 54
Send a message via Skype™ to shiva.0537
Default

Quote:
Originally Posted by SammySellers View Post
I did everything perfectly like the tutorial said. It works fine, absolutely no errors. I checked the code multiple times and it's exactly the way it should be. The sound works perfectly on the iphone 3.0 SIMULATOR. But when i put it on the device the sounds do not come out. I also checked to make sure the vibrate was OFF.... Any ideas?

thanks!
Some times some formats of sounds play on simulator, but not works on device.. eg: mp3

Change sound format and try.
shiva.0537 is offline   Reply With Quote
Reply

Bookmarks

Tags
play, sound, tutorial

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 263
16 members and 247 guests
14DEV, @sandris, ADY, ArtieFufkin10, bookesp, ckgni, Dani77, DarkAn, HemiMG, iDifferent, jakerocheleau, JasonR, prchn4christ, Rudy, Speed
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:51 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0