最近業餘時間整理的一個ios播放器,界面採用storyboard,之前幾乎都是用代碼佈局,可是用過一個項目storyboard後,就感受storyboard很靠譜,團隊合做版本控制的問題解決其實很簡單,每一個人一個storyboard就能夠了。播放代碼使用的是avplayer,最大的優化就是播放界面和播放代碼徹底分離。ios
-(void)createPlayer { self.playBackView=[[AVPlayerDemoPlaybackView alloc] initWithFrame:viewBounds]; [self.view addSubview:self.playBackView]; self.playerManager=[[AVPlayerManager alloc] init]; //設置url //self.playerManager.URL=[[NSBundle mainBundle] URLForResource:@"1" withExtension:@"mp4"]; //self.playerManager.URL=[[NSBundle mainBundle] URLForResource:@"2" withExtension:@"mov"]; self.playerManager.URL=[NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"]; //設置播放界面 self.playerManager.mPlaybackView=self.playBackView; //設置時間軸 self.playerManager.scrubSlider=self.scrubSlider; //播放出錯回調 self.playerManager.failedBlock=^(NSError *error){ if (error) { UIAlertView *alert=[[UIAlertView alloc] initWithTitle:[error description] message:[error debugDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } }; __weak ViewController *weakself=self; //播放狀態改變 self.playerManager.statusChangeBlock=^(BOOL isPlaying){ weakself.playBtn.hidden=isPlaying; weakself.pauseBtn.hidden=!isPlaying; }; //播放結束 self.playerManager.playEndBlock=^(){ NSLog(@"播放結束"); }; //準備好播放 self.playerManager.readyToPlayBlock=^(){ weakself.totalTimeLabel.text=[weakself convertTimetoString:weakself.playerManager.duration]; weakself.playBtn.enabled=YES; weakself.pauseBtn.enabled=YES; [weakself.playerManager play]; }; //播放過程時間改變 self.playerManager.timeChangedBlock=^(double time) { weakself.currentTimeLabel.text=[weakself convertTimetoString:time]; }; //聲音發生改變 self.playerManager.volumeChangedBlock=^(){ weakself.volumeSlider.value=weakself.playerManager.volume; }; //緩衝時間 self.playerManager.bufferChangedBlock=^(double buffered){ weakself.processBar.progress=buffered/weakself.playerManager.duration; weakself.speedLabel.text=[NSString stringWithFormat:@"%0.0fkb/s",weakself.playerManager.bufferSpeed]; }; //初始化完畢,準備播放 [self.playerManager prepare]; }
代碼下載:http://pan.baidu.com/s/1eyVMmide