iOS后台运行播放无声音频 测试可行
如果打回来了,就自认倒霉吧
制作无声音频。
@interface AppDelegate ()
{
NSInteger count;
}
@property(strong, nonatomic)NSTimer *mTimer;
@property(assign, nonatomic)UIBackgroundTaskIdentifier backIden;
- (void)applicationDidEnterBackground:(UIApplication *)application {
_mTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_mTimer forMode:NSRunLoopCommonModes];
[self beginTask];
}
//计时
-(void)countAction{
NSLog(@"%li",count++);
}
//申请后台
-(void)beginTask
{
NSLog(@"begin=============");
_backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"将要挂起=============");
[self endBack];
}];
}
//注销后台
-(void)endBack
{
NSLog(@"end=============");
[[UIApplication sharedApplication] endBackgroundTask:_backIden];
_backIden = UIBackgroundTaskInvalid;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self endBack];
}
#import <AVFoundation/AVFoundation.h>
@property(strong, nonatomic)AVAudioPlayer *mPlayer;
@property(assign, nonatomic)CGFloat mCount;
- (void)viewDidLoad {
[super viewDidLoad];
_mCount = 0;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)countTime{
_mCount+=10;
NSLog(@"%f",_mCount);
if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 60.) {//当剩余时间小于60时,开如播放音乐,并用这个假前台状态再次申请后台
NSLog(@"播放%@",[NSThread currentThread]);
[self playMusic];
//申请后台
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"我要挂起了");
}];
}
}
-(void)playMusic{
//1.音频文件的url路径,实际开发中,用无声音乐
NSURL *url=[[NSBundle mainBundle]URLForResource:@"wusheng.mp3" withExtension:Nil];
//2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
_mPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
//3.缓冲
[_mPlayer prepareToPlay];
_mPlayer.numberOfLoops = -1;
//4.播放
[_mPlayer play];
}
iOS后台运行播放无声音频 测试可行的更多相关文章
- iOS 后台运行 类型
iOS后台运行,需要有特定的类型才可以进行.这些内容并不是一直不变的,苹果也在逐步的更新这些内容. 本文内容是2015年11月03日时苹果支持的后台运行类型. 这是官方连接地址 其中较为重要的是下面这 ...
- IOS后台运行 之 后台播放音乐
iOS 4开始引入的multitask,我们可以实现像ipod程序那样在后台播放音频了.如果音频操作是用苹果官方的AVFoundation.framework实现,像用AvAudioPlayer,Av ...
- iOS后台运行
http://www.cocoachina.com/bbs/read.php?tid=149564 文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后 ...
- iOS 后台运行实现 --备用
文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后台运行一小段时间. 还有三种类型的可以运行在后以,1.音乐2.location 3.voip 文二 ...
- IOS 后台执行 播放音乐
iOS 4開始引入的multitask.我们能够实现像ipod程序那样在后台播放音频了. 假设音频操作是用苹果官方的AVFoundation.framework实现.像用AvAudioPlayer.A ...
- IOS 后台运行
默认情况下,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作.但是应用可以调用UIApplication的beginBackgroundTaskWithExpirat ...
- iOS用AVAudioPlayer播放m4a音频
音频文件sound.m4a放到Supporting Files目录 引用头文件 #import <AVFoundation/AVFoundation.h> 定义一个全局的属性: @prop ...
- iOS 后台运行执行代码(例如定位)
- iOS开发:后台运行以及保持程序在后台长时间运行
第一部分 1.先说说iOS 应用程序5个状态: 停止运行-应用程序已经终止,或者还未启动. 不活动-应用程序处于前台但不再接收事件(例如,用户在app处于活动时锁住了设备). 活动-app处于“使用中 ...
随机推荐
- Java中的hashCode() 和 equals()的若干问题解答
一.hashCode()的作用 哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int ...
- python内置的高效好用各种库
二分查找,import bisect 堆排序,import heapq 哈希算法,import hashlib 压缩,lzma 图形处理,PIL 处理xml文件,PyXML 多媒体操作,PyMedia ...
- git 分支管理规范
保证master分支永远处于可部署的状态.禁止自接提交代码到master分支 开发分支基于master分支创建,命名规范如下: 如果是功能需求,分支命名为feature/xxx,xxx要具有描述性 如 ...
- TensorFlow 使用变量共享
参考: https://www.tensorflow.org/programmers_guide/variable_scope 举例说明 TensorFlow中的变量一般就是模型的参数.当模型复杂的时 ...
- 基于linux下的krpano的使用
鉴于目前网络上关于krpano的使用和介绍少之又少,结合自己的学习和使用经历,做个总结和记录. 1.安装 下载地址: linux https://krpano.com/forum/wbb/index. ...
- openstack-KVM-存储配置
一.块存储设备 1.存储设备类型 IDE SCSI 软盘 U盘 virtio磁盘(KVM使用类型) 2.查看存储设备 lspci | grep IDE lspci | grep SCSI lspci ...
- Nginx Configuring HTTPS servers
Configuring HTTPS servershttp://nginx.org/en/docs/http/configuring_https_servers.html Configuring HT ...
- 给网站配置免费的HTTS证书
取经自思否:https://segmentfault.com/a/1190000015231137 https 的网站 搜索引擎 会优先收录,所以就抽时间记录下配置博客的过程,各种找资料,终于给我找到 ...
- Azure系列2.1.7 —— BlobRequestOptions
(小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...
- [转帖]ipvsadm命令参考及其应用例子
ipvsadm命令参考及其应用例子 https://blog.csdn.net/orichisonic/article/details/47375227 只是简单创建了 service和添加serve ...