AVPlayer:当按下主页按钮时,背景音频停止,但在锁定屏幕时播放

时间:2012-07-05 06:43:52

标签: ios5 video background avplayer

好的,所以我的应用播放视频,我希望声音继续在后台播放。但是当我点击“Home”按钮时播放停止。我正在使用iOS 5.0.1 iPhone 4s进行测试

什么有效?

  • 如果视频正在播放且我锁定屏幕,则继续播放
  • 我可以控制从锁定屏幕控制播放:下一个,上一个,播放,暂停。
  • 如果我按“主页”,请从多任务栏中转到“遥控器”,可以

怎么了?

  • 当我按“主页”按钮时,我希望应用程序继续播放,因为当我这样做时,音频停止

我做了什么:

  • 在我的app.plist
  • 中为“所需的背景模式”添加了“音频”

在app delegate的应用程序中添加了以下代码:didFinishLaunchingWithOptions:

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];    
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

在我的视图控制器中,我实现了接收远程控制的方法:

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

    //End recieving events
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self play:nil];
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                [self actionPlaybackPreviousItem];
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                [self actionPlaybackNextItem];
                break;

            default:
                break;
        }
    }
}

一切如何运作但是,这个?

5 个答案:

答案 0 :(得分:4)

Stack Overflow每次响应变得越来越少......希望对于那里的每个人来说都不是那么糟糕。我没有发现任何回应,为什么视频在点击主页按钮后暂停,但是,如果它对任何人都有帮助,我会找到解决方法:

由于玩家正在玩,直到应用程序进入后台,我创建了一个计时器,以检查应用程序是否在前台播放时播放,如果是,则恢复播放:

- (void)applicationWillResignActive:(UIApplication *)application
{
    MainViewController* mainController=self.viewController;
    playerWasPlaying=mainController.player.rate!=0;
}

-(void)resumePlayback {
    MainViewController* mainController=self.viewController;
    if (mainController.player.rate==0&&playerWasPlaying)
        [mainController play:nil];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(resumePlayback) userInfo:nil repeats:NO];
}

在我看来,这不是最好的选择,但是现在它正在工作,在重新开始播放时稍微跳了一下。

答案 1 :(得分:4)

请参阅Apple文档中的此{​​{3}},并阅读视频媒体的特殊注意事项部分。这里解释了为什么视频暂停以及如何避免这种情况。它帮助了我。我尝试了两种选择,两者都运作良好。

答案 2 :(得分:3)

iOS14 。如果遇到上述方法无效的问题,请使用通知(Notification.Name("UISceneDidEnterBackgroundNotification")而不是通知UIApplication.didEnterBackgroundNotification或AppDelegate中的相应方法。

答案 3 :(得分:2)

Swift 3

当您的应用进入后台并返回时,首先注册通知:

NotificationCenter.default.addObserver(self, selector: #selector(appEnteredBackgound), name: Notification.Name.UIApplicationDidEnterBackground, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(appEnteredForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)

选项1 - 从AVPlayer移除/添加AVPlayerLayer

func appEnteredBackgound() {
  playerLayer.player = nil
}

func appEnteredForeground() {
  playerLayer.player = player
}

选项2 - 禁用/启用视频曲目:

func appEnteredBackgound() {
  if let tracks = player.currentItem?.tracks {
    for track in tracks {
      if track.assetTrack.hasMediaCharacteristic(AVMediaCharacteristicVisual) {
        track.isEnabled = false
      }
    }
  }
}

func appEnteredForeground() {
  if let tracks = player.currentItem?.tracks {
    for track in tracks {
      if track.assetTrack.hasMediaCharacteristic(AVMediaCharacteristicVisual) {
        track.isEnabled = true
      }
    }
  }
}

答案 4 :(得分:0)

快速4 +。

当您的应用进入后台并返回时,请先注册以获取通知:

<TablePhoneHome><Number Num="1111"/></TablePhoneHome>

选项1-从其AVPlayerLayer中删除/添加AVPlayer:

1111

选项2-禁用/启用视频轨道:

NotificationCenter.default.addObserver(self, selector: #selector(appEnteredBackgound), name: UIApplication.didEnterBackgroundNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(appEnteredForeground), name: UIApplication.willEnterForegroundNotification, object: nil)