后台模式下的 MPMoviePlayerController
我正在尝试使用 MPMoviePlayerController 从特定 URL 播放流媒体文件。当我加载播放器时它工作正常,但当应用程序进入后台模式(多任务处理)时我需要保持它运行。这就是我正在做的事情:
-(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 主页按钮时,它会暂停流式传输,直到我加载回应用程序。当应用程序进入后台时,我需要保持它运行。
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 = [[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];
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 plist 文件中,您是否将
UIBackgroundModes
键设置为 audio ?如果没有这个,您的应用程序将不会在后台播放任何声音。请注意,在 iOS5 中,当您编辑 plist 文件时,
UIBackgroundModes
现在被引用为必需的背景模式
。In your plist file, did you set the
UIBackgroundModes
key to audio ? Without this, your application will not play any sound in backgroundNote that in iOS5,
UIBackgroundModes
is now referenced asRequired background modes
when you edit your plist file.请参阅mpmovieplayercontroller-multitasking-problem
See mpmovieplayercontroller-multitasking-problem