オーディオファイルを再生するための非常に単純なコードを書き込もうとしています。コードは iPad2 シミュレーターで正常に動作します。しかし、私の iPad2 デバイスでは、[audooPlayer play] は false を返し、音声は聞こえません。
.caf ファイルと .wav ファイルの両方を試しました。iPad2 を再起動し、ミュート スイッチがオフになっていることを確認しました。デバッグ方法に関するヒントは大歓迎です。
サンプルコード:
- (void)playAudio {
NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];
url = [NSURL fileURLWithPath:file];
NSError *error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error != nil) {
NSLog(@"Error creating audio player: %@", [error localizedDescription]);
return;
}
[audioPlayer setDelegate:self];
if (![audioPlayer prepareToPlay]) {
NSLog(@"Error preparing audio");
return;
}
// Everything works fine up to this point
if (![audioPlayer play]) {
// I hit this on hardware. Works perfect on the simulator.
NSLog(@"Error playing audio");
return;
}