iphone - 在背景中播放音频流

CdB*_*CdB 5 iphone audio background-process mpmovieplayercontroller

我正在使用xcode 4.2并构建一个iphone应用程序,并且有一个视图,当打开时播放音频流.
我试图实现的是让应用程序继续播放即使它进入后台.
我已经尝试了在stackoverflow或其他地方找到的任何可能的解决方案(并且有许多可用的),但无法使其工作.当应用程序进入后台时,音频停止,当我再次打开应用程序时,音频继续.

在我的Info.plist中,我设置了2行:
Required Background Modes -> App plays audio&
Application does not run in background -> NO

我错过了什么?还有什么需要继续在后台播放音频?
先感谢您.

编辑:我在这个问题上看到很多答案,建议使用AVAudio Framework.MPMoviePlayerController是否有可能无法在后台播放流?我应该换成AVAudio吗?

编辑2:
好的,似乎对我来说太复杂了.我给出了确切的代码,希望这会有所帮助.

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  
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Til*_*ill 6

检查您的音频会话设置,因为可能需要额外注意.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
                                       error:nil];
[[AVAudioSession sharedInstance] setActive:YES 
                                     error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Run Code Online (Sandbox Code Playgroud)