在后台播放音频流



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

在我的Info.plist中,ia设置了两行:
CCD_ 1&
Application does not run in background -> NO

我错过了什么?还需要什么才能在后台继续播放音频
提前谢谢。

编辑:我看到了很多关于这个问题的答案,建议使用AVAudio框架。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  

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

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

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
                                       error:nil];
[[AVAudioSession sharedInstance] setActive:YES 
                                     error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

How在这里偶然发现了详细的编程教程。http://mobisoftinfotech.com/integrate-music-player-in-iphone/

这应该在应用程序中播放音乐,如果你在后台使用应用程序,也可以继续播放。

最新更新