简要咨询咨询QQ网站导航网站搜索手机站点联系我们设为首页加入收藏 

防止iphone进入休眠后台播放音乐

来源:易贤网   阅读:996 次  日期:2014-11-04 11:38:13

温馨提示:易贤网小编为您整理了“防止iphone进入休眠后台播放音乐”,方便广大网友查阅!

步骤一:在resource文件夹下找到该项目的info.plist添加一个

Required background modes 的数组

并在下面添加一个元素,其值为

App plays audio

步骤二:

需要导入相应的framework和头文件

#import <AudioToolbox/AudioToolbox.h>

#import <AVFoundation/AVFoundation.h>

步骤三:

//禁止程序运行时自动锁屏

[[UIApplicationsharedApplication]setIdleTimerDisabled:YES];

步骤四:

AVAudioSession *audioSession = [AVAudioSessionsharedInstance];

NSError *err =nil;

[audioSession setCategory :AVAudioSessionCategoryPlayAndRecorderror:&err];

if(err)

{

NSLog(@"audioSession: %@ %d %@", [errdomain], [errcode], [[erruserInfo]description]);

}

[audioSessionsetActive:YESerror:&err];

err =nil;

if(err)

{

NSLog(@"audioSession: %@ %d %@", [errdomain], [errcode], [[erruserInfo]description]);

}

NSString * musicFilePath = [[NSBundlemainBundle]pathForResource:@"初音ミク-World Is Mine" ofType:@"mp3"]; //创建音乐文件路径

NSURL * musicURL= [[NSURLalloc]initFileURLWithPath:musicFilePath];

AVAudioPlayer * thePlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:musicURLerror:nil];

[thePlayerplay];

[thePlayersetVolume:1]; //设置音量大小

thePlayer.numberOfLoops = -1;//设置音乐播放次数 -1为一直循环

[musicURLrelease];

[thePlayerrelease];

更多信息请查看IT技术专栏

更多信息请查看技术文章
点此处就本文及相关问题在本站进行非正式的简要咨询(便捷快速)】     【点此处查询各地各类考试咨询QQ号码及交流群
上一篇:解决AssetsLibrary无法获取本地所有相册
下一篇:如何查看经过iOS优化的PNG图片
易贤网手机网站地址:防止iphone进入休眠后台播放音乐
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!