【问题标题】:AVPlayer play video in backgroundAVPlayer 在后台播放视频
【发布时间】:2012-05-07 07:59:26
【问题描述】:

我正在使用 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 会打印到控制台

我需要添加其他内容吗?

【问题讨论】:

标签: iphone objective-c


【解决方案1】:

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

【讨论】:

  • 等等。这行得通吗?视频在后台工作?从互联网上的所有噪音来看,我认为背景视频在 iOS 上是不可能的! @MTA,如果可能,请确认。
  • @dev 我可以确认它有效。您需要锁定您的设备,而不是按主页按钮。您还可以支持主页按钮注册UIApplicationDidEnterBackgroundNotification 并使用计时器重新启动视频,您会有点滞后(褪色),但总比没有好。
  • 我已经回答了this 问题,详细说明了如何达到预期的结果。
  • 解释链接已损坏
猜你喜欢
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多