使用Avplayer在后台播放video

在我的iPhone应用程序,我想保持播放video时,应用程序进入后台模式。

我正在使用AVPlayer,并没有find任何方式在后台播放video。 如果有人能帮助我,我将非常感激。 谢谢

我可以惊奇地发现,这可以实现,我只是做到了。

这种方法支持所有的可能性:

  • 屏幕由用户locking;
  • 主页button被按下;
  • 切换到其他应用程序。

只要你有一个运行iOS的AVPlayer实例防止设备自动locking。

首先,您需要configuration应用程序以支持来自Info.plist文件的audio背景,在UIBackgroundModes数组中添加audio元素。

然后把你的AppDelegate.m放到- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

这些方法

 [[AVAudioSession sharedInstance] setDelegate: self]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 

#import <AVFoundation/AVFoundation.h>

然后在你的视图控制器,控制AVPlayer

 -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; } 

 - (void)viewWillDisappear:(BOOL)animated { [mPlayer pause]; [super viewWillDisappear:animated]; [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; [self resignFirstResponder]; } 

然后回应

 - (void)remoteControlReceivedWithEvent:(UIEvent *)event { switch (event.subtype) { case UIEventSubtypeRemoteControlTogglePlayPause: if([mPlayer rate] == 0){ [mPlayer play]; } else { [mPlayer pause]; } break; case UIEventSubtypeRemoteControlPlay: [mPlayer play]; break; case UIEventSubtypeRemoteControlPause: [mPlayer pause]; break; default: break; } } 

如果用户按下主页button(在这种情况下再现被暂停而淡出),则需要另一个技巧来恢复再现。

当你控制video的再现(我play:pause:方法)设置

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil]; 

 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil]; 

并调用相应的方法启动一个定时器并恢复复制。

 - (void)applicationDidEnterBackground:(NSNotification *)notification { [mPlayer performSelector:@selector(play) withObject:nil afterDelay:0.01]; } 

当应用程序处于后台时,您可以执行此后台任务:

1>audio
2>位置
3> voip
4>报摊内容
5>外部附件
6>蓝牙中央
7>蓝牙外设
看到这个链接,

 http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html http://www.rideitdown.com/2012/10/how-to-listen-youtube-videos-in.html 

这也可以帮助你: AVPlayer在后台播放video

applicationDidEnterBackground试试这段代码:

 UIApplication *app = [UIApplication sharedApplication]; bgTask = 0; backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(backgroundTask) userInfo:nil repeats:YES]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; 

我发现它在堆栈上的某个地方适合我

另外看看这个教程,其中包括背景audio的背景模式.. http://www.raywenderlich.com/29948/backgrounding-for-ios

以上答案在应用程序进入后台暂停video一秒钟,但通过使用下面提到的方法,它保持video播放,而应用程序去背景没有任何小故障。

如果AVPlayer的当前项目在设备的显示屏上显示video,则当应用程序发送到后台时,AVPlayer的播放会自动暂停。 有两种方法可以防止这种暂停:

  1. 禁用播放器项目中的video轨道(仅限基于文件的内容)。
  2. 从其关联的AVPlayer中移除AVPlayerLayer(将AVPlayerLayer播放器属性设置为零)。

参考

在iOS上使用AV Foundation的背景中播放媒体