I know that you can for example play music in you app and it would be running in background but it looks like it's not working. I've this code when my application starts on iPhone:
pathToAudio = [[NSBundle mainBundle] pathForResource:#"silence" ofType:#"mp3"];
audioURL = [NSURL fileURLWithPath:pathToAudio];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:NULL];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
Result is that it plays music when I send app to background but other activities doesnt run. For example in my app I'm listening to socket and although music plays in background my app doesnt respond to incomming messages (it normally responds when app is in foreground). What do I do wrong? I've read that you can do other activities when playing music when app is in background but it looks like it doesn't work...
Edit: I have jailbroken iPhone so any unconventional methods are welcome. I know that Apple would reject my app if I was playing silent audio in background and I dont care about it. I just want to know how to make app running in background when playing music or in any other way... Maybe I need to have iPhone 4? I have iPhone 3G.
Nothing, it's only possible to run audio in background (aside with some special cases like geoloc notifs & VoIP), not other stuff.
You'll need to provide a specification for the info.plist file like the UIBackgroundModes.
Related
I know that in iOS, background apps can only be running
Finite-length tasks (10 min)
Location updates
VoIP
Audio
Is there a way for my application to avoid being terminated after being 10 min. in the background? I will not be submitting my app to the app store, so everything is allowed (private frameworks, using the gps even if I don't need it) I know apple does not recommend this, but it is just for monitoring purposes. I need it to be running without a limit.
I explored several possibilities including the VoIP , but it only gives me 30 seconds every 10 minutes, which is not enough. I also read this post:
iPhone - Backgrounding to poll for events
in which JackPearse specified a way to "revive" the 10 minute finite-length task using the VoIP 30 second task. But I don't want my task to start and end every 10 minutes, it must run continuosly.
I also tried his UPDATE2, but it's not working for me.
I even tried intercepting the UIEvent with GSEvent.type 2012, which seemed to be the one ending my background task, but no luck. Strangely, my background task is never ended when I have Xcode opened and debugging, but when I don't (test the simulator alone) it ends after 10 minutes.
I have already tried some way(nsrunloop,*performselectoronmainthread*) like that.It's works well in simulator (not in device because apple crashes automatically after sometimes) when the app goes to background.
status is a BOOL variable.
- (void)applicationDidEnterBackground:(UIApplication *)application {
while (!**status**) {
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:60.0]];
[self goBackground];
}
}
I found out how to keep my Application in the background for longer than 10 minutes by continuously playing a song in the background.
On the AppDidFinishLauching:
[[AVAudioSession sharedInstance] setActive:YES error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
NSURL *url = [NSURL fileURLWithPath:...]; //Song URL
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.numberOfLoops = -1;
[player play];
In the AppDidEnterBackground you can perform a selector in background, which can last forever. The App will not be interrupted, you can check UIApplication backgroundtimeRemaining and see it never decreases.
Of course, declare the app as a background audio App in the plist.
Im using the AVFoundation framework to play sound files. The problem im having is that its stopping music from playing when the audio file gets used, im not saying play both files continuously, but play the sound file, then pick up the ipod music right where it left off. Is there any way i can use AVFoundation is this kind of way? or is there a better framework for it?
Here is what my code looks like:
click = [NSURL fileURLWithPath:[NSString stringWithFormat:#"#/Click.WAV", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:click error:nil];
audioPlayer.numberOfLoops = 1;
[click release];
[audioPlayer play];
This code works completely fine, i had to type it out so ignore any problems that there might be with it.
Thanks,
Jacob
You can use the AVAudioSession class to change the audio "category" of your app: thus you can allow it to play on top of the iPod music. Use the -setCategory:error: method, and you will probably want to use AVAudioSessionCategoryAmbient. More info can be found in the Audio Session Programming Guide.
AudioServicesPlaySystemSound doesn't do anything but AudioServicesPlayAlertSound makes the iPhone vibrate.
AudioServicesCreateSystemSoundID returns success. I'm developing on iPhone3G running iOS4.
Any ideas?
Peter
=====
Here's how I create the sound:
NSString *sndPath = [[NSBundle mainBundle] pathForResource:#"first_touch" ofType:#"wav" inDirectory:#"/"];
CFURLRef sndURL = (CFURLRef)[[NSURL alloc] initFileURLWithPath:sndPath];
int e = AudioServicesCreateSystemSoundID(sndURL, &_firstTouch); // TODO: call 'AudioServicesDisposeSystemSoundID'
And that's who I play it:
AudioServicesPlayAlertSound(_firstTouch);
Showing some code helps.
At a guess, you're creating a sound, playing the sound, and then immediately deleting the sound. This doesn't work; deleting the sound causes it to stop playing.
I'm not deleting the sound. I added the source code.
After I rebooted both my machine and the iPhone, it started working on the simulator but not on the iPhone.
It worked once on the iPhone. I'm testing with a new project with just the create and play sound in it.
I have a need to play some sporadic sound effects in the background of an iPhone app. From everything I've read and experienced with iOS 4, I can keep my app running in the background as long as I am running GPS by specifying "location" as a background mode. That actually works. But at times I want to play a sound effect...in other words, it's not "continuous" sound which I see reference to.
But the app is running, so why can't I just use AVAudioPlayer to play some sound effects? Would another sound API work?
GAHHH!
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"BEEP" ofType:#"aiff"];
NSURL *fileURL = [[[NSURL alloc] initFileURLWithPath: soundFilePath] autorelease];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
player.volume = 1.0;
[player prepareToPlay];
// play
[player play];
I think it is important to clarify that your application is not running in the background. Rather, your application is 'put to sleep'. The info.plist background process value that you specify tells the OS that it should wake your application and allow it to respond to specific types of events. From the Apple Documentation
Each of the preceding values [audio, location, voip] lets the system know that your application should be woken up at appropriate times to respond to relevant events.
In your case, your application is frozen and is only able to respond to the type of event that you specify (location, or GPS).
You don't make it clear what context this code is in, so at this point it's difficult to tell you why the audio is not playing. Also, you need to make sure that your application is running in the background for the specified purpose. If you are using the location mechanism and not using GPS in your app, you may get rejected when you submit your application.
You may also want to refer to the Checklist for Supporting Multitasking to ensure the structure of your application implements their requirements.
[EDIT]
You have a good example of background playback while monitoring GPS position here :
http://www.informit.com/articles/article.aspx?p=1646438&seqNum=5
And it's using AudioToolbox rather than AVAudioPlayer
I've managed to do it using the AVAudioPlayer.
did you also add the audio value in the info.plist for key UIBackgroundModes ?
The answer is that it won't work with AVAudioPlayer. Use the AudioServicesPlaySystemSound API instead.
I'm using the following to play an m4a file:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: fileName];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
It works fine on the simulator but I hear nothing on the device. Sounds files I'm using all stay in the bundle. Here is what filePath looks like from the device:
file://localhost/var/mobile/Applications/418945F3-3711-4B4D-BC65-0D78993C77FB/African%20Adventure.app/Switch%201.m4a
Is there an issue with the file path or any thing different I need to do for the device?
Just as a sidenote - I was having the exact same problem and spent probably close to an hour on converting files to the correct format, etc.. Yet the problem was the "mute" switch on the iPad. So even though the volume was up, and I could hear other sounds on the iPad, because the mute switch was turned on, it wasn't playing system sounds.
To add to the confusion, this app uses text-to-speech and the volume coming from the dictation was perfectly fine, it was only the sounds coming from AudioServicesPlaySystemSound() that weren't being played.
I had trouble with this too. Finally I realised it was because AudioServices can only play audio with the following constratints.
Sound files that you play using this
function must be:
- No longer than 30 seconds in duration
- In linear PCM or IMA4 (IMA/ADPCM) format
- Packaged in a .caf, .aif, or .wav file
From Apple docs: http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html
You might want to use the AVAudioPlayer instead of AudioServices.
The following code will take an audio file (.m4a) and play the audio file 1 time. Don't forget to release "audioPlayer" when you're done with it.
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"m4a"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
{
NSLog([error description]);
}
else
{
[audioPlayer play];
}
Hope this example helps you with playing audio on the actual device. It might also be a good idea to increase the device audio when the file is playing.
Note: You will need to add the AVFoundation framework to your project if you have not already done so. As well as import the header file.
#import <AVFoundation/AVFoundation.h>
Update:
From Apple's Core Audio Overview Document
Audio Session Services
Audio Session Services lets you manage audio sessions in your application—coordinating the audio behavior in your application with background applications on an iPhone or iPod touch. Audio Session Services consists of a subset of the functions, data types, and constants declared in the AudioServices.h header file in AudioToolbox.framework.
The AVAudioPlayer Class
The AVAudioPlayer class provides a simple Objective-C interface for playing sounds. If your application does not require stereo positioning or precise synchronization, and if you are not playing audio captured from a network stream, Apple recommends that you use this class for playback. This class is declared in the AVAudioPlayer.h header file in AVFoundation.framework.
Start by error-checking your returns. Is filePath nil? Do either of the AudioServices functions return an error? The most likely cause is case-sensitivity. The iPhone filesystem is case sensitive while the Mac is not. But the first step in debugging is to look at the errors the system is providing.
The simulator uses regular QuickTime for playback, so it's easy to have media assets which work in the sim, but fail on the device due to missing / unsupported codecs. The test is if you can play the file at all on the device, eg through Safari or the iPod app.