標籤:
iOS開發拓展篇—音頻處理(音樂播放器5)
實現效果:
一、半透明滑塊的設定
1 /** 2 *拖動滑塊 3 */ 4 - (IBAction)panSlider:(UIPanGestureRecognizer *)sender { 5 6 //1.獲得挪動的距離 7 CGPoint t=[sender translationInView:sender.view]; 8 //把挪動清零 9 [sender setTranslation:CGPointZero inView:sender.view];10 11 //2.控制滑塊和進度條的frame12 CGFloat sliderMaxX=self.view.width-self.slider.width;13 self.slider.x+=t.x;14 //控制滑塊的frame,不讓其越界15 if(self.slider.x<0)16 {17 self.slider.x=0;18 }else if (self.slider.x>sliderMaxX)19 {20 self.slider.x=sliderMaxX;21 }22 //設定進度條的寬度23 self.progressView.width=self.slider.center.x;24 25 //3.設定時間值26 double progress=self.slider.x/sliderMaxX;27 //當前的時間值=音樂的時間長度*當前的進度值28 NSTimeInterval time=self.player.duration*progress;29 [self.slider setTitle:[self strWithTime:time] forState:UIControlStateNormal];30 31 //設定拖拽進度的X的值32 self.currentTimeView.x=self.slider.x;33 [self.currentTimeView setTitle:self.slider.currentTitle forState:UIControlStateNormal];34 35 //4.如果開始拖動,那麼就停止定時器36 if (sender.state==UIGestureRecognizerStateBegan) {37 //停止定時器38 [self removeCurrentTime];39 40 //設定拖拽進度41 //顯示42 self.currentTimeView.hidden=NO;43 self.currentTimeView.y=self.currentTimeView.superview.height-5-self.currentTimeView.height;44 45 }else if(sender.state==UIGestureRecognizerStateEnded)46 {47 //隱藏48 self.currentTimeView.hidden=YES;49 //設定播放器播放的時間50 self.player.currentTime=time;51 #warning 如果現正播放,才需要添加定時器52 // if (self.player.isPlaying) {53 //開啟定時器54 [self addCurrentTimeTimer];55 // }56 }57 }
裁剪圓角的細節處理:
二、播放或暫停、上一首、下一首的實現
1 //上一首 2 - (IBAction)previous { 3 //1.在開始播放之前,禁用一切的app點擊事件 4 UIWindow *window=[[UIApplication sharedApplication].windows lastObject]; 5 window.userInteractionEnabled=NO; 6 7 //2.重設當前歌曲 8 [self resetPlayingMusic]; 9 10 //3.獲得上一首歌曲11 [YYMusicTool setPlayingMusic:[YYMusicTool previousMusic]];12 13 //4.播放上一首歌曲14 [self starPlayingMusic];15 16 //5.回複window的點擊為可用17 window.userInteractionEnabled=YES;18 }19 //下一首20 - (IBAction)next {21 //1.在開始播放之前,禁用一切的app點擊事件22 UIWindow *window=[[UIApplication sharedApplication].windows lastObject];23 window.userInteractionEnabled=NO;24 25 //2.重設當前歌曲26 [self resetPlayingMusic];27 28 //3.獲得下一首歌曲29 [YYMusicTool setPlayingMusic:[YYMusicTool nextMusic]];30 31 //4.播放下一首歌曲32 [self starPlayingMusic];33 34 //5.回複window的點擊為可用35 window.userInteractionEnabled=YES;36 }37 38 //繼續或暫停播放39 - (IBAction)playOrPause {40 if (self.playOrPauseButton.isSelected) {//暫停41 self.playOrPauseButton.selected=NO;42 //暫停播放43 [YYAudioTool pauseMusic:self.playingMusic.filename];44 //停掉定時器45 [self removeCurrentTime];46 }else47 {48 self.playOrPauseButton.selected=YES;49 //繼續播放50 [YYAudioTool playMusic:self.playingMusic.filename];51 //開啟定時器52 [self addCurrentTimeTimer];53 }54 }說明:播放和暫停按鈕的圖片設定在兩種狀態下並不一樣,設定播放按鈕的狀態 三、對存在的bug進行改進拖拽還存在問題(定時器的問題) 更好的方法時在添加定時器的地方進行更細的控制:
1 /** 2 * 添加一個定時器 3 */ 4 -(void)addCurrentTimeTimer 5 { 6 //如果當前沒有在播放,那麼就直接返回 7 if (self.player.isPlaying==NO) return; 8 9 //在添加一個定時器之前,先把以前的定時器移除10 [self removeCurrentTime];11 12 //提前先調用一次進度更新,以保證定時器的工作時及時的13 [self updateCurrentTime];14 15 //建立一個定時器,每一秒鐘調用一次16 self.CurrentTimeTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];17 //把定時器加入到運行時中18 [[NSRunLoop mainRunLoop]addTimer:self.CurrentTimeTimer forMode:NSRunLoopCommonModes];19 }
四、補充
完整的代碼如下:
1 // 2 // YYPlayingViewController.m 3 // 20-音頻處理(音樂播放器1) 4 // 5 // Created by apple on 14-8-13. 6 // Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8 9 #import "YYPlayingViewController.h" 10 #import "YYMusicTool.h" 11 #import "YYMusicModel.h" 12 #import "YYAudioTool.h" 13 14 @interface YYPlayingViewController () 15 //顯示拖拽進度 16 @property (weak, nonatomic) IBOutlet UIButton *currentTimeView; 17 //進度條 18 @property (weak, nonatomic) IBOutlet UIView *progressView; 19 //滑塊 20 @property (weak, nonatomic) IBOutlet UIButton *slider; 21 @property (weak, nonatomic) IBOutlet UIImageView *iconView; 22 @property (weak, nonatomic) IBOutlet UILabel *songLabel; 23 @property (weak, nonatomic) IBOutlet UILabel *singerLabel; 24 //當前播放的音樂的時間長度 25 @property (weak, nonatomic) IBOutlet UILabel *durationLabel; 26 //現正播放的音樂 27 @property(nonatomic,strong)YYMusicModel *playingMusic; 28 //音樂播放器對象 29 @property(nonatomic,strong)AVAudioPlayer *player; 30 //定時器 31 @property(nonatomic,strong)NSTimer *CurrentTimeTimer; 32 - (IBAction)exit; 33 - (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender; 34 - (IBAction)panSlider:(UIPanGestureRecognizer *)sender; 35 - (IBAction)previous; 36 - (IBAction)playOrPause; 37 - (IBAction)next; 38 @property (weak, nonatomic) IBOutlet UIButton *playOrPauseButton; 39 40 @end 41 42 @implementation YYPlayingViewController 43 44 -(void)viewDidLoad 45 { 46 [super viewDidLoad]; 47 48 //裁剪圓角 49 self.currentTimeView.layer.cornerRadius=8; 50 51 } 52 #pragma mark-公用方法 53 -(void)show 54 { 55 //1.禁用整個app的點擊事件 56 UIWindow *window=[UIApplication sharedApplication].keyWindow; 57 window.userInteractionEnabled=NO; 58 59 //2.添加播放介面 60 //設定View的大小為覆蓋整個視窗 61 self.view.frame=window.bounds; 62 //設定view顯示 63 self.view.hidden=NO; 64 //把View添加到視窗上 65 [window addSubview:self.view]; 66 67 //3.檢測是否換了歌曲 68 if (self.playingMusic!=[YYMusicTool playingMusic]) { 69 [self resetPlayingMusic]; 70 } 71 72 //4.使用動畫讓View顯示 73 self.view.y=self.view.height; 74 [UIView animateWithDuration:0.25 animations:^{ 75 self.view.y=0; 76 } completion:^(BOOL finished) { 77 78 //設定音樂資料 79 [self starPlayingMusic]; 80 window.userInteractionEnabled=YES; 81 }]; 82 } 83 84 85 #pragma mark-私人方法 86 //重設現正播放的音樂 87 -(void)resetPlayingMusic 88 { 89 //1.重設介面資料 90 self.iconView.image=[UIImage imageNamed:@"play_cover_pic_bg"]; 91 self.songLabel.text=nil; 92 self.singerLabel.text=nil; 93 94 //2.停止播放 95 [YYAudioTool stopMusic:self.playingMusic.filename]; 96 //把播放器進行清空 97 self.player=nil; 98 99 //3.停止定時器100 [self removeCurrentTime];101 102 //4.設定音樂播放按鈕的狀態103 self.playOrPauseButton.selected=NO;104 }105 //開始播放音樂資料106 -(void)starPlayingMusic107 {108 //1.設定介面資料109 110 //如果當前播放的音樂就是傳入的音樂,那麼就直接返回111 if (self.playingMusic==[YYMusicTool playingMusic])112 {113 //把定時器加進去114 [self addCurrentTimeTimer];115 return;116 }117 //存取音樂118 self.playingMusic=[YYMusicTool playingMusic];119 self.iconView.image=[UIImage imageNamed:self.playingMusic.icon];120 self.songLabel.text=self.playingMusic.name;121 self.singerLabel.text=self.playingMusic.singer;122 123 //2.開始播放124 self.player = [YYAudioTool playMusic:self.playingMusic.filename];125 126 //3.設定時間長度127 //self.player.duration; 播放器現正播放的音樂檔案的時間長度128 self.durationLabel.text=[self strWithTime:self.player.duration];129 130 //4.添加定時器131 [self addCurrentTimeTimer];132 133 //5.設定音樂播放按鈕的狀態134 self.playOrPauseButton.selected=YES;135 }136 137 /**138 *把時間長度-->時間字串139 */140 -(NSString *)strWithTime:(NSTimeInterval)time141 {142 int minute=time / 60;143 int second=(int)time % 60;144 return [NSString stringWithFormat:@"%d:%d",minute,second];145 }146 147 #pragma mark-定時器控制148 /**149 * 添加一個定時器150 */151 -(void)addCurrentTimeTimer152 {153 //如果當前沒有在播放,那麼就直接返回154 if (self.player.isPlaying==NO) return;155 156 //在添加一個定時器之前,先把以前的定時器移除157 [self removeCurrentTime];158 159 //提前先調用一次進度更新,以保證定時器的工作時及時的160 [self updateCurrentTime];161 162 //建立一個定時器,每一秒鐘調用一次163 self.CurrentTimeTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];164 //把定時器加入到運行時中165 [[NSRunLoop mainRunLoop]addTimer:self.CurrentTimeTimer forMode:NSRunLoopCommonModes];166 }167 /**168 *移除一個定時器169 */170 -(void)removeCurrentTime171 {172 [self.CurrentTimeTimer invalidate];173 174 //把定時器清空175 self.CurrentTimeTimer=nil;176 }177 178 /**179 * 更新播放進度180 */181 -(void)updateCurrentTime182 {183 //1.計算進度值184 double progress=self.player.currentTime/self.player.duration;185 186 //2.計算滑塊的x值187 // 滑塊的最大的x值188 CGFloat sliderMaxX=self.view.width-self.slider.width;189 self.slider.x=sliderMaxX*progress;190 //設定滑塊上的當前播放時間191 [self.slider setTitle:[self strWithTime:self.player.currentTime] forState:UIControlStateNormal];192 193 //3.設定進度條的寬度194 self.progressView.width=self.slider.center.x;195 196 }197 198 #pragma mark-內部的按鈕監聽方法199 //返回按鈕200 - (IBAction)exit {201 202 //0.移除定時器203 [self removeCurrentTime];204 //1.禁用整個app的點擊事件205 UIWindow *window=[UIApplication sharedApplication].keyWindow;206 window.userInteractionEnabled=NO;207 208 //2.動畫隱藏View209 [UIView animateWithDuration:0.25 animations:^{210 self.view.y=window.height;211 } completion:^(BOOL finished) {212 window.userInteractionEnabled=YES;213 //設定view隱藏能夠節省一些效能214 self.view.hidden=YES;215 }];216 }217 218 /**219 *點擊了進度條220 */221 - (IBAction)tapProgressBg:(UITapGestureRecognizer *)sender {222 //擷取當前單擊的點223 CGPoint point=[sender locationInView:sender.view];224 //切換歌曲的當前播放時間225 self.player.currentTime=(point.x/sender.view.width)*self.player.duration;226 //更新播放進度227 [self updateCurrentTime];228 }229 /**230 *拖動滑塊231 */232 - (IBAction)panSlider:(UIPanGestureRecognizer *)sender {233 234 //1.獲得挪動的距離235 CGPoint t=[sender translationInView:sender.view];236 //把挪動清零237 [sender setTranslation:CGPointZero inView:sender.view];238 239 //2.控制滑塊和進度條的frame240 CGFloat sliderMaxX=self.view.width-self.slider.width;241 self.slider.x+=t.x;242 //控制滑塊的frame,不讓其越界243 if(self.slider.x<0)244 {245 self.slider.x=0;246 }else if (self.slider.x>sliderMaxX)247 {248 self.slider.x=sliderMaxX;249 }250 //設定進度條的寬度251 self.progressView.width=self.slider.center.x;252 253 //3.設定時間值254 double progress=self.slider.x/sliderMaxX;255 //當前的時間值=音樂的時間長度*當前的進度值256 NSTimeInterval time=self.player.duration*progress;257 [self.slider setTitle:[self strWithTime:time] forState:UIControlStateNormal];258 259 //設定拖拽進度的X的值260 self.currentTimeView.x=self.slider.x;261 [self.currentTimeView setTitle:self.slider.currentTitle forState:UIControlStateNormal];262 263 //4.如果開始拖動,那麼就停止定時器264 if (sender.state==UIGestureRecognizerStateBegan) {265 //停止定時器266 [self removeCurrentTime];267 268 //設定拖拽進度269 //顯示270 self.currentTimeView.hidden=NO;271 self.currentTimeView.y=self.currentTimeView.superview.height-5-self.currentTimeView.height;272 273 }else if(sender.state==UIGestureRecognizerStateEnded)274 {275 //隱藏276 self.currentTimeView.hidden=YES;277 //設定播放器播放的時間278 self.player.currentTime=time;279 #warning 如果現正播放,才需要添加定時器280 // if (self.player.isPlaying) {281 //開啟定時器282 [self addCurrentTimeTimer];283 // }284 }285 }286 287 //上一首288 - (IBAction)previous {289 //1.在開始播放之前,禁用一切的app點擊事件290 UIWindow *window=[[UIApplication sharedApplication].windows lastObject];291 window.userInteractionEnabled=NO;292 293 //2.重設當前歌曲294 [self resetPlayingMusic];295 296 //3.獲得上一首歌曲297 [YYMusicTool setPlayingMusic:[YYMusicTool previousMusic]];298 299 //4.播放上一首歌曲300 [self starPlayingMusic];301 302 //5.回複window的點擊為可用303 window.userInteractionEnabled=YES;304 }305 //下一首306 - (IBAction)next {307 //1.在開始播放之前,禁用一切的app點擊事件308 UIWindow *window=[[UIApplication sharedApplication].windows lastObject];309 window.userInteractionEnabled=NO;310 311 //2.重設當前歌曲312 [self resetPlayingMusic];313 314 //3.獲得下一首歌曲315 [YYMusicTool setPlayingMusic:[YYMusicTool nextMusic]];316 317 //4.播放下一首歌曲318 [self starPlayingMusic];319 320 //5.回複window的點擊為可用321 window.userInteractionEnabled=YES;322 }323 //繼續或暫停播放324 - (IBAction)playOrPause {325 if (self.playOrPauseButton.isSelected) {//暫停326 self.playOrPauseButton.selected=NO;327 //暫停播放328 [YYAudioTool pauseMusic:self.playingMusic.filename];329 //停掉定時器330 [self removeCurrentTime];331 }else332 {333 self.playOrPauseButton.selected=YES;334 //繼續播放335 [YYAudioTool playMusic:self.playingMusic.filename];336 //開啟定時器337 [self addCurrentTimeTimer];338 }339 }340 341 342 @end
iOS開發拓展篇—音頻處理(音樂播放器5)