背景模式下的MPMoviePlayerController

时间:2011-04-28 07:26:51

标签: iphone background mpmovieplayercontroller multitasking

我正在尝试使用MPMoviePlayerController播放来自特定网址的流媒体文件。当我加载播放器时工作正常,但是当应用程序进入后台模式(多任务处理)时我需要保持运行。这就是我在做的事情:

-(void) playAudio :(NSString *)playAudioURL
{

    //NSURLRequest *audioFile = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString:self.playAudioURL] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];  

    MPMoviePlayerController *mediaPlayer  = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:playAudioURL]];

    mediaPlayer.scalingMode = MPMovieScalingModeAspectFill; 

    mediaPlayer.scalingMode = MPMovieScalingModeAspectFill;

    mediaPlayer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home-bg.png"]];

    //[self.view addSubview:mediaPlayer.view];  

    mediaPlayer.fullscreen = YES; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(myMovieFinishedCallback:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:mediaPlayer]; 


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(videoPreloadCallback:)
                                                 name:MPMoviePlayerContentPreloadDidFinishNotification
                                               object:mediaPlayer];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];

    [mediaPlayer play];         

}

现在,当我在运行音频时按下iPhone主页按钮时,它会暂停流式传输,直到我加载应用程序。当app进入后台时,我需要让它继续运行。

2 个答案:

答案 0 :(得分:1)

在plist文件中,您是否将UIBackgroundModes键设置为音频?如果没有这个,您的应用程序将无法在后台播放任何声音

请注意,在iOS5中,当您编辑plist文件时,UIBackgroundModes现在被引用为Required background modes

答案 1 :(得分:0)