一、背景音樂播放 循環播放長音樂 支持mp3格式
html
#import <AVFoundation/AVFoundation.h>;app
1 NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"]; //建立音樂文件路徑 2 NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath]; 3 4 AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil]; 5 6 //建立播放器 7 self.myBackMusic = thePlayer; //賦值給本身定義的類變量 8 9 [musicURL release]; 10 [thePlayer release]; 11 12 [myBackMusic prepareToPlay]; 13 [myBackMusic setVolume:1]; //設置音量大小 14 myBackMusic.numberOfLoops = -1;//設置音樂播放次數 -1爲一直循環 15 [myBackMusic play]; //播放
二、音效播放 短聲音框架
#import <AudioToolbox/AudioToolbox.h> oop
NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"Clapping Crowd Studio 01" ofType:@"caf"]; //建立音樂文件路徑 CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath:thesoundFilePath]; AudioServicesCreateSystemSoundID(thesoundURL, &sameViewSoundID); //變量SoundID與URL對應 AudioServicesPlaySystemSound(sameViewSoundID); //播放SoundID聲音
使用AudioToolbox framework。這個框架能夠將比較短的聲音註冊到 system sound服務上。被註冊到system sound服務上的聲音稱之爲 system sounds。它必須知足下面幾個條件。url
一、 播放的時間不能超過30秒spa
二、數據必須是 PCM或者IMA4流格式code
三、必須被打包成下面三個格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff)orm
聲音文件必須放到設備的本地文件夾下面。經過AudioServicesCreateSystemSoundID方法註冊這個聲音文件,AudioServicesCreateSystemSoundID須要聲音文件的url的CFURLRef對象。htm
另詳見: http://www.cnblogs.com/zhuqil/archive/2011/07/23/2115021.html對象