iOS: Playing audio in silent mode [duplicate] - iphone

This question already has answers here:
Play sound on iPhone even in silent mode [duplicate]
(8 answers)
Closed 9 years ago.
What is the "correct" way to get my app to play audio even if the user's silent/mute switch is set to silent/vibrate?
Some background:
I actually got it working in debug stage, but when I submitted my app to Apple and then later downloaded it from the app store, the audio was disabled in silent mode! Here is how I got it working in debug stage:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory), &sessionCategory);
Audio was played using:
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"mp3"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];

I think it does the same thing, but have you tried, setting the setting throught AVFoundation like so?
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

Related

iPhone App : Starting to play music very slow

Am working on an iPad/iPhone App & am playing a background music with this code snippet which I call in the Delegate's didFinishLaunchingWithOptions method:
-(void) playMusic {
NSLog(#"play music!");
NSString *sound_file;
if ((sound_file = [[NSBundle mainBundle] pathForResource:#"musicfile" ofType:#"mp3"])){
NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
self.audioPlayer.delegate = self;
[self.audioPlayer setNumberOfLoops:-1]; // Infinite loop
[self.audioPlayer prepareToPlay];
[self.audioPlayer setVolume:0.2];
[self.audioPlayer play];
} else {
NSLog(#"WARN : music file not found!");
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
But I see that this method alone is taking 0.43 seconds on an average, which is a lot! Am I doing it the right way? Or is there a faster way to start the music play ? Please help.
My environment : iOS5, XCode 4.3, iPad 2.0 is what I tested this with now

How to play an audio when home button clicked in iphone sdk

Now am developing an application in which the application will play the audio automatically when the home button is tapped (when entering the background mode), below is my code i've used. it works perfectly in simulator but not working in any of the devices (iPhone, iPad, iPod). please guide me to go further...
- (void)applicationDidEnterBackground:(UIApplication *)application {
AVAudioSession * session = [AVAudioSession sharedInstance];
[session setDelegate: self];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#.wav", [[NSBundle mainBundle] resourcePath], soundName]];
AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer setVolume:1.0];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSAssert(backgroundTask == UIBackgroundTaskInvalid, nil);
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}
I don't think you can do this, when the app is going into the background audio is muted thus the sound you play will nog be heard.
And this will be very annoying and should just not be done, also the simulator reacts differently than then a real iOS device.
An app can only play audio that has been started before entering background mode. Note that an app can play silent audio, but Apple has rejected apps that play a lot of silence.

iphone background audio on/off

I have an application that can play audio while the application is in the background with the entry in the plist and everything work fine.
But I would like to give the option to the user to setup the background play on and off
I use this method to activate the player
moviePlayer.useApplicationAudioSession = YES;
[moviePlayer play];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
[[AVAudioSession sharedInstance] setActive:YES error:NULL];
I would like to change this option on run time and without any interruptions to the audio play.
Any ideas if this can happen?
Thanks for your time
I will post the solution here to make it better
if (backgroundSwitch.on) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
}else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:NULL];
}

iPhone: How to play a sound on top of the iPod sound?

I'm trying to play a sound on top of the iPod music but when I enter my app the iPod sound is silenced automatically. I tried:
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory
);
But probably because I use RemoteIO/Audio Units to play my sound this has no effect (not sure if that's the cause, but it sure thing does nothing ... the sound still stops)
I am missing something?
For some reason, the code in the question doesn't work, however this works:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
AudioSessionSetProperty returns an error, are you checking it?
You may have forgotten to call AudioSessionInitialize, something you don't need to do with the AVFoundation wrapper.
#property (nonatomic, strong) AVAudioPlayer * avAudioPlayer;
NSString *zeroAudioPath = [[NSBundle mainBundle] pathForResource:#"customSound" ofType:#"wav"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:zeroAudioPath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file
error:nil];
self.avAudioPlayer = audioPlayer;
self.avAudioPlayer.delegate = self;
[_zeroAudioPlayer prepareToPlay];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
[_zeroAudioPlayer play];
Hope it will help you.

playing audio files from table, iphone

I've created a list of music file names in an array which is used to populate an uitableview. I want to make the audio player take this array or table as the playlist and play them continuously one by one, with all the features like play, pause, next, previous, forward, backward and stop. What is the best way to implement this. All the music files in the resource folder itself so that i can initiate each one as
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = newURL;
[newURL release];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
Could this be as simple as implementing the AVAudioPlayerDelegate?
appSoundPlayer.delegate=self;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (successfully)
{
// play next track
}
}
Obviously you'd need to keep track of which track is currently playing in order to work out the next track to play, but that shouldn't be a problem. You'd also have to implement your own controls and logic for track skip etc, but again this is easily done.