步骤一:在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技术专栏