2012-05-07 119 views
2

我使用AVPlayer在我的应用中播放视频,现在我想让视频在后台模式下播放。AVPlayer在后台播放视频

这是我放在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

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

,我也加入到plist中: 所需的背景模式 - >应用程序播放音频

我也把它加上:

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

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

- (void)remoteControlReceivedWithEvent:(UIEvent *)event { 
    switch (event.subtype) { 
     case UIEventSubtypeRemoteControlTogglePlayPause: 
      NSLog(@"4"); 
      break; 
     case UIEventSubtypeRemoteControlPlay: 
      break; 
     case UIEventSubtypeRemoteControlPause: 
      NSLog(@"3"); 
      break; 
     case UIEventSubtypeRemoteControlNextTrack: 
      NSLog(@"2"); 
      break; 
     case UIEventSubtypeRemoteControlPreviousTrack: 
      NSLog(@"1"); 
      break; 
     default: 
      break; 
    } 
} 

当我将应用程序移动到背景并按下nslog打印到控制台时

我需要添加其他东西吗?

+0

请点击此链接: http://stackoverflow.com/questions/4771105/how-do-i-get-my-avplayer-to-play-while-app-is-in-background – iMash

回答

1

只需添加[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];和其他一些调整。这全是here

+0

我编辑自己的帖子等你写下了什么 – MTA

+1

。这工作?视频在后台工作?从互联网上的所有噪音中,我认为背景视频在iOS上是不可能的! @MTA,请确认是否可能。 –

+0

@dev我可以证实它的工作原理。您需要锁定设备,而不是按主页按钮。您还可以支持“UIApplicationDidEnterBackgroundNotification”的主页按钮注册,并通过定时器重新启动视频,您会有一点滞后(淡化),但总比没有好。 – MacTeo