iOS AVPlayer 后台播放问题自动停止问题 防止应用被后台挂起方法

 

iOS AVPlayer 后台播放问题自动停止问题 防止应用被后台挂起方法 

 分类:
iOS 开发笔记(37) 
 

目录(?)[+]

 

1、创建播放器时创建AVAudioSession

[objc]  view plain  copy
  1. AVAudioSession *session = [AVAudioSessionsharedInstance];  
  2. [session setCategory:AVAudioSessionCategoryPlaybackerror:nil];  
  3. [session setActive:YES error:nil];  

2、在plist文件中添加字段

Required background modes

在这里添加后台播放:

App plays audio or streams audio/video using AirPlay

3、在设备将要挂起app时添加下面代码

 

[objc]  view plain  copy
  1. - (void)applicationWillResignActive:(UIApplication *)application {  
  2.       
  3.     if ([MediaPlayerplayer].playStatus == MediaPlayerPlayStatusPlaying) {  
  4.         UIDevice* device = [UIDevicecurrentDevice];  
  5.         if ([devicerespondsToSelector:@selector(isMultitaskingSupported)]) {  
  6.             if(device.multitaskingSupported) {  
  7.                 if(device.multitaskingSupported) {  
  8.                     if ([MediaPlayerplayer].bgTaskId ==UIBackgroundTaskInvalid) {  
  9.                         [MediaPlayerplayer].bgTaskId = [[UIApplicationsharedApplication]beginBackgroundTaskWithExpirationHandler:NULL];  
  10.                     }  
  11.                 }  
  12.             }  
  13.         }  
  14.     }  
  15. }  

在设备进入前台时添加下面代码

 

[objc]  view plain  copy
  1. if ([MediaPlayerplayer].bgTaskId !=UIBackgroundTaskInvalid) {  
  2.         [[UIApplicationsharedApplication]endBackgroundTask:[MediaPlayerplayer].bgTaskId];  
  3.         [MediaPlayerplayer].bgTaskId =UIBackgroundTaskInvalid;  
  4.     }  

使用这三步基本可以保证在后台的长时间播放问题,且不会因为后台挂起APP导致的播放停止问题。

转载于:https://www.cnblogs.com/ios4app/p/6947625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值