MPMoviePlayerViewController 在转到 youtube 后台后播放视频 [英] MPMoviePlayerViewController playback video after going to background for youtube

查看:36
本文介绍了MPMoviePlayerViewController 在转到 youtube 后台后播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的 info.plist 中为所需的后台模式设置了应用程序播放音频,以保持 mpmovieplayerviewcontroller 在进入后台后不会关闭.它做得很好.但是,Apple 最近拒绝了我的应用更新,因为我设置了该属性.

I have set App Plays Audio for Required background modes in my app's info.plist to keep the mpmovieplayerviewcontroller not dismissing after going to background. It did a great job. However, Apple has rejected my app's update recently, as I set that attribute.

然后我一直在寻找解决方案来做同样的工作,但失败了.为了处理它,我在视图控制器收到 UIApplicationDidEnterBackgroundNotification 时保存当前播放时间

Then I keep finding solutions for doing the same job but fail. To deal with it, I save the currentplaybacktime when the view controller receive UIApplicationDidEnterBackgroundNotification

-(void)willEnterBackground {
    NSLog(@"willEnterBackground");
    if (self.mp) {
        playbackTime = self.mp.moviePlayer.currentPlaybackTime;
        [self.mp dismissModalViewControllerAnimated:YES];
    }
}

并在收到 UIApplicationWillEnterForegroundNotification 后设置 currentPlayBackTime:

And set the currentPlayBackTime after it receives UIApplicationWillEnterForegroundNotification:

- (void)willEnterForeground {
    NSLog(@"willEnterForeground");

    if (!self.mp)
        return;
    if(self.mp.moviePlayer.playbackState == MPMoviePlaybackStateInterrupted || self.mp.moviePlayer.playbackState == MPMoviePlaybackStateStopped || self.mp.moviePlayer.playbackState == MPMoviePlaybackStatePaused)
    {
        [self continuePlayback];
    }
}

- (void)continuePlayback {
    NSLog(@"%f", self.mp.moviePlayer.duration);
    NSLog(@"%f", playbackTime);
    [self.mp.moviePlayer stop];
    self.mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[self.videos objectForKey:@"medium"]]] ;
    [self.mp.moviePlayer setInitialPlaybackTime:playbackTime];
    [self.mp.moviePlayer play];
    [self presentModalViewController:self.mp animated:YES];
}

它有效,但有一些权衡:当我重新初始化它时,它丢失了流式视频部分.用户需要再次等待流式传输.

It works, but with some trade off: As I re-init it, it lost the streamed video part. Users need to wait for streaming again.

我的问题来了:

  1. 有没有办法在应用程序运行时继续播放 Youtube 的视频?在后台?
  2. 或者如何保留流媒体部分?

如果我跳过该行,它将从视频的开头播放:

It would play from the beginning of the video if I skip the line:

self.mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[self.videos objectForKey:@"medium"]]] ;

在 continuePlayBack 方法中或我 setCurrentPlayBackTime 而不是 setInitPlayBackTime.

in continuePlayBack method or I setCurrentPlayBackTime instead of setInitPlayBackTime.

推荐答案

您在那里发现的情况完全正常 - 很奇怪,但很正常.MPMoviePlayerController 在新实例上使用时仅尊重 initialPlaybackTime.我确实敦促您提交关于该问题的错误报告,因为它自古以来就已为人所知 (iOS3),但尚未修复.

What you have discovered there is perfectly normal - weird, but normal. MPMoviePlayerController does only respect the initialPlaybackTime when being used on a fresh instance. I do actually urge you to file a bug-report on that issue as it is known since ages (iOS3) but not fixed.

没有办法保持缓冲区状态,您起草的解决方案与我在许多应用程序中使用的解决方案几乎相同.

There is no way to keep the buffer state and the solution you have drafted is pretty much the same that I am using in many apps.

现在直接回答你的问题;

Now directly answering your questions;

re 1) 是和否.正如您自己发现的那样,Apple 确实对 UIBackgroundModes 的使用非常苛刻.尽管他们明确推荐使用它来实现不间断的 AirPlay 流(视频和音频内容),但当他们所做的只是播放视频时,他们仍然拒绝应用程序.我还认为 Apple 审查团队方面的一次史诗般的失败.您可能想尝试与他们的拒绝作斗争,引用他们自己的文档:

re 1) Yes and no. As you have discovered yourself, Apple did get very harsh on the UIBackgroundModes usage. Even though they clearly recommend using it for achieving uninterrupted AirPlay streaming (of video and audio content), they still reject apps when all they do is playing a video. That I also consider an epic failure on Apple's review team side. You might want to try to fight their rejection, quoting their own documentation:

重要提示:UIBackgroundModes 音频键将允许应用流式传输使用 AirPlay 在后台播放音频或视频内容.

Important: The UIBackgroundModes audio key will allow apps to stream audio or video content in the background using AirPlay.

来自:http://developer.apple.com/library/ios/#qa/qa1668/_index.html

re 2) 否(见介绍性回答文本)

re 2) No (see introductional answer text)

让我也从另一个方面解决这个问题...

Let me also tackle the issue from a different side...

如果您的问题实际上是在 iOS5 上,设备在通过 AirPlay 流式传输时进入睡眠模式.然后,实际上有一种可能的解决方法至少是有效的 - 即使它非常讨厌......

If your problem actually is the fact that on iOS5 the device is going into sleep mode while streaming via AirPlay. Then, there is actually one possible workaround that at least works - even though it is nasty as hell...

您可以注册MPMoviePlayerIsAirPlayVideoActiveDidChangeNotification 并检查钩子方法中的airPlayVideoActive.一旦 AirPlay 处于活动状态,只需通过设置 [UIApplication sharedApplication].idleTimerDisabled = YES; 来防止空闲,记住一旦 AirPlay 处于非活动状态,请执行相反的操作.所有这些应该只在 iOS5 上需要,因为 iOS6 修复了 AirPlay 处于活动状态时的空闲问题.

You can register for MPMoviePlayerIsAirPlayVideoActiveDidChangeNotification and check the airPlayVideoActive within the hooked method. Once AirPlay is active, just prevent idling by setting [UIApplication sharedApplication].idleTimerDisabled = YES; Remember to do the reverse once AirPlay became inactive. All of this should only be needed on iOS5 as iOS6 fixed the idling issue while AirPlay is active.

这篇关于MPMoviePlayerViewController 在转到 youtube 后台后播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆