开发者

ios: system sound played in the simulator but not on the iphone

I've got the following code:

 soundFilenames = [[NSArray alloc] initWithObjects:@"Alarm Clock Bell.caf", @"Bark.caf", @"Cartoon Boing.caf", @"Chimpanzee Calls.caf", @"School Bell Ringing.caf", @"Sheep Bah.caf", @"Squeeze Toy.caf", @"Tape Rewinding.caf", nil];

...

-(void)playSound {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int index = rand() % [soundFilenames count];
    NSString* filePath = [ [self getBasePath] stringByAppendingPathComponent:     [soundFilenames objectAtIndex:index] ];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if( [fileManager fileExistsAtPath:filePath] )
        NSLog(@"File %@ exists", [soundFilenames objectAtIndex:index]);
    else
        NSLog(@"File %@ NOT exists", [soundFilenames objectAtIndex:index]);

    CFURLRef url = CFURLCreateWithFileSystemPath ( 0, (CFStringRef) filePath, kCFURLPOSIXPathStyle, NO );
    SystemSoundID outSystemSoundID;
    AudioServicesCreateSystemSoundID( url, &outSystemSou开发者_StackOverflow中文版ndID );
    AudioServicesPlaySystemSound( outSystemSoundID );
    [pool drain];
}

The output in the simulator and the iphone is something like this:

2011-08-06 00:46:45.323 Alarm[199:5f03] File Cartoon Boing.caf exists

The problem with that code: When I run this in the simulator everything works fine the sounds are being played but when I run it on my iphone no sound is played? (The method playsound is not called from within the main thread therefore the separated autorelease pool)


To play the short duration sound files in your device, try aif or aiff (Audio Interchange File Format which is uncompressed pulse-code modulation (PCM)) format sound files instead of caf (Core Audio Format) format files.

You can convert caf to aiff by using any conversion software. There are many available online. One such I tried is the trial version of Bigasoft Audio Converter.

After trying this in your device, please let me know the results. All the Best.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜