开发者

iphone, how to play sound even in silent or mute mode?

开发者 https://www.devze.com 2022-12-29 00:22 出处:网络
as topic... is it possible ? Thanks again, I have attached the code as follows, please check which step is wrong .thanks.

as topic... is it possible ?

Thanks

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

thanks...


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);

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


As of iOS 6, there is an alternative method that's more compact than Ramin's answer.

#import <AVFoundation/AVFoundation.h>

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                       error:nil];

To allow background audio from other apps to continue playing, add the AVAudioSessionCategoryOptionMixWithOthers option:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                 withOptions:AVAudioSessionCategoryOptionMixWithOthers
                                       error:nil];

There's further details in Apple's AVAudioSession Class Reference.


It must be, there's been a couple games I've had which (even when its in mute mode) will play sound. Unfortunately this was discovered whilst attempting to play games covertly in class.

As to how to actually do it, I really don't have any idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消