I think everything is in the title.
I have a UIWebiew that plays music, and when the user use the power or home button, putting the app in background, I want the music to keep playing.
Is it possible ?
Thank you for your help !
You can't actually do this with a UIWebView, but you can still do it. This method still streams the audio off of an online resource via a URL. Here is some code to help you. Now, lets say, you want to stream the audio when the app exits for a video, then its very similar, but uses the MediaPlayer framework instead of AVAudioPlayer framework.
NSURL *url = [NSURL URLWithString:#"http://website.com/audio.mp3"];
NSData *data = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audioPlayer play];
A little tricky but it is possible. From the official Apple doc below:
https://developer.apple.com/library/ios/qa/qa1668/_index.html
You need to first declare in your plist the UIBackgroundModes for audio, and then write the following code snippet in your AppDelegate
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) { /* handle the error condition */ }
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /* handle the error condition */ }
Now when you have audio playing from a UIWebView, you can go to the home screen, change to another app, or lock the screen and the audio will continue playing.
I don't believe this is possible, and even after checking UIWebView's documentation I only found the following three attributes related to media playback.
allowsInlineMediaPlayback property
mediaPlaybackRequiresUserAction property
mediaPlaybackAllowsAirPlay property
Unfortunately, none of them do this.
Yes it is possible. You need to do:
Activate playback session:
#import AVFoundation;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
Add audio background mode in your Info.plist:
Required background modes: App plays audio or streams audio/video using AirPlay
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
Related
In my app at particular time i am alerting user with beep sound which i am playing using AVAudioPlayer.
Now i want this sound to be played even when app is in background. So i can achieve this by setting AVAudioSession category as AVAudioSessionCategoryPlayback and i also want that this sound should not stop the ipod music and i can do it with it setting AVAudioPlayer category AVAudioSessionCategoryAmbient.
But i want them together means sound should also play in background and it should also not stop the ipod music.
So how can i achieve this?
If it is not possible to play sound with AVAudioSessionCategoryAmbient category in background then any other workaround this?
Apologies if i am doing any mistake.
Thanks.
you need to add one row in your project.plist file Required background modes like bellow image and set it.
and put bellow code in to your project appdelegate didFinishLaunchingWithOptions
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
NSError *error;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if (!success) {
//Handle error
NSLog(#"%#", [error localizedDescription]);
} else {
// Yay! It worked!
}
its working for me hope its useful for you my frnd al the best.
UPDATE
You can't run AVAudioPlayer and the iPod player or MPMusicPlayer or MPMoviePlayer at the same time, without doing a bit more work. If you want easy, then use Audio Toolbox's System Sounds.
please refer this bellow link:-
iPhone AVAudioPlayer stopping background music
Please check bellow Link there are demo of Playing audio file in background. please check it
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_background-audio/
I am showing media hierarchy in the table view. When I tap on a song in the table view, it plays the song using MPMoviePlayerViewController. But when I click on the done button, the sound stops playing . I have created the following code:
NSURL *songUrl=[operationControl getSong:stringId];
mediaPlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:songUrl];
[self presentModalViewController:mediaPlayerController animated:YES];
[[mediaPlayerController moviePlayer] play];
I to want to browse the media hierarchy as well as play the song in the background. How can I achieve this?
You should start AVAudioSession and declare in main plist that your application plays music in background.
in didFinishLaunchingWithOptions:
// Setup audio session
AVAudioSession *sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryPlayback error:nil];
in applicationDidBecomeActive:
[sharedSession setActive:YES error:nil]; // FIXME: Error handling
In main plist add: Required background modes - App plays audio
It sounds like you didn't set up your Audio Session correctly. From
http://developer.apple.com/iphone/library/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html
For example, when using the default audio session, audio in your application stops when the Auto-Lock period times out and the screen locks. If you want to ensure that playback continues with the screen locked, include the following lines in your application’s initialization code:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
The AVAudioSessionCategoryPlayback category ensures that playback continues when the screen locks. Activating the audio session puts the specified category into effect.
I'm new to iOS and developing an app.
I've been searching all over to get a simple example on how to play an audio file in the background when an app is launched. Since the app that I'm developing is related to music lessons, I need this audio to be playing when the home screen is active. This is what I've done so far...
Created a home screen (UIViewController) with some background images and a button to navigate further into the app.
Set up "Required background mode" in the .plist
Add the audio file into the project
In my controller I have the following code block.
- (void)viewDidLoad
{
[super viewDidLoad];
//Set some labels here
//Set a background image
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"shrutiFsharp" ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer play];
}
All the labels, background image, buttons show up fine.. and I'm able to navigate to another view. But when I'm on the home screen, I just cannot get the audio file to play. I'm running the iPhone 5.0 simulator.
I don't see any error messages in the console.
The code you see here is from an example that was already posted.. but I'm not quite sure why this does not work. I'm probably missing something dearly. Any help is appreciated.
-KD
You should use the error parameters the way they were intended. It's entirely likely the audio player is trying to tell you exactly why it can't continue, but you're ignoring it!
NSError *error = nil;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&error];
if (error) {
NSLog(#"audio player error: %#", [error localizedDescription]);
}
I have set up my AVAudioSession to play music in the background
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) {
}
NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) {
}
Also added 'Required background modes' in my plist file.
When I play a remote file and the iPhone enters the background the audio keeps playing as suspected. But when I play a song from the iPod inside my app (MPMediaItem URL) it won't continue to play in the background.
What am I missing?
You're not missing anything. When the user starts playing music from iPod, it will stop your app from playing any further audio.
If you mean something else, please leave a note and I'll update this answer.
Edit: You should start iPod music by setting the queue on the relevant instance of MPMusicPlayerController, and then sending it the -play message. If you use the URL provided by the MPMediaItem object with an AVAudioPlayer instance or similar, it will be routed differently and may not be able to continue playing in the background.
I am trying to use iPhone OS 4.0's multitasking capability. I tried to play audio in the background with no luck. I added UIBackgroundModes property in info.plist and mentioned requires audio to play in background. Also I added the code for playing the audio.
`
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"someday" ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];
`. The audio starts playing once i click on button in the app. But when I shut the app it stops. How can I make it play in the background?
Thanks,
Tony
It sounds like you didn't set up your Audio Session correctly. From http://developer.apple.com/iphone/library/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html :
For example, when using the default audio session, audio in your application stops when the Auto-Lock period times out and the screen locks. If you want to ensure that playback continues with the screen locked, include the following lines in your application’s initialization code:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
The AVAudioSessionCategoryPlayback category ensures that playback continues when the screen locks.
Activating the audio session puts the specified category into effect.
HI,
I think this video helps u in solving ur problem...
IN WWDC videos they have clearly explained how u can enable the back ground audio...
http://developer.apple.com/videos/wwdc/2010/
to view or download these videos u need to have a apple account...
and in that see Session 109-Adopting multitasking on iPhone OS, Part2...
Hope this will help u..
~Raviraja