UIWebview radio stream plays in background? - ios

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];

Related

Why does AVAudioPlayer does not play?

We developed this iOS scenario:
1) iOS App schedules local push notification
2) Push is a trigger to start AVAudioPlayer audio playing - even if the iPhone is locked (app in background)
Now there are two scenarios:
a) Time difference between push scheduling is more than 3 hours:
Audio does start playing after unlocking and starting the app
b) Time difference between push scheduling is less than 3 hours:
Audio does start playing even without unlocking
We want case b as default case. Does iOS stop/freeze our application after some hours? Interesting: On indian devices there does not seem to be case a - even if time difference is more than 12 hours.
Any ideas?
Thanks so much!
Did you integrate background execution? Otherwise the app will be suspended after a while.
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
Once didReceiveLocalNotification method triggered in appdelegate, then trigger one method in appdelegate or in global then use below code to play audio file using AVAudioPlayer in background or device lock
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#“filename”
ofType:#“type”];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
player.numberOfLoops = -1; //Infinite
[player prepareToPlay];
[player play];

iOS - Sending one audio stream to the built-in speaker and a different stream to a Bluetooth HFP speaker

I am building an alarm clock app which works in conjunction with a physical Bluetooth device. I need to send one audio file (a song selected from the iTunes Library) to the built-in speaker and a separate audio file to the Bluetooth device (which is, effectively, a Bluetooth HFP speaker) at the same time.
My first thought in completing this would be to use AVAudioSession's new AVAudioSessionCategoryMultiRoute, but iOS 7 does not detect my speaker as a possible route; it will detect either the built-in speaker or the HFP speaker, but it will not detect both simultaneously. Detecting both is required to send two files at the same time.
My second thought was to use AVAudioSessionCategoryPlayAndRecord (even though the app has no need for a microphone) and to use overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker and overrideOutputAudioPort:AVAudioSessionPortOverrideNone like so:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"alarm"
ofType:#"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
self.bluetoothAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
self.bluetoothAudioPlayer.delegate = self;
[self.bluetoothAudioPlayer play];
NSError *error = nil;
NSURL *selectedSongURL = [self.selectedSong valueForProperty:MPMediaItemPropertyAssetURL];
NSLog(#"selectedSongURL: %#", selectedSongURL);
if (selectedSongURL) {
if (![[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]) {
NSLog(#"overrideOutput error: %#", error);
}
self.internalAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:selectedSongURL error:nil];
self.internalAudioPlayer.delegate = self;
[self.internalAudioPlayer play];
}
After this alarm is silenced, I run [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil]; to reset the override call.
However, this doesn't work because iOS just puts both audio signals on the same speaker.
Is there a way to send one audio stream to a Bluetooth HFP speaker and a different audio stream to the built-in speaker?

iOS AVAudioPlayer sound stops playing when screen is locked

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.

Audio playing in background not working in iOS Simulator

I've seen lots of questions about playing audio in the background. These questions often ask why the audio plays in the background of the simulator, yet the device simply wouldn't.
In my case, it's reversed.
When I tested my sample app in my iPad, it worked perfectly. However, when I began testing in the simulator, it simply wouldn't continue playing in the background.
Here are the codes I used:
NSURL *url = [NSURL URLWithString:firstSong];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer setNumberOfLoops:-1];
[audioPlayer setCurrentTime:0];
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance]setDelegate:self];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
[audioPlayer play];
Can anyone tell me why this won't work? Is it a bug or something?
Background audio is not supported in iOS Simulator afaik.

AVPlayer streaming audio in background

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

Resources