我正在做一个soundboard应用程序,一切都很好,但我有一个问题,如果铃声关闭,声音就不会播放。
我已经尝试过了:
[super viewDidLoad];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];我使用的是AVFoundation框架
播放该按钮的代码如下:
- (IBAction)queChin:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"chin", CFSTR ("m4a"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}我已经搜索了这个话题,但我找不到任何有效的答案。也许我错过了什么?
作为另一种选择,我可以提醒用户说“嘿,打开你的铃声”,但我不知道如何检测电话是否静音。
有什么提示吗?
谢谢。
发布于 2012-12-07 07:06:49
好吧,我发现,你不能用AudioServicesPlaySystemSound播放系统声音,即使你用这个初始化音频会话:
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];如果用户将设备静音,系统声音将保持静音。
我最终使用了AVAudioPlayer,这解决了问题。
使用系统声音的旧代码:
- (IBAction)queChin:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"chin", CFSTR ("m4a"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
CFRelease(soundFileURLRef);//prevents memory leak
AudioServicesPlaySystemSound(soundID);
}使用AVAudioPlayer的新代码:
- (IBAction)queChin:(id)sender {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"chin" withExtension:@"wav"];
_chingosound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
_chingosound.delegate = self;
[_chingosound prepareToPlay];
[_chingosound play];
} 您必须初始化音频会话才能在振铃器关闭的情况下播放声音。
https://stackoverflow.com/questions/13735294
复制相似问题