ios:系统声音在模拟器中播放但在iphone上播放

时间:2011-08-05 22:50:54

标签: ios audio simulator

我有以下代码:

 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, &outSystemSoundID );
    AudioServicesPlaySystemSound( outSystemSoundID );
    [pool drain];
}

模拟器和iphone中的输出是这样的:

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

该代码的问题:当我在模拟器中运行时,一切正常,但是当我在iphone上运行时没有声音播放? (方法playound不是从主线程中调用,因此是分离的自动释放池)

1 个答案:

答案 0 :(得分:1)

要播放设备中的短时间声音文件,请尝试aif或aiff(音频交换文件格式,即未压缩的脉冲编码调制(PCM))格式的声音文件,而不是caf(核心音频格式)格式文件。

您可以使用任何转换软件将caf转换为aiff。网上有很多可用的。我试过的一个是Bigasoft Audio Converter的试用版。

在您的设备中尝试此操作后,请告诉我结果。一切都好。

相关问题