任何人和事都有一個開始認識,逐漸提高,深入理解所灸,全面掌握儿礼,隨心所用的過程。本人初次自學音視頻相關(guān)庆寺,如有不足還請各位大神指正蚊夫,話不多說,進入主題:
該小應(yīng)用可以實現(xiàn)三個播放器同步播放懦尝,并通過調(diào)節(jié)各自的播放速率和音量來呈現(xiàn)你想要的節(jié)奏知纷。是不是有點炫酷哦!
1.搭建界面
2.創(chuàng)建播放器類
JWDPlayerController.h
#import
#import
@interfaceJWDPlayerController :NSObject
@property(nonatomic,readonly,getter=isPlaying)BOOLplaying;
- (void)play;
- (void)stop;
- (void)adjustRate:(CGFloat)rate forPlayerAtIndex:(NSInteger)index;;//控制播放速率
- (void)adjustPan:(CGFloat)pan ;/*調(diào)節(jié)聲道權(quán)重set panning. -1.0 is left, 0.0
is center, 1.0 is right. */
- (void)adjustVolume:(CGFloat)volume forPlayerAtIndex:(NSInteger)index;//音量
@end
JWDPlayerController.m
#import"JWDPlayerController.h"
#import
@interfaceJWDPlayerController()
@property(nonatomic,assign)BOOLplaying;
@property(nonatomic,strong)NSArray*players;
@end
@implementationJWDPlayerController
- (instancetype)init {
self= [superinit];
if(self){
AVAudioPlayer*guitarplayer = [selfplayerWithFileName:@"guitar"];
AVAudioPlayer*bassplayer = [selfplayerWithFileName:@"bass"];
AVAudioPlayer*drumplayer = [selfplayerWithFileName:@"drums"];
_players=@[guitarplayer,drumplayer,bassplayer];
}
return
self;
}
- (AVAudioPlayer*)playerWithFileName:(NSString*)fileName {
NSURL*fileURL = [[NSBundlebundleForClass:[selfclass]]URLForResource:fileNamewithExtension:@"caf"];
NSError*error;
AVAudioPlayer*audioPlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:fileURLerror:&error];
if(audioPlayer) {
audioPlayer.numberOfLoops= -1;//無限循環(huán)播放
audioPlayer.enableRate=YES;//設(shè)置為YES可以控制播放速率
[audioPlayerprepareToPlay];
returnaudioPlayer;
}else{
NSLog(@"創(chuàng)建播放器出錯error: %@",[errorlocalizedDescription]);
return nil;
}
}
/**
播放
播放要對三個播放器同步陵霉,獲取當前設(shè)備的時間琅轧,加一個小延時,然后遍歷播放器數(shù)組里面的播放器踊挠,通過[player playAtTime:delayTime];設(shè)置起始播放時間乍桂,這樣三個播放器就能精密的同步播放了。
*/
- (void)play {
if(!self.playing) {
NSTimeIntervaldelayTime = [self.players[0]deviceCurrentTime]+0.01;
for(AVAudioPlayer*playerin self.players){
[playerplayAtTime:delayTime];
}
self.playing=YES;
}
}
/**
停止
如果三個播放器都在播放效床,遍歷去停止播放睹酌,并且player.currentTime = 0.0f;讓播放進度回到音頻文件的原點。
*/
- (void)stop {
if(self.playing) {
for(AVAudioPlayer*playerin self.players){
[playerstop];
player.currentTime=0.0f;
}
self.playing=NO;
}
}
//速率,在不改變音調(diào)的前提下剩檀,改變速率
- (void)adjustRate:(CGFloat)rateforPlayerAtIndex:(NSInteger)index;{
if([selfisValidIndex:index]){
AVAudioPlayer*player =self.players[index];
player.rate= rate;
}
}
/*調(diào)節(jié)聲道權(quán)重set panning. -1.0 is left, 0.0 is center, 1.0
is right. */
- (void)adjustPan:(CGFloat)pan?{
for(AVAudioPlayer*playerin self.players){
player.pan= pan;
}
}
//音量
- (void)adjustVolume:(CGFloat)volumeforPlayerAtIndex:(NSInteger)index {
if([selfisValidIndex:index]){
AVAudioPlayer*player =self.players[index];
player.volume= volume;
}
}
//防止數(shù)組越界
- (BOOL)isValidIndex:(NSUInteger)index{
returnindex ==0|| index
}
@end
在ViewController.m處理相應(yīng)的事件
#import"ViewController.h"
#import"JWDPlayerController.h"
@interfaceViewController()
@property(nonatomic,strong)JWDPlayerController*playerController;//!< <#value#>
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.playerController= [[JWDPlayerControlleralloc]init];
}
//播放
- (IBAction)paly {
NSLog(@"播放");
[self.playerControllerplay];
}
//暫停
- (IBAction)stop {
NSLog(@"暫停");
[self.playerControllerstop];
}
//速率
- (IBAction)changeRate:(UISlider*)sender {
NSLog(@"改變速率-sender %f -- tag %ld",sender.value,(long)sender.tag);
[self.playerControlleradjustRate:sender.valueforPlayerAtIndex:sender.tag];
}
//音量
- (IBAction)changeVolume:(UISlider*)sender {
NSLog(@"改變音量-sender %f -- tag %ld",sender.value,(long)sender.tag);
[self.playerControlleradjustVolume:sender.valueforPlayerAtIndex:sender.tag];
}
//聲道權(quán)衡
- (IBAction)pan:(UISlider*)sender {
NSLog(@"改變聲道比重-sender %f -- tag %ld",sender.value,(long)sender.tag);
[self.playerControlleradjustPan:sender.value];
}
@end
********************************************華麗的分割線*******************************************
截止以上邏輯憋沿,就可以實現(xiàn)多個播放器同時播放,控制不同的音量沪猴、速率辐啄、聲道等功能。但是如果作為一個專門播放音頻類的應(yīng)用运嗜,以上還是不夠的壶辜,還需要進行一下的配置。
配置音頻會話
測試一
在設(shè)備上運行程序担租,當播放時砸民,切換“鈴聲/靜音”開關(guān),會有兩種狀態(tài)的切換翩活。
測試二
在播放的同時按下Lock按鈕阱洪,會有聲音逐漸消失的現(xiàn)象。
作為以播放音頻為核心功能的應(yīng)用菠镇,不能允許以上情況出現(xiàn)。
解決
解決問題一方法:
在AppDelegate中
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
AVAudioSession*session = [AVAudioSessionsharedInstance];
NSError*error;
//指定音頻會話分類
BOOLisSessionCategory = [sessionsetCategory:AVAudioSessionCategoryPlaybackerror:&error];
if(!isSessionCategory) {
NSLog(@"AVAudioSession
setCategory:error: %@",[errorlocalizedDescription]);
}
//設(shè)置為yes激活會話
BOOLisSessionActive = [sessionsetActive:YESerror:&error];
if(!isSessionActive) {
NSLog(@"AVAudioSession
setActive:error: %@",[errorlocalizedDescription]);
}
return YES;
}
注:
在獲得一個AVAudioSession類的實例后承璃,你就能通過調(diào)用音頻會話對象的setCategory:error:實例方法利耍,來從IOS應(yīng)用可用的不同類別中作出選擇。下面列出了可供使用的音頻會話類別:
AVAudioSessionCategorySoloAmbient
這個類別非常像AVAudioSessionCategoryAmbient類別,除了會停止其他程序的音頻回放隘梨。當設(shè)備被設(shè)置為靜音模式程癌,你的音頻回放將會停止。
AVAudioSessionCategoryRecord
這會停止其他應(yīng)用的聲音并讓你的應(yīng)用也不能初始化音頻回放(比如AVAudioPlayer)轴猎。在這種模式下嵌莉,你只能進行錄音。使用這個類別捻脖,調(diào)用AVAudioPlayer的prepareToPlay會返回YES锐峭,但是調(diào)用play方法將返回NO。主UI界面會照常工作可婶。這時沿癞,即使你的設(shè)備屏幕被用戶鎖定了,應(yīng)用的錄音仍會繼續(xù)矛渴。
AVAudioSessionCategoryPlayback
這個類別會靜止其他應(yīng)用的音頻回放椎扬。你可以使用AVAudioPlayer的prepareToPlay和play方法,在你的應(yīng)用中播放聲音具温。主UI界面會照常工作蚕涤。這時,即使屏幕被鎖定或者設(shè)備為靜音模式铣猩,音頻回放都會繼續(xù)钻趋。
AVAudioSessionCategoryPlayAndRecord
這個類別允許你的應(yīng)用中同時進行聲音的播放和錄制。當你的聲音錄制或播放開始后剂习,其他應(yīng)用的聲音播放將會停止蛮位。主UI界面會照常工作。這時鳞绕,即使屏幕被鎖定或者設(shè)備為靜音模式失仁,音頻回放和錄制都會繼續(xù)。
AVAudioSessionCategoryAudioProcessing
這個類別用于應(yīng)用中進行音頻處理的情形们何,而不是音頻回放或錄制萄焦。設(shè)置了這種模式,你在應(yīng)用中就不能播放和錄制任何聲音冤竹。調(diào)用AVAPlayer的prepareToPlay和play方法都將返回NO拂封。其他應(yīng)用的音頻回放,比如iPod鹦蠕,也會在此模式下停止冒签。
AVAudioSessionCategoryAmbient
這個類別不會停止其他應(yīng)用的聲音,相反钟病,它允許你的音頻播放于其他應(yīng)用的聲音之上萧恕,比如iPod刚梭。你的應(yīng)用的主UI線程會工作正常。調(diào)用AVAPlayer的prepareToPlay和play方法都將返回YES票唆。當用戶鎖屏時朴读,你的應(yīng)用將停止所有正在回放的音頻。僅當你的應(yīng)用是唯一播放該音頻文件的應(yīng)用時走趋,靜音模式將停止你程序的音頻回放衅金。如果正當iPod播放一手歌時,你開始播放音頻簿煌,將設(shè)備設(shè)為靜音模式并不能停止你的音頻回放氮唯。
解決問題二方法:
設(shè)置info.plist
也可以這樣設(shè)置
UIBackgroundModes
audio
添加完以后再次運行,按下lock鍵啦吧,依然會聽到音樂播放您觉。感覺像練成了降龍十八掌,太棒啦授滓!
********************************************華麗的分割線*******************************************
處理中斷事件
當我們正在播放音頻時琳水,如果突然來電話,那么我們播放的音樂會停止般堆,當電話結(jié)束時在孝,播放的音樂不會再次自動播放,那么就必須處理中斷事件淮摔。
在出現(xiàn)中斷之前私沮,需要知道 中斷事件的通知,注冊通知
- (instancetype)init {
self= [superinit];
if(self){
AVAudioPlayer*guitarplayer = [selfplayerWithFileName:@"guitar"];
AVAudioPlayer*bassplayer = [selfplayerWithFileName:@"bass"];
AVAudioPlayer*drumplayer = [selfplayerWithFileName:@"drums"];
_players=@[guitarplayer,drumplayer,bassplayer];
//注冊中斷事件的通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleInterrRuption:)name:AVAudioSessionInterruptionNotificationobject:[AVAudioSessionsharedInstance]];
}
return
self;
}
- (void)handleInterrRuption:(NSNotification*)notification {
NSDictionary*info =
notification.userInfo;
AVAudioSessionInterruptionTypetype = [info[AVAudioSessionInterruptionTypeKey]unsignedIntegerValue];
if(type ==AVAudioSessionInterruptionTypeBegan) {//開始中斷
[selfstop];
}else{//中斷結(jié)束
AVAudioSessionInterruptionOptionsoptions = [info[AVAudioSessionInterruptionOptionKey]unsignedIntegerValue];
if(options ==AVAudioSessionInterruptionOptionShouldResume) {
[selfplay];
}
}
}
********************************************華麗的分割線*******************************************
截止以上代碼和橙,還是有小瑕疵仔燕,當插入耳機或者外界音頻輸出設(shè)備時,會在外界設(shè)備上播放魔招,當斷開外界設(shè)備時晰搀,音頻播放有回到手機內(nèi)置揚聲器播放。根據(jù)蘋果隱私問題办斑,當插入耳機播放后外恕,表示用戶不愿意讓別人聽到在播放什么,所以當拔下耳機的時候乡翅,需要停止播放鳞疲,保護用戶的隱私。
在JWDPlayerController.m 中 祖冊通知
- (instancetype)init {
self= [superinit];
if(self){
AVAudioPlayer*guitarplayer = [selfplayerWithFileName:@"guitar"];
AVAudioPlayer*bassplayer = [selfplayerWithFileName:@"bass"];
AVAudioPlayer*drumplayer = [selfplayerWithFileName:@"drums"];
_players=@[guitarplayer,drumplayer,bassplayer];
//注冊中斷事件的通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleInterruption:)name:AVAudioSessionInterruptionNotificationobject:[AVAudioSessionsharedInstance]];
//注冊保護用戶隱私的通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleRouteChange:)name:AVAudioSessionRouteChangeNotificationobject:[AVAudioSessionsharedInstance]];
}
return
self;
}
- (void)handleRouteChange:(NSNotification*)notification {
NSDictionary*info =
notification.userInfo;
NSLog(@"info--%@",info);
AVAudioSessionRouteChangeReasonreason = [info[AVAudioSessionRouteChangeReasonKey]unsignedIntegerValue];
if(reason ==AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
AVAudioSessionRouteDescription*description = info[AVAudioSessionRouteChangePreviousRouteKey];
AVAudioSessionPortDescription*portDescription = description.outputs[0];
NSString*portType = portDescription.portType;
if([portTypeisEqualToString:AVAudioSessionPortHeadphones]){
[selfstop];
}
}
}
在接到通知之后蠕蚜,判斷是否有線路發(fā)送變化尚洽。
if(reason ==AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {}
知道有設(shè)備斷開后,需要向userinfo字典提出請求波势,獲取前一個線路的AVAudioSessionRouteDescription
線路的描述 保存在一個數(shù)組中翎朱,元素為AVAudioSessionPortDescription用于描述不同接口的I/O接口屬性橄维。需要從線路描述中找到第一個輸出接口并判斷是否為耳機尺铣。然后入停止播放拴曲。
好了。截止現(xiàn)在凛忿,以音頻播放為核心功能的應(yīng)用澈灼,應(yīng)該做出的基本問題完成。
千山過后盡開顏店溢,萬里長征第一步叁熔。本人以前接觸的音視頻相關(guān)方面較少,現(xiàn)想系統(tǒng)學習床牧,由于是自學荣回,可能有不足之處,如你發(fā)現(xiàn)戈咳,還請不吝賜教心软。謝謝!
符demo地址:https://github.com/weidongjiang/AVFoundation-AudioPlay-Group.git
如果幫助你解決了你的問題著蛙,或者你覺得還可以删铃,那就給小弟一個star,一起共同學習踏堡。謝謝猎唁!