7

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

什么在起作用?

  • 如果视频正在播放并且我锁定屏幕,它会继续播放
  • I 可以从锁屏控件控制播放:下一个、上一个、播放、暂停。
  • 如果我按“主页”,然后从多任务栏中转到“遥控器”,它就可以了

怎么了?

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

我做了什么:

  • 在我的 app.plist 中将“音频”添加到“必需的背景模式”中

在应用代理的应用程序中添加了以下代码: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;
        }
    }
}

为什么一切正常,但是,这个?

4

5 回答 5

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];
}

在我看来,这不是最好的选择,但现在它正在工作,在恢复时播放会有一点跳跃。

于 2012-07-07T23:34:04.347 回答
4

请参阅Apple 文档中的此链接,并阅读视频媒体的特殊注意事项部分。这里解释了视频暂停的原因以及如何避免这种情况。它帮助了我。我尝试了两种选择,并且都运行良好。

于 2014-04-13T08:16:34.690 回答
3

斯威夫特 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)

选项 1AVPlayer -从其删除/添加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
      }
    }
  }
}
于 2017-04-28T07:30:23.750 回答
3

iOS14。谁遇到上述方法都不起作用的问题,用通知(Notification.Name("UISceneDidEnterBackgroundNotification")代替通知UIApplication.didEnterBackgroundNotification或AppDelegate中的相应方法。

于 2020-10-01T10:10:43.600 回答
1

对于迅速 4+ 。

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

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

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

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

@objc func appEnteredBackgound() {
  playerLayer.player = nil
}

@objc func appEnteredForeground() {
  playerLayer.player = player
}

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

@objc func appEnteredBackgound() {
        if let tracks = player?.currentItem?.tracks {
            for track in tracks {
                if (track.assetTrack?.hasMediaCharacteristic(AVMediaCharacteristic.visual))! {
                    track.isEnabled = false
                }
            }
        }
    }

    @objc func appEnteredForeground() {
        if let tracks = player?.currentItem?.tracks {
            for track in tracks {
                if (track.assetTrack?.hasMediaCharacteristic(AVMediaCharacteristic.visual))! {
                    track.isEnabled = true
                }
            }
        }
    }
于 2019-07-13T08:55:18.727 回答