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
Start playing a movie or TV show. Swipe up from the bottom screen after it starts playing, as if you're closing the app. The video will start playing in a small window on your screen. You can resize the window by pinching or pulling it to make the video even bigger.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With