AVAudioPlayer成功调用了播放方法,并且在模拟器中正常工作,但它没有播放声音。该设备具有iOS 7.1.1。这是我用过的代码。
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"miata-hn" ofType:@"wav"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSError *error = nil;
/* Start the audio player */
self.playerAudio = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
self.playerAudio.volume = 1;
if (error)
{
NSLog(@"error localized description %@",[error localizedDescription]);
}
if (self.playerAudio != nil)
{
/* Set the delegate and start playing */
self.playerAudio.delegate = self;
if ([self.playerAudio prepareToPlay] && [self.playerAudio play])
{
NSLog(@"player success");
}
else
{
NSLog(@"sound player failed to play audio");
}
}
else
{
/* Failed to instantiate AVAudioPlayer */
}
答案 0 :(得分:3)
我得到了解决方案。添加此行代码后,可在应用程序中听到音频。 AVAudioSession应该被激活。
**
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
**
答案 1 :(得分:0)
尝试使用:
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];