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处于“使用中 ...
随机推荐
- Express+MongoDB开发web后台接口MongoDB
摘要: Express开发web接口; 安装MongoDB,启动.连接MongoDB服务台; 使用nodejs和mongoose模块链接和操作MongoDB; 一.Express开发web接口 exp ...
- 个人博客添加网易云音乐Flash插件
博客底部添加网易云音乐播放插件 歌单或者歌曲外链可从音乐界面"生成外链播放器"中得到,选择Flash播放插件即可 footer.html文件增加 实现效果: 历史精选文章: Jli ...
- 不容错过的超赞项目管理PPT
不容错过的超赞项目管理PPT(转载) 大公司的一个好处,是各个领域都有牛人,可以为你提供经验分享交流.腾讯庞大的培训体系更是保证了:如果你想学点什么东西,你总可以学到.腾讯内部资源30页PPT曝光 — ...
- 《React Native 精解与实战》书籍连载「React Native 网络请求与列表绑定」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- VS2012添加数据库连接时报错,未能加载文件或程序集microsoft.sqlserver.management.sdk.sfc
今天在VS2012中添加数据库连接时报错 未能加载文件或程序集microsoft.sqlserver.management.sdk.sfc,Version=11.0 查了很多资料,最后下载安装了Sha ...
- python第五章:文件--小白博客
文件操作, 操作文件完毕后一定要记得close # 读,默认是rt(文本的方式读取),rb模式是以字节读取 # 文件路径可以用3中形式表示 f = open(r'C:\Users\fengzi\Des ...
- Latex(表格|图片(一丢丢))
目录 普通的例子 Notation 例子 p{width} 列分割符 @{} \multicolumn supertabular | longtabular 浮动体 table 浮动体 图片 \use ...
- debian6保存iptables规则
iptables规则不保存,一旦机器重启规则就清空了,所以需要保存: iptables-save >/etc/iptables-script vi /etc/rc.local 然后在文件中输入: ...
- HTML,CSS笔记
text-indent 属性规定文本块中首行文本的缩进.允许使用负值.如果使用负值,那么首行会被缩进到左边.p{ text-indent:50px; } HTML <label> 标签的 ...
- Linux sudoers
xxx is not in the sudoers file.This incident will be reported.的解决方法 - xiaochaoyxc - 博客园http://www.cn ...