PDA

View Full Version : Sound won't play - Using AudioToolbox




kwjohns
Feb 16, 2009, 10:04 AM
I'm trying to play a sound but it's not working.


if (sound == @"Song Name") {
UIImage *image = [UIImage imageNamed: @"picture.png"];
lyrics = [[UIImageView alloc] initWithImage: image];
[self.view addSubview:lyrics];
// Create pathname to sound file
NSString *sndPath = [[NSBundle mainBundle]
pathForResource:@"song"
ofType:@"aif"
inDirectory:@"/"];
// Create URL
CFURLRef sndURL = (CFURLRef)[[NSURL alloc]
initFileURLWithPath:sndPath];

// Create system sound ID
AudioServicesCreateSystemSoundID(sndURL, &ssid);
NSLog(@"%@\n", sndURL);
NSLog(@"%@\n", ssid);
AudioServicesPlaySystemSound(ssid);

}


I don't get any errors and the app runs fine but just no audio plays. The console shows this with my logs:

2009-02-16 10:01:39.407 Program[1370:20b] file://localhost/Users/username/Library/Application%20Support/iPhone%20Simulator/User/Applications/37D00767-4E08-4DCB-BA37-99B5BCBE2DC1/Program.app///song.aif
2009-02-16 10:01:39.408 Program[1370:20b] (null)

ssid is showing up as null? Any help is appreciated!



jnic
Feb 16, 2009, 10:11 AM
Try replacing

[[NSBundle mainBundle] pathForResource:@"song" ofType:@"aif" inDirectory:@"/"];


with

[[NSBundle mainBundle] pathForResource:@"song" ofType:@"aif"];

kwjohns
Feb 16, 2009, 11:17 AM
Try replacing

[[NSBundle mainBundle] pathForResource:@"song" ofType:@"aif" inDirectory:@"/"];


with

[[NSBundle mainBundle] pathForResource:@"song" ofType:@"aif"];


I got the same outcome.

jnic
Feb 16, 2009, 11:42 AM
It's working fine for me. You are running on the device and not simulator, right?

For reference, here's the exact code I'm using:


NSString *sndPath = [[NSBundle mainBundle] pathForResource:@"tock" ofType:@"caf"];
CFURLRef sndURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndPath];
SystemSoundID ssid;
AudioServicesCreateSystemSoundID(sndURL, &ssid);
AudioServicesPlaySystemSound(ssid);

kwjohns
Feb 16, 2009, 11:52 AM
Nope, I am running it on the simulator. I haven't paid the fee to be able to run it on my phone. Was wanting to test the waters with some of this stuff before I jumped in by paying. Should ssid be returning null, though?

kwjohns
Feb 16, 2009, 12:42 PM
I got it working (and with mp3s!) using a different method found here (http://idevgames.com/forum/showthread.php?t=15280).