简体   繁体   English

当应用程序进入后台时,AVPlayer暂停并播放问题

[英]AVPlayer Pause and play issues when app goes background

It's working correctly if the app just become inactive/active (when some alert came or by double clicking on home button) 如果应用程序变为非活动/活动状态(当出现某些警报或双击主页按钮时)它正常工作

AVPlayer *player;

- (void)applicationWillResignActive:(UIApplication *)application
{
    [player pause];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [player play];
}

If the app goes to background (by clicking home button) and comes back, it's not playing from paused position instead of that it is playing from different point (sometimes from start, sometimes from middle). 如果应用程序进入后台(通过单击主页按钮)并返回,它不会从暂停位置播放而不是从不同点播放(有时从开始,有时从中间)。

Follow below steps: 请遵循以下步骤:

1) Firstly add float *time in appDelegate.h file. 1)首先在appDelegate.h文件中添加float *time

2) Take current time in applicationWillResignActive 2)在applicationWillResignActive获取current time

3) Add below methods 3)添加以下方法

ApplicationWillResignActive method pause player and save current time of player ApplicationWillResignActive方法pause播放器并save播放器的current time

- (void)applicationWillResignActive:(UIApplication *)application
{
   [player pause];
   time = player.currentTime
}

Now in applicationDidBecomeActive add seekToTime 现在在applicationDidBecomeActive添加seekToTime

- (void)applicationDidBecomeActive:(UIApplication *)application
{
  [player seekToTime:time toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
  [player play];
}

You should call the this methods before application is going to background. 您应该在应用程序转到后台之前调用此方法。 [player pause]; [玩家暂停]; or [player stop]; 或[玩家停止];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM