步驟一:在resource文件夾下找到該項(xiàng)目的info.plist添加一個(gè)
Required background modes 的數(shù)組
并在下面添加一個(gè)元素,其值為
App plays audio
步驟二:
需要導(dǎo)入相應(yīng)的framework和頭文件
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
步驟三:
//禁止程序運(yùn)行時(shí)自動(dòng)鎖屏
[[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"]; //創(chuàng)建音樂文件路徑
NSURL * musicURL= [[NSURLalloc]initFileURLWithPath:musicFilePath];
AVAudioPlayer * thePlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:musicURLerror:nil];
[thePlayerplay];
[thePlayersetVolume:1]; //設(shè)置音量大小
thePlayer.numberOfLoops = -1;//設(shè)置音樂播放次數(shù) -1為一直循環(huán)
[musicURLrelease];
[thePlayerrelease];
更多信息請(qǐng)查看IT技術(shù)專欄