Play audio in background in ios 7 - objective-c

I have made an audio player for my app using this guide
http://www.ymc.ch/en/building-a-simple-audioplayer-in-ios
The problem i am facing is that the audio stops when the phone gets locked and the audio becomes mute when the ringer is off.
Can someone guide me how to keep playing the audio even when the phone is locked.

Firstly you must set Required background mode to App plays audio
And write below codes in didFinishLaunching Method of AppDelegate.m
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
iOS background audio not playing

Related

UIImagePickerController opening Picture mode instead of Video Mode after using Opentok features

I am using Opentok v2.0 in my iOS App for the chat/call purposes with other online users. Application has a feature of recording videos too when internet is absent so they can be uploaded later on. Everything is working accordingly but when I record videos after using Opentok features, UIImagePickerController opens Picture mode instead of Video mode. Here is my code of invoking camera for the video recording.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.allowsEditing = NO;
self.picker.delegate = self;
self.picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
[self presentViewController:self.picker animated:YES completion:nil];
}
Is it Opentok bug or am I doing something wrong? Please share your thoughts
Regards
This is a bug as of OpenTok iOS SDK version 2.3.1. To work around the issue, you can use the code below after exiting your OpenTok Session and before opening the UIImagePickerController. I would suggest placing this in your sessionDidDisconnect method
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
don't forget to set your Session object to nil for cleanup.
Now, if you want to be able to enter back into a Session and have proper audio, you will need to call:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
You can place this line of code before connecting with the Session Object's connectWithToken method.

AVPlayer not going to the next song when it is in background mode

I am useing AVPlayer to play setof songs from urls. I can play songs in background mode too. But the problem is in background mode the next song is not playing. It excecute the code and but [player play]; method is not working. But If play 1 song it plays to the end though it goes to the background mode.
Please help me
Thanks
You should use AVQueuePlayer instead. This is working fine for me.
Well, you probably have this figured out by now, but for anyone else who might come across this, try putting this in your viewDidLoad:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Assuming you have this :
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
in your AppDelegate.m, under didFinishLaunch and your info.plist set up to play audio in the background.
Had the same problem, and this solved it for me.

AVAudioPlayer will not play when device is locked

I am using AVAudioPlayer in my application. When I select a song it plays, but my problem is, it is not playing when the device gets locked.
You need to read Executing Code in the Background in Apple's documentation. If you set the UIBackgroundModes key in you app's Info.plist to audio, audio will keep playing while backgrounded.
See the sections Declaring the Background Tasks You Support and Playing Background Audio in the aforementioned documentation.
All we need to make music play in the background with AVAudioPlayer by two step:
Step 1: Choose Project -> Capabilities -> Enable Background Modes -> Select Audio Mode
Step 2: Use this code:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"demo" withExtension:#"mp3"];
avSound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[avSound setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[avSound prepareToPlay];
[avSound setNumberOfLoops:-1];
[avSound setVolume:1.0];
Note that with this line the music can start even if the app was in the background:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
As per my knowledge it is not possible to play sound when device locked. Can't you just disallow device to lock and alive apps active always until user close it manually ?

iPod - Let the native music app to play sound in background

I am building an application for iPod.
When running it, the native iPod music app should continue play in background, but this is not happening.
I think it might have something to do with the sound that it is played by my app. For playing a short sound at a certain point, I am using AVAudioPlayer. The code looks like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"mp3"];
NSLog(#"%#", path);
player = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:path] error:nil];
[player prepareToPlay];
[player play];
Could this interfere with the native music player on iPod?
And another thing. In my app, I have integrated AdWhirl and it appears to use AudioToolbox.
Thanks a lot,
George
You must set up your AVAudioSession to use the Ambient category. This tells the system that your sound should blend with any already playing sound. The best way to do this in iOS 4 is in the app delegate, like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
// ...
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// reactivate audio session
[[AVAudioSession sharedInstance] setActive: YES error: nil];
}
Read up on Audio Sessions. If your app produces sound, you always want be conscious of and in control of your audio session.

how to play sound when alertview appears iphone sdk?

i want to play a sound file when alertview appears and plays continuously till user clicks on ok or cancel.how do i do this?
As Zoul says, you set-up and play your sound as you call [myAlert show] and cancel the sound in the alert view callback. Your code will look something like this:
AVAudioPlayer *myPlayer;
// ...
// create an alert...
NSError *error;
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mySoundFileURL error:&error];
// handle errors here.
[myPlayer setNumberOfLoops:-1]; // repeat forever
[myPlayer play];
[myAlert show];
// ...
// in alert callback.
[myPlayer stop];
[myPlayer release];
As you already call the show method to display the dialog, why don’t you simply start playing the sound there and stop in the alert view callback? For the sound itself you can use AVAudioPlayer.