Ran*_*rns 10 iphone audio avaudioplayer soundeffect
我目前在我的应用程序中播放背景音乐和其他声音的方式非常奇怪:
最奇怪的部分: 当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];
}
Run Code Online (Sandbox Code Playgroud)
对于播放期间的声音:
#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];
}
Run Code Online (Sandbox Code Playgroud)
任何想法将不胜感激.
小智 1
也许是因为您使用 AVAudioPlayer 来播放音乐,并使用 SystemSounds 来再现声音效果。AVAudioPlayer 的音量设置在应用程序中,但 SystemSounds 的音量设置是 systemVolume。