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
音樂播放器
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.