AVAudioPlayer的音量与系统音量不同

时间:2015-03-02 02:57:51

标签: ios objective-c avaudioplayer volume avaudiosession

我使用AVAudioPlayer播放声音,但有时音量变得非常低,与系统音量不同。当我通过按音量按钮更改系统音量时,它变得正常。它是如何发生的?

代码如下:

- (void)prepareAudioSession {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *setCategoryError = nil;
    if (![session setCategory:AVAudioSessionCategoryPlayback
              withOptions:AVAudioSessionCategoryOptionMixWithOthers
                    error:&setCategoryError]) {
        // handle error
    }

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *error = nil;
    BOOL result = [audioSession setActive:YES withOptions:0 error:&error];

    if (!result && error) {
        // deal with the error
    }
}

- (void)playWithAudioPath:(NSString *)path {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    NSError *error;
    NSURL *audioFileURL = [NSURL fileURLWithPath:path];
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];

    audioPlayer.delegate = self;

    if (error == nil) {

        [self prepareAudioSession];

        [audioPlayer setCurrentTime:0];
        [audioPlayer play];
    }
    else {
        NSLog(@"%@", error.description);
    }
});}

1 个答案:

答案 0 :(得分:-1)

您尚未调用[audioPlayer setVolume: 1.0];来设置代码中的音量。