Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone - play audio stream in background

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
like image 394
CdB Avatar asked Mar 26 '12 16:03

CdB


People also ask

How can I play movies in the background on my iPhone?

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.


1 Answers

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];
like image 104
Till Avatar answered Oct 05 '22 06:10

Till