tags:

views:

135

answers:

2

i want to make an application which creates sound ,music ,system sound etc when iphone is in silent mode ,is it possible to play any type ofsound whether music or system tones when it is silent mode?

+1  A: 

It's not advisable, but who am I to say you can't do it. You may have a good reason to be playing sound.

If you are using Audio Sessions, then include <AVFoundation/AVFoundation.h> at the start of your file and

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

should do the trick. Note if you play music or sounds, then iPod playback will be paused.

Once this has been done, probably somewhere in the initialisation of one of your classes that plays the sounds, you can instanciate sounds like this:

// probably an instance variable: AVAudioPlayer *player;
NSString *path = [[NSBundle mainBundle] pathForResource...];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url];

When that's done, you can play with it any time you want with:

[player play]; // Play the sound
[player pause]; // Pause the sound halfway through playing
player.currentTime += 10 // skip forward 10 seconds
player.duration // Get the duration

And other nice stuff. Look up the AVAudioPlayer Class reference.

cool_me5000
is there any code to play sound on silent mode
pankaj kainthla
+1  A: 

Yes you can play sound when the phone is set to vibrate. Simply use the AVAudioPlayer class.

By default, playing an Audio Session sound will ~not~ respect the setting of the mute switch on the iPhone. In other words, if you make a call to play a sound and the silent (hardware) switch on the iPhone is set to silent, you’ll still hear the sound.

This is what you want. So now you know that playing an Audio Session when your phone is in silent mode will still play the sound you just need to know how to create an audio session to play the sound, like so: taken from this website: http://iphonedevelopertips.com/audio/playing-short-sounds-audio-session-services.html

SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle]
   pathForResource:@"RapidFire" ofType:@"wav"];    

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound (soundID);

For this to work, you will need to import header file, and also add the AudioToolbox.framework to your project.

And that's it.

So from this answer you now know that you can play sound while the phone is on vibrate. You dont need extra or special code to allow you to do this functionality as it already does that by default.

Do let me know if this helps.

Pk

Pavan