Reputation: 4908
I'm using xcode 4.2 and building an iphone app, and have a view that when opened plays an audio stream.
What i try to achieve is have the app continue playing even if it enters the background.
I've tried any possible solution that i found in stackoverflow or elsewhere (and there are many available), but can't get it work. When the application enters background the audio stops, when i open the app again the audio continues.
In my Info.plist ia have set 2 rows:
Required Background Modes -> App plays audio
&
Application does not run in background -> NO
What am i missing? what else is needed to keep playing the audio in the background?
Thank you in advance.
EDIT: I see many answers on the issue, suggestiong the use of AVAudio Framework. Is there any chance that MPMoviePlayerController is not able to play the stream in the background? Should i change to AVAudio?
EDIT 2:
Ok, seems it's too complex for me to achieve. I'm giving the exact code, hope this will help.
RadioStreamer.h
#import <UIKit/UIKit.h>
#import "MediaPlayer/MediaPlayer.h"
@interface RadioStreamer : UIViewController {
MPMoviePlayerController *player;
IBOutlet UIButton *playButton;
IBOutlet UIButton *pauseButton;
}
@property (nonatomic, retain) MPMoviePlayerController *player;
@property (nonatomic, retain) IBOutlet UIButton *playButton;
@property (nonatomic, retain) IBOutlet UIButton *pauseButton;
- (IBAction)playButtonPressed:(id)button;
- (IBAction)pauseButtonPressed:(id)button;
- (void) playAudio;
- (void) pauseAudio;
@end
RadioStreamer.m
#import "RadioStreamer.h"
#import "tabbartestAppDelegate.h"
@implementation RadioStreamer
@synthesize player;
@synthesize playButton;
@synthesize pauseButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title=@"";
// Do any additional setup after loading the view from its nib.
if (!self.player) {
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://s6.onweb.gr:8006/listen.pls"]];
player.movieSourceType = MPMovieSourceTypeStreaming;
player.view.frame = CGRectMake(55, 180, 200, 30);
player.backgroundView.backgroundColor = [UIColor clearColor];
player.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:player.view];
[player play];
}
}
- (void) playAudio {
if (player.playbackState != MPMoviePlaybackStatePlaying){
[player play];
}
}
- (void) pauseAudio {
if (player.playbackState == MPMoviePlaybackStatePlaying) {
[player pause];
}
}
- (IBAction)playButtonPressed:(id)button {
[self playAudio];
}
- (IBAction)pauseButtonPressed:(id)button {
[self pauseAudio];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc {
[self.player release];
[self.playButton release];
[self.pauseButton release];
self.player = nil;
self.playButton = nil;
self.pauseButton = nil;
}
@end
Upvotes: 5
Views: 6086
Reputation: 41
How stumbled upon the programming tutorial in details at this place. http://mobisoftinfotech.com/integrate-music-player-in-iphone/
This should play the music within the app and also continue to play if you take your app in background.
Upvotes: 0
Reputation: 27597
Check your audio session setup as that might need some extra attention.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
error:nil];
[[AVAudioSession sharedInstance] setActive:YES
error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Upvotes: 6