当 iPhone 音量完全调低至静音时,音频仍会播放

发布于 2024-12-12 15:51:17 字数 1182 浏览 2 评论 0原文

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

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

最奇怪的部分: 当 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];
}

任何想法将不胜感激。

The way I am currently playing background music and other sounds in my app is behaving very strangely:

  • Turn the background music off, and the other sounds played are MUCH louder.
  • With the background music on, the the audio is MUCH quieter.
  • You can even turn the music on mid-sound, and the sound gets quieter.

The strangest part:
When the 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't hear music or sound effects. But if the background music is turned off, the sound effects are still played and quite loudly even though the device itself is turned all the way down!

Here is my code...

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

Any ideas would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后知后觉 2024-12-19 15:51:18

也许是因为您使用 AVAudioPlayer 来播放音乐,并使用 SystemSounds 来再现声音效果。 AVAudioPlayer 的音量设置在应用程序中,但 SystemSounds 的音量设置是 systemVolume。

Maybe it is because you are using AVAudioPlayer for your music and SystemSounds to reproduce your sound effects. The volume set for AVAudioPlayer is in the app, but the volume set for SystemSounds is the systemVolume.

一花一树开 2024-12-19 15:51:18

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

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

此代码初始化 AVAudioSession 并将其设置为声音不会与其他背景声音混合的类别(它们一旦您激活会话,就会停止)并且您的应用程序具有优先权。请注意,屏幕锁定和静音开关仍将允许播放声音(以最大音量)。我相信你可以具体设置,但我不知道确切的代码。

如果您想要不同的音频行为,请查看您可以在 Apple 的 AVAudioSession 类文档。其他选项是:

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

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

You need to set your AVAudioSession before playback of sounds using the AVAudioPlayer and MPMediaPlayer in your app, and there are different categories for it based on how you want the audio to interact. While I have no idea why the music would still play even on zero volume, the other problems seem to be from an AVAudioSessionCategory setting you don't want. In the initialization of your app or wherever you begin to play sounds, try the following code:

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

This code initializes the AVAudioSession and sets it to a category where the sounds won't mix with other background sounds (they'll be stopped once you activate the session) and where your app has priority. Note that screen locking and the silence switch will still allow sounds to play (at their full volume). You can set that specifically, I believe, but I don't know the exact code.

If you want different behavior for audio, check out the other categories you could set it to in Apple's documentation of the AVAudioSession Class. Other options are:

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

Anyway, hopefully the audio session issue was causing those problems. Let me know if that worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文