Sound won't play - Using AudioToolbox

Discussion in 'iOS Programming' started by kwjohns, Feb 16, 2009.

  1. macrumors 6502a

    kwjohns

    Joined:
    Jul 4, 2007
    #1
    I'm trying to play a sound but it's not working.

    Code:
    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:

    Code:
    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!
     
  2. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    Try replacing

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

    Code:
    [[NSBundle mainBundle] pathForResource:@"song" ofType:@"aif"];
    
     
  3. thread starter macrumors 6502a

    kwjohns

    Joined:
    Jul 4, 2007
    #3
    I got the same outcome.
     
  4. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #4
    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:

    Code:
    NSString *sndPath = [[NSBundle mainBundle] pathForResource:@"tock" ofType:@"caf"];
    CFURLRef sndURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndPath];
    SystemSoundID ssid;
    AudioServicesCreateSystemSoundID(sndURL, &ssid);	
    AudioServicesPlaySystemSound(ssid);
     
  5. thread starter macrumors 6502a

    kwjohns

    Joined:
    Jul 4, 2007
    #5
    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?
     
  6. thread starter macrumors 6502a

    kwjohns

    Joined:
    Jul 4, 2007
    #6
    I got it working (and with mp3s!) using a different method found here.
     

Share This Page