开发者

MPMoviePlayerController in Background mode

I am trying to play streaming file from particular URL using MPMoviePlayerController. It is working fine when I load the player but I need to keep it running when application enter into the background mode (Multitasking). This is what I am doing:

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

    //NSURLRequest *audioFile = [[NS开发者_运维技巧URLRequest 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];         

}

Now when I press iPhone home button while running the audio, it pauses the streaming until I load back the Application. I need to keep it running when app enter into background.


In your plist file, did you set the UIBackgroundModes key to audio ? Without this, your application will not play any sound in background

Note that in iOS5, UIBackgroundModes is now referenced as Required background modes when you edit your plist file.


See mpmovieplayercontroller-multitasking-problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜