2

どんな助けでも本当に感謝しています。下部にある私の質問 (1) と (2) を参照してください。

Flex 開発から来て、Objective C プログラミングは初めてです。簡単な音声再生を初めてやってみました。私は失敗しています。これは、インターネットで答えを探すときによくある問題です。ただし、私が見つけた解決策はどれも機能していないようです。次の方法で短い .caf ファイルを再生しようとしています。

- (void) playUserInterfaceSoundEffect:(NSString *)file ofType:(NSString *)extension
{
  NSLog(@"Call to playerUserInterfaceSoundEffect:%@ ofType:%@", file, extension);

  SystemSoundID soundID = 0;
  NSString *filePath = [NSString stringWithFormat:@"%@.%@", file, extension];
  CFURLRef soundFileURL = (__bridge CFURLRef)[NSURL URLWithString:filePath];
  OSStatus errorCode = AudioServicesCreateSystemSoundID(soundFileURL, &soundID);

  NSLog(@"filePath: %@, soundFileURL: %@, errorCode: %ld", filePath, soundFileURL, errorCode);

  if (errorCode != 0) NSLog(@"ERROR %ld: Failed to initialize UI audio file %@", errorCode, filePath);

  AudioServicesPlaySystemSound(soundID);
}

私はそれを次のように呼んでいます:

// Start with playing the audio effect for correct answer:
[self playUserInterfaceSoundEffect:@"answer_correct" ofType:@"caf"];

ただし、サウンドは再生されません。ログ トレース ステートメントで確認できるように、システム サウンド ID を作成しようとすると、エラー コード -1500 が返されました。

2013-09-10 17:50:48.498 InFocus-dev[25888:c07] Call to playerUserInterfaceSoundEffect:answer_correct ofType:caf 
2013-09-10 17:50:48.498 InFocus-dev[25888:c07] filePath: answer_correct.caf, soundFileURL: answer_correct.caf, errorCode: -1500 
2013-09-10 17:50:48.498 InFocus-dev[25888:c07] ERROR -1500: Failed to initialize UI audio file answer_correct.caf

ここで説明されているように、明らかにそのエラー コードはかなり一般的なものです: (OSStatus) error = -1500 in AudioToolbox Framework -- kAudioServicesSystemSoundUnspecifiedError の略で、オーディオ ファイルがおそらく正しい形式ではないか、見つからないことを意味する場合があります。

私の質問は次のとおりです。

(1) 音声ファイルは正しい形式である必要がありますが、どうすれば確認できますか? この短い投稿は、一部のファイルがこのエラーを引き起こす可能性があることを示唆しているようですが、理由は明らかではありません: http://www.dosomethinghere.com/2009/05/05/audioservicescreatesystemsoundid-error-1500/

(2) アプリケーションはファイルを見つけることができるはずですが、どうすれば確認できますか? それらは resources/assets/sounds/ にあります。たとえば、 resources/assets/icons/ にある画像は問題なく見つかります。アプリケーションが単にファイルをまったく見つけられないかどうかをテストするにはどうすればよいですか?

私はそのようなN00bのように感じます-助けてくれてありがとう!

エリック


私は StackOverflow を初めて使用するので、まだ自分の質問に答えることができませんが、次のとおりです。

さて、@borrrden が指摘したように、答えは NSBundle を使用して完全なファイル パスを取得することです。

メソッドを次のように書き直したところ、問題が修正されました。

- (void) playUserInterfaceSoundEffect:(NSString *)file ofType:(NSString *)extension
{
  NSLog(@"Sound FX: Call to playerUserInterfaceSoundEffect:%@ ofType:%@", file, extension);

  NSString *filePath  = [[NSBundle mainBundle] pathForResource:file ofType:extension];
  NSURL *fileURL      = [NSURL fileURLWithPath:filePath];
  CFURLRef inFileURL  = (CFURLRef)CFBridgingRetain(fileURL);

  SystemSoundID soundID;
  OSStatus errorCode  = AudioServicesCreateSystemSoundID(inFileURL, &soundID);

  NSLog(@"\nfilePath:\t%@\nfileURL:\t%@\ninFileURL:\t%@", filePath, fileURL, inFileURL);

  if (errorCode != 0) NSLog(@"ERROR %ld: Failed to initialize UI audio file %@", errorCode, filePath);

  AudioServicesPlaySystemSound (soundID);
}

このログ トレースは次のようになります。

2013-09-10 19:58:56.165 InFocus-dev[27986:c07] Sound FX: Call to playerUserInterfaceSoundEffect:answer_correct ofType:caf  
2013-09-10 19:58:56.165 InFocus-dev[27986:c07] 
filePath:   /Users/erik/Library/Application Support/iPhone Simulator/6.1/Applications/5A8B1CC8-2E32-4469-80C2-FA311242B5C5/InFocus-dev.app/answer_correct.caf
fileURL:    file://localhost/Users/erik/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/5A8B1CC8-2E32-4469-80C2-FA311242B5C5/InFocus-dev.app/answer_correct.caf
inFileURL:  file://localhost/Users/erik/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/5A8B1CC8-2E32-4469-80C2-FA311242B5C5/InFocus-dev.app/answer_correct.caf

ほっとした..!

ありがとう、エリック

4

1 に答える 1