4

我正在使用此功能在我的 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");
     }
}
}
4

1 回答 1

5

尝试添加这个:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error: nil];
UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);

我遇到了类似的问题,并在调用该游戏之前添加了它,它为我解决了这个问题。

于 2012-05-20T11:49:28.473 回答