【问题标题】:The volume of AVAudioPlayer is not as same as the system volumeAVAudioPlayer 音量与系统音量不一样
【发布时间】:2015-03-02 02:57:50
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c avaudioplayer volume avaudiosession


    【解决方案1】:

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

    【讨论】:

    • [self prepareAudioSession]; 之后我应该在哪里调用此代码?这种方法是否使音量为最大音量?
    • 在[audioPlayer播放]之前;它将音量设置为最大,因为我们使用 1.0
    • 但是我不要最大音量,我要使用系统音量。
    • 尝试先从下面的代码中获取,然后再设置回。Float32 体积; UInt32 dataSize = sizeof(Float32); Float32 currentVolume= AudioSessionGetProperty ( 'chov', &dataSize, &volume );
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2011-07-25
    • 2012-09-22
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    相关资源
    最近更新 更多