iphone background audio on/off - iphone

I have an application that can play audio while the application is in the background with the entry in the plist and everything work fine.
But I would like to give the option to the user to setup the background play on and off
I use this method to activate the player
moviePlayer.useApplicationAudioSession = YES;
[moviePlayer play];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
[[AVAudioSession sharedInstance] setActive:YES error:NULL];
I would like to change this option on run time and without any interruptions to the audio play.
Any ideas if this can happen?
Thanks for your time
I will post the solution here to make it better
if (backgroundSwitch.on) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];
}else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:NULL];
}

Related

Playing a youtube video in Mpmovieplayer or Mpmusicplayer ios

I want to play a youtube video with Mpmovieplayer or Mpmusicplayer. I want to do this because i have a requirement to make the app keep playing the video in background mode and that is quite not possible in uiwebview. Kindly if you could answer keeping the background mode audio playing in mind. Thanks in advance :)
You can use MPMovieplayerviewcontroller & also it looks like default player of iPhone.
Here is my answer : how to play live streaming from url for a camera that broadcasting live
FOR BACKGROUND: For background play write below lines in ViewDidLoad method -
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
And also make a small change in your info.plist like below image:
I successfully found a solution for the UIWebView, After see this hack https://gist.github.com/romainbriche/2308668
It's different on IO5/6 and IOS7.
The problem you will encounter will be to not stop the sound when the app enter in background.
We work on it here https://github.com/0xced/XCDYouTubeVideoPlayerViewController/issues/10#issuecomment-34252488
I found some solutions but not perfects.
Write Given Below code in didFinishLaunchingWithOptions
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) { NSLog(#"Error"); } else {NSLog(#"Success");}
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { NSLog(#"Error"); } else {NSLog(#"Success");}

iPhone App : Starting to play music very slow

Am working on an iPad/iPhone App & am playing a background music with this code snippet which I call in the Delegate's didFinishLaunchingWithOptions method:
-(void) playMusic {
NSLog(#"play music!");
NSString *sound_file;
if ((sound_file = [[NSBundle mainBundle] pathForResource:#"musicfile" ofType:#"mp3"])){
NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
self.audioPlayer.delegate = self;
[self.audioPlayer setNumberOfLoops:-1]; // Infinite loop
[self.audioPlayer prepareToPlay];
[self.audioPlayer setVolume:0.2];
[self.audioPlayer play];
} else {
NSLog(#"WARN : music file not found!");
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
But I see that this method alone is taking 0.43 seconds on an average, which is a lot! Am I doing it the right way? Or is there a faster way to start the music play ? Please help.
My environment : iOS5, XCode 4.3, iPad 2.0 is what I tested this with now

iOS: Playing audio in silent mode [duplicate]

This question already has answers here:
Play sound on iPhone even in silent mode [duplicate]
(8 answers)
Closed 9 years ago.
What is the "correct" way to get my app to play audio even if the user's silent/mute switch is set to silent/vibrate?
Some background:
I actually got it working in debug stage, but when I submitted my app to Apple and then later downloaded it from the app store, the audio was disabled in silent mode! Here is how I got it working in debug stage:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory), &sessionCategory);
Audio was played using:
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"mp3"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
I think it does the same thing, but have you tried, setting the setting throught AVFoundation like so?
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

How to play an audio when home button clicked in iphone sdk

Now am developing an application in which the application will play the audio automatically when the home button is tapped (when entering the background mode), below is my code i've used. it works perfectly in simulator but not working in any of the devices (iPhone, iPad, iPod). please guide me to go further...
- (void)applicationDidEnterBackground:(UIApplication *)application {
AVAudioSession * session = [AVAudioSession sharedInstance];
[session setDelegate: self];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#.wav", [[NSBundle mainBundle] resourcePath], soundName]];
AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer setVolume:1.0];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSAssert(backgroundTask == UIBackgroundTaskInvalid, nil);
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}
I don't think you can do this, when the app is going into the background audio is muted thus the sound you play will nog be heard.
And this will be very annoying and should just not be done, also the simulator reacts differently than then a real iOS device.
An app can only play audio that has been started before entering background mode. Note that an app can play silent audio, but Apple has rejected apps that play a lot of silence.

playing audio files from table, iphone

I've created a list of music file names in an array which is used to populate an uitableview. I want to make the audio player take this array or table as the playlist and play them continuously one by one, with all the features like play, pause, next, previous, forward, backward and stop. What is the best way to implement this. All the music files in the resource folder itself so that i can initiate each one as
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
self.soundFileURL = newURL;
[newURL release];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
Could this be as simple as implementing the AVAudioPlayerDelegate?
appSoundPlayer.delegate=self;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (successfully)
{
// play next track
}
}
Obviously you'd need to keep track of which track is currently playing in order to work out the next track to play, but that shouldn't be a problem. You'd also have to implement your own controls and logic for track skip etc, but again this is easily done.