我目前在应用程序中播放背景音乐和其他声音的方式非常奇怪:
最奇怪的部分是:,当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];
}任何想法都将不胜感激。
发布于 2011-12-14 03:55:44
也许是因为你正在使用AVAudioPlayer作为你的音乐和SystemSounds来再现你的音效。AVAudioPlayer的卷集在应用程序中,但是SystemSounds的卷集是systemVolume。
发布于 2012-01-02 23:41:44
您需要在使用应用程序中的AVAudioSession和MPMediaPlayer回放声音之前设置您的AVAudioPlayer,并且根据您希望音频交互的方式对其进行不同的分类。虽然我不知道为什么音乐仍然会在零音量下播放,但其他问题似乎来自你不想要的AVAudioSessionCategory设置。在应用程序的初始化或开始播放声音的任何地方,请尝试以下代码:
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];这段代码初始化AVAudioSession并将其设置为一个类别,在这个类别中,声音不会与其他背景声音混合(一旦激活会话,它们就会停止),并且应用程序具有优先权。请注意,屏幕锁定和静音开关仍然允许声音播放(在他们的全部音量)。我相信你可以特别设定,但我不知道确切的代码。
如果您想要不同的音频行为,请查看可以在苹果的AVAudioSession类文档中将其设置为的其他类别。其他选择包括:
AVAudioSessionCategoryAmbient;
AVAudioSessionCategorySoloAmbient;
AVAudioSessionCategoryPlayback;
AVAudioSessionCategoryRecord;
AVAudioSessionCategoryPlayAndRecord;
AVAudioSessionCategoryAudioProcessing;不管怎么说,希望音频会话问题引起了这些问题。如果成功的话请告诉我。
https://stackoverflow.com/questions/7950283
复制相似问题