Set device volume - UILocalNotification - iphone in silent mode - iphone

How can i set volume of device(in silent mode) when UILocalNotification is generated when application is in background? I am working on alarm app, so sound has to be played in silent mode too and i am handling app alarm using local notification.
Badly stuck in this issue, not able to play alarm in silent mode.
Please help..

It is simply not possible. The UILocalNotification popup and sound are generated by another system process, and that process observes the device silent mode, so it won't play the notification sound if the device is on silent.
if you want your alarm clock app to play the alarm sound even when device is in silent mode, you will have to play the alarm sound right from your app. To do that, you will need to keep your app running in the background, then you will have to play the alarm sound file while in the background. The later can be done by specifying "audio" that the "Required background modes" property in your info.plist (you will have to add that property to your plist file)
Now, using AVAudioPlayer, there is a way to play sound even when device is silent by setting the Audio session category like this:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
Hope this helps.

You have to realize that local notifications are triggered even your app is killed. That leads me to conclusion that it is probably not possible to do that.
But you can try that like this:
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
right before you make your audio session active.

Related

Alarm clock with answer-to-disable on iOS

Apple resources contain a lot of informations but there's one thing which I can't clearly understand reading about audio and notification.
Is it possible to make an app, running in background which produce sound (even if phone is locked and/or silenced) and when it's happend user must solve eg. equation to turn it off?
p.s. For now I mostly use Cordova framework but Obj-C tip will also be nice.
Yes it is posssible.
You can use UILocalNotification for this.
Also apple allows apps that are playing music in background.
Please check these links for the background task feature:
ManagingYourApplicationsFlow
ios multitasking background tasks
How to handle background audio playing while ios device is locked or on another
You can change Local Notifications for NSTimers (keeping them alive in inactive mode with https://github.com/mruegenberg/MMPDeepSleepPreventer) and calculate the time interval for each alarm. That way you can then play an audio even with the screen locked and the sound off pasting this in your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:
// Let the sound run with the screen blocked
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
But you will have some problems:
The app must be playing an audio file each 10 seconds so it doesn´t deep sleep and kills all NSTimers.
Apple could reject your app for doing so.
You can´t close the app with the home button, otherwise, it won´t work.
You must open the app every time you need to use the alarm (you can´t schedule and forget).
When the alarm fires, you only have the lock screen of the iPhone and need to unlock it first and then stop the alarm from inside the app.
In Apple they don´t want competitors for their alarm clock app, that's for sure! Almost all the alarm clock apps you see in the App Store use this poor approach.

AudioSession - want to stop everything being played in background?

I got a need to stop (or mute at least) music/sound that is played in iPhone.
Important: I want my app will do that it even if is in background-state!
I'm using:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategorySoloAmbient error:nil]];
[[AVAudioSession sharedInstance] setActive: YES];
The issue is that everything is being stopped, even streaming music from some other app, but only if app is in FOREGROUND. As wrote before, I want it to be working also in BACKGROUND.
I did simple research and realized it's somehow possible, these apps prove:
App Store - Streaming Music Timer or
App Store - Music Sleep Timer
I guess my solution with SoloAmbient can be not so perfect and it may be a wrong way.
Does anybody know how could I stop/sleep/pause/mute global music even if app will be in background state?
These apps I pointed out are doing basically this thing...
You need to enable the audio background mode.
Add the Required Background Modes key to your app's Info.plist and add the App Plays Audio key to it.
See this tutorial for more.
EDIT Also, you probably want the AVAudioSessionCategoryPlayback category, not AVAudioSessionCategorySoloAmbient

MPMoviePlayerController background audio issue in iOS5

I have an App that does the pretty standard operation:
It plays audio (streamed or in filesystem) when the app is in 1) Foreground mode, 2) Screen locked state 3)Background mode.
This was working fine in all iOS prior to iOS5.
I have been using MPMoviePlayerController (Because it can play streamed and local file system audio)
I have the following setup:
info.plist has Background Mode set to "Audio"
I have Audiosession setup as shown at http://developer.apple.com/library/ios/#qa/qa1668/_index.html
NSError *activationError = nil;
AVAudioSession *mySession = [AVAudioSession sharedInstance];
[mySession setCategory: AVAudioSessionCategoryPlayback error: &activationError];
if (activationError) { /* handle the error condition */ }
[mySession setActive: YES error: &activationError];
if (activationError) { /* handle the error condition */ }
I have background timer enabled that gets stopped at the end of audio playback
UIBackgroundTaskIdentifier newId = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:NULL];
I have the Moveplayer's useApplicationAudioSession = NO
I have subscribed to the following events to detect and handle various playback state and to start a new audio file at the end of current file.
MPMoviePlayerLoadStateDidChangeNotification
MPMoviePlayerPlaybackDidFinishNotification
MPMoviePlayerPlaybackStateDidChangeNotification
MPMoviePlayerNowPlayingMovieDidChangeNotification
Problem:
With this the audio starts to play and when the application is put to background state or if the phone is locked, the audio continues to play. But, after when I start another audio file,
I start getting PlaybackDidFinishNotification immediately with the state set to Playback ended (But the file was never played)
The same code plays audio files in foreground mode (After the current audio file ends, the next file is started without any problem)
Is there anything new in iOS5 I should be doing to get this to work? I read through the MPMoviePlayerController class reference and I couldn't see anything specific for iOS5.
Thanks in advance.
Finally figured out the issue. This is solved in this post in apple dev forums (needs login to see). That post was applicable to AVPlayer but also fixes the problem with MPMoviePlayerController as well.
Basically, this is an excerpt from that post:
your app must support remote control events! These are the audio
controller interface prex/nex/play/pause on the left of the multitask
switcher taskbar (not sure about the proper name of the thing). You
to this ensuring your view becomes First Controller and then calling
> [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
in viewDidLoad. Once you do this, your Player will no longer return
NO!!
My situation was different and I'm only answering here (and in the other SO question) to help future searchers on this error message. This does not answer the original question.
My app plays a sound OR a song but when I first coded it could play both. And in testing I always tested with a song. I played the song in the usual way:
self.musicQuery = [MPMediaQuery songsQuery];
[_musicQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:selectedSongID forProperty:MPMediaItemPropertyPersistentID comparisonType:MPMediaPredicateComparisonEqualTo]];
[_musicQuery setGroupingType:MPMediaGroupingTitle];
[_myPlayer setQueueWithQuery:_musicQuery];
[_myPlayer play];
Weeks passed and I started testing with the sound, played with AVAudioPlayer. My app started freezing for 5 seconds and I'd get the MediaPlayer: Message playbackState timed out message in the Console.
It turns out that passing a query that was empty was causing the freeze and the message. Changing my app's logic to only play a song when there was a song to play fixed it.

App vibrating but ring tone is not playing when makes app in sleep mode manually. iphone

Im implementing a fake call app.Im using a NSTimer for checking the time and it is working fine.The problem is when we press the sleep button,only vibration is working but ring tone is not playing.I tried this code but still ring tone is not playing.
AudioSessionSetActive(true);
// Set up audio session, to prevent iPhone from deep sleeping, while playing sounds
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (
kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory
);
How can i solve this issue.please help.Thanks in advance
I am sorry but I think You can not show calling screen while device locked (deep sleep). The only way is to fire local notification to wake up your application manually by user. Something like : You have fake call now. Do you want to receive ? So when user press Yes and it will open your application and you will need to handle to show calling screen in appDelegate background methods.
I played with other Fake Call apps and they are doing same. We are not allowed to do any UI Changes in background. So this is the only way to achieve your goal to display calling screen.
Hope this help.
Edit:
I am not sure why vibrate is working. Did you set below property in your info.plist file ? Just to give it try. Not sure that will solve your problem. Just give it a try once. See attache picture. Set Required Background modes to App Plays Audio.

iPhone: How to detect if iTunes is playing?

I noticed that some apps programmatically mute itunes (if its running) at launching. How is this achieved? I have a game with background music and would like to either stop itunes or get at least a message that itunes is playing so that I can stop the game's background music.
thx,
marc.
You don't need to. With Audio Session you can decide how the audio should behave.
From the Audio Session Programming Guide:
With the audio session interface, you
specify aspects of your application’s
audio behavior and configure it to
live harmoniously within the iPhone
audio environment. You start by asking
yourself questions such as these:
Do you want your audio to be silenced by the Ring/Silent switch?
The answer is probably “yes” if audio
is not essential to using your
application successfully. (Users will
appreciate being able to run your game
in a meeting with no one the wiser.)
Do you want iPod audio to continue playing when your audio
starts? This could be appropriate for
a virtual piano, letting users play
along to songs in their libraries.
You’d want iPod audio to stop,
however, for a streaming radio
application.
You probably want this:
UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
AudioSessionSetProperty (
kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory
);
For more behaviour types, check the Audio Session Categories, or read the entire Audio Session Programming Guide.
I had the opposite problem. My app plays a short video with no sound after launch. This caused the iTunes music playing in the background to mute.
In order to keep the music playing, I add this in the applicationDidFinishLaunching:
NSError* error;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error];
if (error) NSLog(#"Unable to configure Audio");