简体   繁体   English

AVAudioPlayer的音量与系统音量不同

[英]The volume of AVAudioPlayer is not as same as the system volume

I use AVAudioPlayer to play sound, but sometimes the volume becomes very low, and not as same as the system volume. 我使用AVAudioPlayer播放声音,但有时音量变得很低,与系统音量不一样。 When I change the system volume by press the volume button, it becomes normal. 当我通过按音量按钮更改系统音量时,它变为正常。 How does it happen? 它是怎么发生的?

the code is below: 代码如下:

- (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);
    }
});}

You haven't called [audioPlayer setVolume: 1.0]; 您尚未调用[audioPlayer setVolume: 1.0]; to set the Volume in your code. 在代码中设置音量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM