簡體   English   中英

AVAudioPlayer的音量與系統音量不同

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

我使用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);
    }
});}

您尚未調用[audioPlayer setVolume: 1.0]; 在代碼中設置音量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM