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");
Related
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
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.
I am trying to be able to record audio through the iPhone's microphone while simultaneously playing music from the iPod.
The recording and playback work separately, but if I try to playback from the iPod and record at the same time the recording stops.
By the way, I am not actually recording the audio to save the sound file, but merely analyzing it. It's for my app that flashes to the beat of the music you play.
Does Apple just not allow record and playback at the same time?
You should be able to do this by setting the Audio Session to AVAudioSessionCategoryPlayAndRecord. Information on this is in the Apple docs, although admittedly the Core Audio documentation is (and has always been) lacking in clarity.
AVSession Class Reference
Also, it's worth noting that AVAudioRecorder and AVAudioPlayer automatically set the session for you. You can wrestle control and set the session manually using the key I highlighted above.
the problem may be you are recording audio to the same path where the playing file is saved.
try to play an audio from a url with AVAudioPlayer and you can record with AVAudioRecorder at the same time.
Cheers :) :)
I'm developing an interactive storybook type application for the iPhone and I've recently encountered a frustrating bug concerning audio mixing on the device.
Firstly, I setup an audio session. I set the category to AVAudioSessionCategoryAmbient and then init and play my AVAudioPlayer instance. Now, in the background whilst the audio is playing I'm pre-loading a video to play using an MPMoviePlayerController followed by a call to prepareToPlay. The reason I pre-load the video this way is because I need it to play instantly later on cue with fairly strict timing.
In this configuration, the audio/movie works fine and they mix and do not interrupt each other. However, this particular audio session category does not permit audio to continue playing while the device is locked, a feature I really need. As a result I'm forced to consider a different category: AVAudioSessionCategoryPlayback.
By default this category does not permit mixing with other audio, according to the Apple docs. To enable mixing with other audio I am overriding the relevant category:
OSStatus propertySetError = 0;
UInt32 setProperty = 1;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(setProperty), &setProperty);
assert(propertySetError == 0);
Unfortunately, this solves my playing whilst locked issue but introduces another issue: the AVAudioPlayer audio is interrupted briefly as the video loads with a minor stutter. The stutter is small, perhaps less than a second but is enough to disrupt the user experience. I've read this related post which enabled me to pre-load the video with the AVAudioSessionCategoryAmbient, but unfortunately the same approach doesn't seem to work with the new category.
The audio session category is applied successfully, according to the return code. Does anyone know why enabling audio mixing with this category is not the same as the mixing facility provided by ambient category?
The best way I've found working a similar problem is to use the newer AVPlayer (+1 #adam) and set your app to enable background audio and receive remote control notifications. I was tipped off to this by #MarquelV following How can you play music from the iPod app while still receiving remote control events in your app?
If you can get backgrounding working properly, that should enable you to continue playing while the device is locked. Oh, and don't forget to add keys to info.plist, its easy to do and then have no idea why it isn't working.
HI all
i am playing a video(.mp4 format) without sound (i mean it doesn't have sound it is a mute video ) and in the background i am playing an audio file (.mp3 format) when i play my code through simulator it works fine as i want like when i tap on the video it is just mute but behind i am playing the audio so for user it seems that video has this sound but when i installed my code in device and play video than it doesn't work like so it play video but without sound than how can i play an audio and a video together in the above format ?
actually we are not just playing a single video or audio file it just comes from an array by choosing randomly and same for the audio file so we cann't do this i think so any other idea for it ??
Should we use another format for audio aur video for doing this thing ??
thanks for the help
Balraj verma
The problem is that the default Audio Session does not allow audio mixing.
In the Reference Library (Working with Movie Players) they say you should use a mixable category configuration for your audio session, for example the Ambient category. In the Application Delegate:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryAmbient error: &setCategoryError];
Apple's documentation states that some audio formats are not suited to be played back simultaneously because of hardware restrictions. See Playing Multiple Sounds Simultaneously for more details.
A solution for you would be to use PCM encoded audio which should allow simultaneous playback.
I would like to add that I managed to play two mp3 audio files at the same time on 3G and 3GS iPhones. Which shouldn't be possible according to documentation but worked for me.
You can rather use the an instance of AvAudioPlayer in a new thread. Use the following link to see how does this work
http://www.mobileorchard.com/easy-audio-playback-with-avaudioplayer/
create an instance of MPMoviePlayer to start playback of videos.
Hope this will work.
You should combine the audio and video using some video editing software like iMovie or Windows Movie Maker. Using the software to "composite" audio and video together is not a good idea and adds additional overhead, synchronization issues, etc.
As far as I know, you can't play AVAudioPlayer and MPMoviePlayer at the same time.