一.播放音效
用到的框架:
1.AVFoundation.framework
2.AudioToolbox.framework
步驟:
0.創(chuàng)建URL
1.創(chuàng)建音效ID
?SystemSoundID soundID;//只能創(chuàng)建一次抒倚,所以創(chuàng)建一個(gè)懶加載
?需要把URL橋接成CFURL
2.播放本地音效
具體代碼如下:
1.獲得音效文件的路徑
NSURL*url= [[NSBundlemainBundle]URLForResource:@"m_03.wav"withExtension:nil];2.加載音效文件昵骤,得到對應(yīng)的音效ID
SystemSoundIDsoundID=0;
AudioServicesCreateSystemSoundID((__bridgeCFURLRef)(url), &soundID);
?3.播放音效
AudioServicesPlaySystemSound(soundID);
音效文件只需要加載1次
音效播放常見函數(shù)總結(jié)
加載音效文件
AudioServicesCreateSystemSoundID(CFURLRefinFileURL,SystemSoundID*outSystemSoundID)
釋放音效資源
AudioServicesDisposeSystemSoundID(SystemSoundIDinSystemSoundID)
播放音效
AudioServicesPlaySystemSound(SystemSoundIDinSystemSoundID)
播放音效帶點(diǎn)震動(dòng)
AudioServicesPlayAlertSound(SystemSoundIDinSystemSoundID)
二.播放音樂
音樂播放用到一個(gè)叫做AVAudioPlayer的類
AVAudioPlayer常用方法
加載音樂文件
- (id)initWithContentsOfURL:(NSURL*)urlerror:(NSError**)outError;
- (id)initWithData:(NSData*)data error:(NSError**)outError;
準(zhǔn)備播放(緩沖微峰,提高播放的流暢性)
- (BOOL)prepareToPlay;
播放(異步播放)
- (BOOL)play;
暫停
- (void)pause;
停止
- (void)stop;
是否正在播放
@property(readonly,getter=isPlaying)BOOLplaying;
時(shí)長
@property(readonly)NSTimeIntervalduration;
當(dāng)前的播放位置
@propertyNSTimeIntervalcurrentTime;
播放次數(shù)(-1代表無限循環(huán)播放,其他代表播放numberOfLoops+1次)
@propertyNSIntegernumberOfLoops;
音量
@propertyfloatvolume;
是否允許更改速率
@propertyBOOLenableRate;
播放速率(1是正常速率,0.5是一般速率未檩,2是雙倍速率)
@propertyfloatrate;
有多少個(gè)聲道
@property(readonly)NSUIntegernumberOfChannels;
聲道(-1是左聲道矛辕,1是右聲道,0是中間)
@propertyfloatpan;
是否允許測量音量
@property(getter=isMeteringEnabled)BOOLmeteringEnabled;
更新測量值
- (void)updateMeters;
獲得當(dāng)前的平均音量
- (float)averagePowerForChannel:(NSUInteger)channelNumber;