I’ve read many articles/posts on this topic, but I’m still unable to play a sound while the screen is locked.
In viewDidLoad, I initialize the audio session and set the audio property to kAudioSessionCategory_MediaPlayback:
AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(true);
I then initialize my AVAudioPlayer variable (audioPlayer):
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: #"click" ofType: #"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: &error];
[fileURL release];
I added “App plays audio” to a “Required background modes” section of my info.plist.
I added AVAudioPlayerDelegate and AVAudioSessionDelegate to my view controller’s *.h file (although I don’t know if that was necessary).
I play the (very short) sound periodically (~1x per second):
[audioPlayer play]
The sound plays fine when the screen is unlocked, but stops playing when I lock it. When I then unlock the screen, the (queued) sounds are played.
Ideas? Thanks!
Thanks holex:
I got "App plays audio" from http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_background-audio/.
If you add a row with UIBackgroundModes to the plist, it's automatically populated and includes this and six other background modes. If you Google "App plays audio" and UIBackgroundModes you'll see plenty of hits.
[I would have replied via a Comment rather than an Answer, but alas, I don't have the reputation.]
It seems the problem is that I'm starting to play the sound after the screen has been locked. That's mostly answered in How to play sounds in locked mode on iPhone.
Related
The app must to play a sound when it is in background. In the capabilities background fetch and audio & airplay are checked.
The method called for play the sound is that
-(void)playSound
{
NSString* str = [[NSBundle mainBundle] pathForResource:#"alarm4" ofType:#"wav"];
NSURL* url = [NSURL fileURLWithPath:str];
NSError* erreur;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&erreur];
if(!erreur)
{
[self.audioPlayer setVolume:1];
[self.audioPlayer setNumberOfLoops:-1];
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
}
else
NSLog(#"erreur de chargemnt %#", [erreur localizedDescription]);
}
Can you give me some advice, please?
My advice is: Don't even dream of such a thing. In general, you can't do it.
You can continue playing a sound as you go into the background, but you cannot spontaneously produce a sound out of the blue in an already backgrounded app. It would be terrible if this could happen, because the user would not know where the sound is coming from, the sound could mess up the user's music listening experience, etc.
You should ask yourself why you think you need to do such a thing in the first place. You probably don't need to.
(Some Apple apps can do it. For example, the Clock app produces a repeating alarm sound out of the blue, in the background. But you, as you have probably noticed by now, are not Apple.)
I have an app that uses AVAudioPlayer to play sound effects, music, etc. It works fine for just about every user (100,000+ users), but I've had several reports of sound not playing at all even though sound effects are working on other apps on the same device. The bug has gone away for some users after an app restart/device reboot, but for others it continues to persist. AVSpeechSynthesizer isn't working for these users either.
I haven't been able to reproduce the bug to see if the AVAudioPlayer is throwing an error, so I'm not sure if it is - but I don't see why it would be working fine for 99.9% of users and not this tiny subset.
Has anyone else run into something like this?
Here's the code I'm using to set up the player:
[[AVAudioSession sharedInstance] setCategory:#"AVAudioSessionCategoryAmbient" error:nil];
NSString *file = [filename stringByDeletingPathExtension];
NSString *extension = [filename pathExtension];
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:file withExtension:extension];
self.soundPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
[self.soundPlayer1 prepareToPlay];
I'm new into Apple developing but I need to make an easy Radio Streaming App. I've made everything and the app works correctly on my iPhone (with IOS 6) but it stops playing when I'm pushing home button (when the app goes into background mode). Is there any posibility to make the Webview work in background?
You will need to go to your Project, then the Capabilities tab, then turn on the Background Modes and toggle on the one for background audio. You will also need to set your audio session to the playback category
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:nil error:nil];
You need to use the AVAudioPlayer class.
NSURL *url = [NSURL URLWithString:#"http://puttheurlhere.com"];
NSData *data = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audioPlayer play];
-(void)viewDidAppear:(BOOL)animated
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"Intro",
CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID);
AudioServicesPlaySystemSound (soundID);
}
I'm a beginner with this. I'm trying to play a wav file in the background of the view controller. I originally made a 30 second wav file, it wouldn't play, I reduced the length of the audio file to 10 seconds and it plays fine on the view controller. My question is, how to loop this audio file and/or play a longer audio file. Also, will the audio stop when I leave the view controller.
I am using xCode 4.3
I am running the app on a iPhone4 & 4s (iOS 5.1)
Thanks in advance...
Apple Documentation states
http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html
You can use System Sound Services to play short (30 seconds or shorter) sounds. The interface does not provide level, positioning, looping, or timing control, and does not support simultaneous playback: You can play only one sound at a time. You can use System Sound Services to provide audible alerts. On some iOS devices, alerts can include vibration.
So no loop is available when using system sounds.
You can check out this tutorial
http://mobileorchard.com/easy-audio-playback-with-avaudioplayer/
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
}
It states that if you set the numberOfLoops property to a negative number causes the audioPlayer to loop indefinitely
And dont forget to add the AVFoundation framework to your project
I have a problem with the AVPlayer playing in background mode, like a lot of people here and everywhere on the web. I've done what I think is supposed to work, but is still doesn't... I think my problem might be where I set and use my AudioSession and AVPlayer.
1) The "audio" key is in UIBackgroundModes of my Info.plist
2) AudioSession set like this in AppDelegate (initialised in didFinishLaunchingWithOptions):
AVAudioSession *audio = [[AVAudioSession alloc]init];
[audio setCategory:AVAudioSessionCategoryPlayback error:nil];
[audio setActive:YES error:nil];
3) I use an AVPlayer (not AVAudioPlayer) also implemented in AppDelegate. (initialised in didFinishLaunchingWithOptions, after the AudioSession), right after the AudioSession
// Load the array with the sample file
NSString *urlAddress = #"http://mystreamadress.mp3";
//Create a URL object.
self.urlStream = [NSURL URLWithString:urlAddress];
self.player = [AVPlayer playerWithURL:urlStream];
//Starts playback
[player play];
And still, the audio is suspended everytime the app goes in background (when I press the "Home" button).
By the way, the problem was just with the simulator. Works fine on the iOS devices