首页 文章

当iPhone音量一直变为静音时,音频仍会播放

提问于
浏览
10

我目前在我的应用程序中播放背景音乐和其他声音的方式非常奇怪:

  • 关闭背景音乐,播放的其他声音更响亮 .

  • 随着背景音乐的开启,音频更加安静 .

  • 你甚至可以在中音时转动音乐,声音也会变得更安静 .

The strangest part: 当iPhone 's volume is turned all the way down (muted) there should be no sounds at all. With the background music on but no device volume, it does what you would expect - you can'听到音乐或声音效果时 . 但是如果关闭背景音乐,即使设备本身完全关闭,声音效果仍会播放并且非常响亮!

这是我的代码......

For my background music:

AVAudioPlayer *musicPlayer;

- (void)playMusic {
    if (musicPlayer == nil) {
        NSURL *musicPath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SongFile" ofType:@"mp3"]];
        musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicPath error:nil];
        musicPlayer.volume = 0.2f;
        musicPlayer.numberOfLoops = -1;
    }
    [musicPlayer play];
}

- (void)stopMusic {
    [musicPlayer stop];
}

For sounds during play:

#import "SoundEffect.h"
SoundEffect *sounds;

- (void)playSoundWithInfo:(NSString *)sound; {
    NSString *path = [[NSBundle mainBundle] pathForResource:sound ofType:@"caf"];
    sounds = nil;
    sounds = [[SoundEffect alloc] initWithContentsOfFile:path];
    [sounds play];
}

任何想法将不胜感激 .

2 回答

  • 1

    也许是因为你正在使用AVAudioPlayer为你的音乐和SystemSounds重现你的声音效果 . 为AVAudioPlayer设置的卷在应用程序中,但为SystemSounds设置的卷是systemVolume .

  • 0

    您需要在使用应用程序中的 AVAudioPlayerMPMediaPlayer 播放声音之前设置 AVAudioSession ,并根据您希望音频的交互方式有不同的类别 . 虽然我不知道为什么音乐即使在零音量上仍然可以播放,但其他问题似乎来自于你不想要的 AVAudioSessionCategory 设置 . 在您的应用初始化或您开始播放声音的任何地方,请尝试以下代码:

    [[AVAudioSession sharedInstance] setDelegate:self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    

    此代码初始化 AVAudioSession 并将其设置为一个类别,一旦激活会话,声音就会被停止,并且您的应用程序具有优先级 . 请注意,屏幕锁定和静音开关仍然可以播放声音(全音量) . 我相信你可以专门设置,但我不知道确切的代码 .

    如果您想要音频的不同行为,请查看您可以在Apple's documentation of the AVAudioSession Class中设置的其他类别 . 其他选择是:

    AVAudioSessionCategoryAmbient;
    AVAudioSessionCategorySoloAmbient;
    AVAudioSessionCategoryPlayback;
    AVAudioSessionCategoryRecord;
    AVAudioSessionCategoryPlayAndRecord;
    AVAudioSessionCategoryAudioProcessing;
    

    无论如何,希望音频会话问题导致了这些问题 . 如果有效,请告诉我 .

相关问题