I have to play a mp3 file using avplayer ,but when i go to background it stops ,How do i keep it playing in background state as well.Any advise will be highly appreciated.
Thanks.
in to your plist fine just add one method like:-
Or in code:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
To play AVPlayer in background,you have to follow below steps:
1) to add "Required background modes" property in info.plist,with value "App plays audio or streams audio/video using AirPlay"
2)to import below in AppDelegate.h file:
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
3)in your AppDelegate.m ,
application didFinishLaunchingWithOptions this exactly like below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
return YES;
}
You should read Technical Q&A 1668, Playing media while in the background using AV Foundation on iOS. In it, Apple gives examples of how to play media in the background with AVPlayer, as well as discusses potential issues you may run into.
From the docs I posted in the comment:
An app that plays or records audio continuously (even while the app is running in the background) can register to perform those tasks in the background. You enable audio support from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the audio value in your app’s Info.plist file.) Apps that play audio content in the background must play audible content and not silence.
...
When the UIBackgroundModes key contains the audio value, the system’s
media frameworks automatically prevent the corresponding app from
being suspended when it moves to the background. As long as it is
playing audio or video content or recording audio content, the app
continues to run in the background. However, if recording or playback
stops, the system suspends the app.
App States and Multitasking Guide
Related
When playing an audio file in watchOS (using SpriteKit):
run(SKAction.playSoundFileNamed("ready.wav", waitForCompletion:false))
It will works until you background the app (says face the watch outwards) and back, then the audio file cannot be played anymore.
I have tried many workarounds, including setting UIBackgroundModes to audio, but the sound will always stop working after the app is background and back. Any solution?
PS: I think I might have found the answer: use AVAudioPlayer instead of SKAction.
You need to start HKWorkoutSession and in info.plist enable workout background processing. Don't need the audio to play in background, just continue to play once it comes back from background.
Use AVAudioPlayer instead of SKAction. This works.
Only SKAction.playSoundFileNamed has this issue.
I think that it's clear from the title...
The Problem:
I already have some objective-c code that uses AVFoundation and its AVAudioPlayer to play some sounds in my iPhone apps, it takes a path for a folder in the documents and plays all the files in it one after the other using an AVAudioPlayer object (I implemented AVAudionPlayerDelegate so I can give the player the next file when the current one is done)...
The Question:
Now how can I keep these sounds playing when my app is in the background cause I upgraded my devices to iOS 4.1 and also my iOS SDK and Xcode...
What I did:
I added the "UIBackgroundModes" to my info.plist file and put "audio" in it and I set the category for the sharedInstance in AVAudionSeession to AVAudioSessionCategoryPlayback and setActive to YES and no errors happened there but still when I press the home button, the music stops!
What else should I do?
Thanks in advance
I had this problem,and solved this way: I added UIBackgroundModes and put audio there. Then in my AudioPlayer class (class where i play my audio) in "init" method i've put this code:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
NOTE: In simulator this will not work! You need real device.
A good example of what I'm trying to accomplish is implemented in the latest version of the Spotify iPhone application for (Pandora seems to have the same feature) .
When Spotify is in the background, double tapping opens the "multi-task dock", where the ipod controls (play/pause, forward etc) allow to control the music playback of Spotify (not the ipod application). Also, when the iphone/ipod touch is locked, double tapping displays similar playback controls.
If you don't know what I mean, here's an article that has screenshots :
http://www.wired.com/gadgetlab/2010/07/spotify-updated-for-ios4-ready-to-replace-ipod/
In my current application, music is streamed from a server (using Matt Gallagher's AudioStreamer). I've managed to keep the music playing in the background. Now, I'd like to link my playback to the "multi-task dock"/lock screen.
Should I be using [MPMusicPlayerController iPodMusicPlayer] ? How should I proceed ?
Bonus question : if you can tell me how to change the ipod icon to my application icon in the "multi-task dock" (Spotify pulled that trick as well...), that whould be AWESOME.
Any help appreciated, thanks.
Problem is solved.
In short, to enable remote control event, 1) use :
- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent
and 2) put this is your view controller :
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
I have to give credit to Grant. He has forked Matt Gallagher's AudioStreamer enabling all the ios4 improvements (background audio, and remote controls working). You can find his sources along with a working sample on github : http://github.com/DigitalDJ/AudioStreamer
Regarding the icon : once you use beginReceivingRemoteControlEvents, the icon automatically switches to your app icon. Brilliant !
Here's the documentation:
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/RemoteControl/RemoteControl.html
Notice however, that it'll work only when you have active audio session in your application.
I'm using it with AVAudioSession with AVAudioSessionCategoryPlayback category and AVAudioPlayer and "remote controls" work only when I have AVAudioSession active and AVAudioPlayer object created.
The controls will change for your application if you are using the new background audio api's. Information can be found here. Specifically the sections about background audio.
I am trying to play music from the user's music library and at the same time record from the built-in microphone. However, as soon as I start recording from the microphone it automatically pauses the music, and I can't find any way to play music and record at the same time. I am using the MediaPlayer framework to play music and the AudioToolbox framework to record from the mic.
Does anyone know if it is possible to play music and record from the mic at the same time, and if so how to do it?
You set your audio session to kAudioSessionCategory_PlayAndRecord
and create an in/out remote io audio unit.
The "loopy" author talks about how to do this here and provides code.
The trailblazing tones of the article are slightly dated now.
You'll need to set your audiosession properties to allow both recording and playback. This might mean that you need to use AudioToolbox for the playback as well as the recording.
try:
Add #import "CDAudioManager.h"to AppDelegate.m and Add [CDAudioManager
initAsynchronously:kAMM_PlayAndRecord]; to - (void)
applicationDidFinishLaunching:(UIApplication*)application
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");