iphone,即使在静音或静音模式下如何播放声音?

问题描述:

作为主题......是否可能?

as topic... is it possible ?

谢谢

我已按如下方式附上代码,请检查哪一步错误。谢谢。

again, I have attached the code as follows, please check which step is wrong .thanks.

    //@step
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
UInt32 sessionCategory =   kAudioSessionCategory_MediaPlayback;
OSStatus error = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,                                            sizeof(sessionCategory),&sessionCategory);

if (error) 
    printf("ERROR AudioSessionSetProperty ! %d\n", error);

//@step 
NSString* filePath = @"AlarmClockBell.caf";
[Util restoreResourceFile:filePath];
filePath =[Util getFileFullPathFromSysDoc:filePath];
NSURL *soundFileURL = [NSURL fileURLWithPath:filePath];   
NSError* error ;
AVAudioPlayer * audioPalyer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: &error];
if (nil == audioPalyer) 
{
    AppTrace3(self, @"Faild to play", soundFileURL, error);
    return FALSE;
}
[audioPalyer prepareToPlay];
[audioPalyer setVolume: 5 ];
[audioPalyer setDelegate: self];
 audioPalyer.numberOfLoops = 10;

[audioPalyer play];

谢谢......

如果查看音频会话类别下的文档,您将找到许多模式,您可以设置这些模式以告诉系统您的应用计划如何使用音频。默认值为 AVAudioSessionCategorySoloAmbient ,它跟踪铃声/静音开关和屏幕锁定。

If you look in the docs under Audio Session Categories, you'll find a number of modes that you can set to tell the system how your app plans to use audio. The default is AVAudioSessionCategorySoloAmbient which tracks the ring/silent switch and the screen lock.

要让您的应用忽略响铃/静音开关设置,您可以尝试更改类别:

To have your app ignore the ring/silent switch settings, you could try changing the category:

#import <AudioToolbox/AudioToolbox.h>

AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
                         sizeof(sessionCategory),&sessionCategory);

如果你想让iPod音频继续在后台播放,你还​​需要检查 kAudioSessionProperty_OverrideCategoryMixWithOthers

If you want to allow iPod audio to continue playing in the background, you'll also want to check kAudioSessionProperty_OverrideCategoryMixWithOthers.