【问题标题】:Audio still plays when iPhone volume is turned all the way down to silent将 iPhone 音量调低至静音时仍会播放音频
【发布时间】:2011-12-18 12:20:51
【问题描述】:

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

  • 关闭背景音乐,播放的其他声音会更大。
  • 打开背景音乐后,声音会小很多。
  • 您甚至可以将音乐调到中音,声音会变得更安静。

最奇怪的部分: 当 iPhone 的音量一直被调低(静音)时,应该根本没有声音。在背景音乐打开但没有设备音量的情况下,它会如您所愿 - 您听不到音乐或声音效果。 但是如果关闭背景音乐,即使设备本身完全关闭,声音效果仍然会播放并且声音很大!

这是我的代码...

我的背景音乐:

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];
}

播放过程中的声音:

#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];
}

任何想法都将不胜感激。

【问题讨论】:

  • 你在更多设备上测试过这个吗?跨设备是否一致?
  • 这不会发生在 iPad(第一代,iOS 5)上。这是迄今为止我能够测试的唯一其他设备。发生这种情况的 iPhone 也是 iOS 5。
  • 好吧,这让你对这个问题投了赞成票。确实感觉是您的设备有问题
  • 你的 AVAudioSessionCategory 设置是什么?
  • 没有看到你的代码,我不知道,但尝试阅读:stackoverflow.com/questions/1659167/…

标签: iphone audio avaudioplayer soundeffect


【解决方案1】:

您需要在播放声音之前在您的应用中使用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;

无论如何,希望音频会话问题导致了这些问题。让我知道这是否有效。

【讨论】:

    【解决方案2】:

    可能是因为您使用 AVAudioPlayer 播放音乐,使用 SystemSounds 再现音效。为 AVAudioPlayer 设置的音量在应用程序中,但为 SystemSounds 设置的音量是 systemVolume。

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 2022-12-31
      • 2012-05-17
      相关资源
      最近更新 更多