Obj-C ios AudioToolkit не воспроизводит звук - PullRequest
2 голосов
/ 19 марта 2012

Когда я жестко кодирую имя звука, оно работает, но когда я использую генератор случайных чисел для создания строки, это не так. Кстати: это в Xcode 4.3 и симуляторе iPhone 5.1.


int rand = round(arc4random_uniform(3));
char buf[100];
sprintf(buf,"Sound%d.aifc",rand);
//get the filename of the sound file
NSString *fp=[NSString stringWithUTF8String:buf];
NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], fp];
//declare a system sound
SystemSoundID soundID;

//get a path for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

//create the sound id
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);

//play the sound
AudioServicesPlaySystemSound(soundID);

1 Ответ

0 голосов
/ 27 февраля 2013

Когда я запускаю -[NSBundle mainBundle] на моей машине, он не включает косую черту.

Измените эту строку:

NSString *path = [NSString stringWithFormat:@"%@%@", 
  [[NSBundle mainBundle] resourcePath], fp];

На это:

NSString *path = [[[NSBundle mainBundle] resourcePath] 
  stringByAppendingPathComponent:fp];

Это будет правильно делать, если NSBundle добавляет косые черты или нет.

...