iOS開發拓展篇—音頻處理(音樂播放器5)windows
實現效果:app
1、半透明滑塊的設置ide
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.控制滑塊和進度條的frame 12 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 }
裁剪圓角的細節處理:oop
2、播放或暫停、上一首、下一首的實現性能
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 }else 47 { 48 self.playOrPauseButton.selected=YES; 49 //繼續播放 50 [YYAudioTool playMusic:self.playingMusic.filename]; 51 //開啓定時器 52 [self addCurrentTimeTimer]; 53 } 54 }
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 }
4、補充動畫
完整的代碼以下:atom
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)starPlayingMusic 107 { 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)time 141 { 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)addCurrentTimeTimer 152 { 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)removeCurrentTime 171 { 172 [self.CurrentTimeTimer invalidate]; 173 174 //把定時器清空 175 self.CurrentTimeTimer=nil; 176 } 177 178 /** 179 * 更新播放進度 180 */ 181 -(void)updateCurrentTime 182 { 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.動畫隱藏View 209 [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.控制滑塊和進度條的frame 240 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 }else 332 { 333 self.playOrPauseButton.selected=YES; 334 //繼續播放 335 [YYAudioTool playMusic:self.playingMusic.filename]; 336 //開啓定時器 337 [self addCurrentTimeTimer]; 338 } 339 } 340 341 342 @end