我正在使用此函数在我的iPhone应用程序上播放声音。问题是声音没有播放。该函数工作正常,读取文件路径正确,我没有得到任何错误,但声音不播放
-(void) playSound : (NSString *) fName; {
if ([audioPlayer isPlaying]) {
[audioPlayer stop];
}
audioPlayer.volume = 1.0;
soundpath = [NSString stringWithFormat:@"sounds/%@", fName];
soundFilePath = [[NSBundle mainBundle] pathForResource:soundpath ofType: @"mp3"];
soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil){
}
else {
[audioPlayer play];
if ([audioPlayer isPlaying]) {
NSLog(@"%@", @"run");
}
}
}发布于 2012-05-20 19:49:28
尝试添加以下内容:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error: nil];
UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);我有类似的问题,在调用剧本之前添加了这个,它为我解决了这个问题。
https://stackoverflow.com/questions/10673054
复制相似问题