3

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...

iXcoder
  • 1,564
  • 4
  • 20
  • 41

3 Answers3

6

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.

Ramin
  • 13,343
  • 3
  • 33
  • 35
  • Hi, Ramin, thanks you very much for the answer, that is very helpful, but I try to do the way, it still no works fine ... the volume of the iPhone device is mute (volume adjust to zero) , when the code running , it still no sound unless I adjust the volume by manual ... my code as follows: – iXcoder May 01 '10 at 13:32
  • hi, Ramin, I have add code in the question above, please help me check it if you have time... thanks! – iXcoder May 01 '10 at 14:11
  • Setting the audio category only ignores the 'mute' toggle switch or if the screen has been put to sleep, not the master volume level (the +/- volume rocker). There are a couple of ways to adjust the master volume via software, but most use undocumented APIs and may well get your app rejected. Here's one method: http://blog.stormyprods.com/2009/06/adjusting-iphone-master-volume.html – Ramin May 01 '10 at 18:23
  • Hi guys, first thanks Ramin for your answer. I think I have the same behaviour in a iPod touch 4 generation (because it hasn't silent switch). In my case the sound sounds when the app is in background (with volume to a minimum) but if the app is in foreground (with volume to a minimum) the sound doesn't sound. Do you know what can I change to achieve that the app sounds in both situations. – xarly Jul 25 '13 at 08:25
1

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.

Community
  • 1
  • 1
Rich
  • 7,348
  • 4
  • 34
  • 54
0

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.

Paul
  • 2,729
  • 20
  • 26
  • Hi, paul, thanks for your answer, does the game is from AppStore ? I have find some solution but it seem is only works on unlock iPhone ... – iXcoder May 01 '10 at 08:51