//
//AppDelegate.m
//QQ播放器
#import"AppDelegate.h"
#import"Song.h"
#import
//屏幕大小宏定義
#define kScreenWidth[UIScreen mainScreen].bounds.size.width
#define kScreenHeight[UIScreen mainScreen].bounds.size.height
@interfaceAppDelegate ()
{
AVAudioPlayer *_player;
NSURL *url;
NSMutableArray *_musicData;
//頭部視圖
UIView *_headBackgroundView;
UILabel *_songNameLabel;
UILabel *_singerNameLabel;
UIButton *_likeButton;
//尾部視圖
UIView *_musicControlBackGroundView;
UIButton *_playButton;
UILabel *_leftTimeLabel;
UILabel *_rightTimeLabel;
UISlider *_timeSlider;
//歌曲背景
UIButton *_backGroundImgaeButton;
//定時器
NSTimer *_timer;
//當(dāng)前播放歌曲的序號
NSInteger_index;
Song*_currentSong;
}
@end
@implementationAppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//建立一個窗口
self.window= [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
self.window.backgroundColor=[UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
//建立一個根控制器
self.window.rootViewController= [UIViewControlleralloc];
//設(shè)置狀態(tài)欄顏色
//在info.plist文件中加入字段View controller-based status bar appearance=NO
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
_index=0;
[selfloadMusicData];
[selfcreateBGImageView];//1. 1和2的順序不能對調(diào)
[selfcreateHeadView];//2.
[selfcreateMusicControlView];
[selfchangeMusic:_musicData[_index]];
returnYES;
}
#pragma mark ---創(chuàng)建視圖界面
//創(chuàng)建背景圖片視圖
-(void)createBGImageView{
_backGroundImgaeButton= [UIButtonbuttonWithType:UIButtonTypeCustom];
_backGroundImgaeButton.frame=CGRectMake(0,0,kScreenWidth,kScreenHeight);
[_backGroundImgaeButtonsetBackgroundImage:[UIImageimageNamed:@"joy.jpg"]forState:UIControlStateNormal];
[_backGroundImgaeButtonsetBackgroundImage:[UIImageimageNamed:@"joy.jpg"]forState:UIControlStateHighlighted];
[_backGroundImgaeButtonsetBackgroundImage:[UIImageimageNamed:@"joy.jpg"]forState:UIControlStateDisabled];
[_backGroundImgaeButtonaddTarget:selfaction:@selector(backgroundButtonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:_backGroundImgaeButton];
}
//創(chuàng)建上邊欄視圖
-(void)createHeadView{
//背景
_headBackgroundView=[[UIViewalloc]initWithFrame:CGRectMake(0,0,kScreenWidth,64)];
_headBackgroundView.backgroundColor= [UIColorcolorWithWhite:0alpha:0.7];
//_headBackgroundView.alpha = 0.5;
[self.windowaddSubview:_headBackgroundView];
//歌曲名
_songNameLabel=[[UILabelalloc]initWithFrame:CGRectMake((kScreenWidth-250)/2,20,250,24)];
//_songNameLabel.backgroundColor = [UIColor redColor];
_songNameLabel.textColor= [UIColorwhiteColor];
_songNameLabel.font= [UIFontboldSystemFontOfSize:20];
_songNameLabel.text=@"七里香";
_songNameLabel.textAlignment=NSTextAlignmentCenter;
[_headBackgroundViewaddSubview:_songNameLabel];
//歌手
_singerNameLabel=[[UILabelalloc]initWithFrame:CGRectMake((kScreenWidth-250)/2,44,250,20)];
//_singerNameLabel.backgroundColor = [UIColor redColor];
_singerNameLabel.textColor= [UIColorwhiteColor];
_singerNameLabel.font= [UIFontsystemFontOfSize:18];
_singerNameLabel.text=@"周杰倫";
_singerNameLabel.textAlignment=NSTextAlignmentCenter;
[_headBackgroundViewaddSubview:_singerNameLabel];
//收藏
_likeButton= [UIButtonbuttonWithType:UIButtonTypeCustom];
_likeButton.frame=CGRectMake(kScreenWidth-45,22,40,40);
[_likeButtonsetImage:[UIImageimageNamed:@"playing_btn_love_disable@2x.png"]forState:UIControlStateNormal];
[_likeButtonsetImage:[UIImageimageNamed:@"playing_btn_in_myfavor_h@2x.png"]forState:UIControlStateHighlighted];
[_likeButtonsetImage:[UIImageimageNamed:@"playing_btn_in_myfavor@2x"]forState:UIControlStateSelected];
[_likeButtonaddTarget:selfaction:@selector(likeButtonAction:)forControlEvents:UIControlEventTouchUpInside];
[_headBackgroundViewaddSubview:_likeButton];
}
//創(chuàng)建底部視圖
-(void)createMusicControlView{
//背景
_musicControlBackGroundView= [[UIViewalloc]initWithFrame:CGRectMake(0,kScreenHeight-100,kScreenWidth,100)];
_musicControlBackGroundView.backgroundColor= [UIColorcolorWithWhite:0alpha:0.7];
[self.windowaddSubview:_musicControlBackGroundView];
//播放按鈕
_playButton= [UIButtonbuttonWithType:UIButtonTypeCustom];
_playButton.frame=CGRectMake(0,0,65,65);
_playButton.center=CGPointMake(kScreenWidth/2,50);
[_playButtonsetImage:[UIImageimageNamed:@"playing_btn_play_n@2x.png"]forState:UIControlStateNormal];
[_playButtonsetImage:[UIImageimageNamed:@"playing_btn_pause_n@2x.png"]forState:UIControlStateSelected];
[_playButtonaddTarget:selfaction:@selector(playButtonAction:)forControlEvents:UIControlEventTouchUpInside];
[_musicControlBackGroundViewaddSubview:_playButton];
//左邊的時間
_leftTimeLabel= [[UILabelalloc]initWithFrame:CGRectMake(0,0,40,15)];
_leftTimeLabel.font= [UIFontsystemFontOfSize:13];
_leftTimeLabel.textColor=[UIColorwhiteColor];
_leftTimeLabel.text=@"11:11";
_leftTimeLabel.textAlignment=NSTextAlignmentLeft;
[_musicControlBackGroundViewaddSubview:_leftTimeLabel];
//右邊時間,即總時間
_rightTimeLabel= [[UILabelalloc]initWithFrame:CGRectMake(kScreenWidth-40,0,40,15)];
_rightTimeLabel.font= [UIFontsystemFontOfSize:13];
_rightTimeLabel.textColor=[UIColorwhiteColor];
_rightTimeLabel.text=@"11:11";
_rightTimeLabel.textAlignment=NSTextAlignmentRight;
[_musicControlBackGroundViewaddSubview:_rightTimeLabel];
//上一首
UIButton*prev = [UIButtonbuttonWithType:UIButtonTypeCustom];
prev.frame=CGRectMake(0,0,40,40);
prev.center=CGPointMake(kScreenWidth*0.3,55);
[prevsetImage:[UIImageimageNamed:@"playing_btn_pre_n@2x.png"]forState:UIControlStateNormal];
[prevaddTarget:selfaction:@selector(prevButtonAction:)forControlEvents:UIControlEventTouchUpInside];
[_musicControlBackGroundViewaddSubview:prev];
//下一首
UIButton*next = [UIButtonbuttonWithType:UIButtonTypeCustom];
next.frame=CGRectMake(0,0,40,40);
next.center=CGPointMake(kScreenWidth*0.7,55);
[nextsetImage:[UIImageimageNamed:@"playing_btn_next_n@2x.png"]forState:UIControlStateNormal];
[nextaddTarget:selfaction:@selector(nextButtonAction:)forControlEvents:UIControlEventTouchUpInside];
[_musicControlBackGroundViewaddSubview:next];
//進(jìn)度條
_timeSlider= [[UISlideralloc]initWithFrame:CGRectMake(-12, -10,kScreenWidth+23,20)];
//設(shè)置顏色和滑塊的樣式
_timeSlider.minimumTrackTintColor= [UIColorcolorWithRed:43/255.0green:187/255.0blue:89/255.0alpha:1];
[_timeSlidersetThumbImage:[UIImageimageNamed:@"playing_slider_thumb@2x"]forState:UIControlStateNormal];
_timeSlider.minimumValue=0;
_timeSlider.maximumValue=30;
//給slider添加一個事件
[_timeSlideraddTarget:nilaction:@selector(timeSliderValueChanged:)forControlEvents:UIControlEventValueChanged];
[_musicControlBackGroundViewaddSubview:_timeSlider];
}
#pragma mark ---定時器方法
-(void)timeChanged{
//改變當(dāng)前播放時間,即左邊的label內(nèi)的值
_timeSlider.value+=0.1;
_leftTimeLabel.text= [selftimeStrongWithIntegerValue:(NSInteger)(_timeSlider.value)];
}
#pragma mark ---播放控制
//開始播放
-(void)startMusic{
//播放按鈕狀態(tài)設(shè)置為正在播放狀態(tài)
_playButton.selected=YES;
//開啟定時器,每個1秒改變slider的值
_timer= [NSTimerscheduledTimerWithTimeInterval:0.1target:selfselector:@selector(timeChanged)userInfo:nilrepeats:YES];
//開始播放
[_playerplay];
}
//歌曲判斷
-(void)playMusicAction{
if(_index==0) {
NSString*filePath = [[NSBundlemainBundle]pathForResource:@"七里香"ofType:@"mp3"];
NSData*songData =[NSDatadataWithContentsOfFile:filePath];
_player= [[AVAudioPlayeralloc]initWithData:songDataerror:nil];
}
if(_index==1) {
NSString*filePath = [[NSBundlemainBundle]pathForResource:@"多遠(yuǎn)都要在一起"ofType:@"mp3"];
NSData*songData =[NSDatadataWithContentsOfFile:filePath];
_player= [[AVAudioPlayeralloc]initWithData:songDataerror:nil];
}
if(_index==2) {
NSString*filePath = [[NSBundlemainBundle]pathForResource:@"你不知道的事"ofType:@"mp3"];
NSData*songData =[NSDatadataWithContentsOfFile:filePath];
_player= [[AVAudioPlayeralloc]initWithData:songDataerror:nil];
}
if(_index==3) {
NSString*filePath = [[NSBundlemainBundle]pathForResource:@"Bad Romance"ofType:@"mp3"];
NSData*songData =[NSDatadataWithContentsOfFile:filePath];
_player= [[AVAudioPlayeralloc]initWithData:songDataerror:nil];
}
if(_index==4) {
NSString*filePath = [[NSBundlemainBundle]pathForResource:@"浮夸"ofType:@"mp3"];
NSData*songData =[NSDatadataWithContentsOfFile:filePath];
_player= [[AVAudioPlayeralloc]initWithData:songDataerror:nil];
}
}
//暫停播放
-(void)pauseMusic{
//播放按鈕設(shè)置為暫停狀態(tài)
_playButton.selected=NO;
[_playerstop];
//關(guān)閉定時器
[_timerinvalidate];
}
//上一首歌
-(void)prevMusic{
_index--;
if(_index<0) {
_index=_musicData.count-1;
}
Song*song =_musicData[_index];
//[self playMusicAction];
[selfchangeMusic:song];
[_timerinvalidate];
_timeSlider.value=0;
[selfstartMusic];
}
//下一首歌
-(void)nextMusic{
//獲取下一首歌曲的數(shù)據(jù)
_index++;
if(_index>=_musicData.count) {
_index=0;
}
Song*song =_musicData[_index];
//更換歌曲
[selfchangeMusic:song];
[_timerinvalidate];
_timeSlider.value=0;
[selfstartMusic];
}
-(void)changeMusic:(Song*)song{
//改變歌曲名,歌手
_songNameLabel.text= song.name;
_singerNameLabel.text= song.singer;
//改變背景圖片
UIImage*bgImage = [UIImageimageNamed:song.image];
[_backGroundImgaeButtonsetBackgroundImage:bgImageforState:UIControlStateNormal];
[_backGroundImgaeButtonsetBackgroundImage:bgImageforState:UIControlStateHighlighted];
[_backGroundImgaeButtonsetBackgroundImage:bgImageforState:UIControlStateDisabled];
//改變進(jìn)度條兩邊的時間
_leftTimeLabel.text=@"00:00";
_rightTimeLabel.text= [selftimeStrongWithIntegerValue:song.length];
[selfplayMusicAction];
//改變收藏按鈕的狀態(tài)
_likeButton.selected= [songislike];
//slider的最大值
_timeSlider.maximumValue=song.length;
}
#pragma mark ---按鈕點擊控制
//上一首按鈕
-(void)prevButtonAction:(UIButton*)button{
NSLog(@"上一首");
//button.selected =! button.selected;
[selfprevMusic];
}
//下一首按鈕
-(void)nextButtonAction:(UIButton*)button{
NSLog(@"下一首");
//button.selected =! button.selected;
[selfnextMusic];
}
//播放按鈕
-(void)playButtonAction:(UIButton*)button{
//NSLog(@"開始播放");
//button.selected =! button.selected;
if(!button.selected) {
[selfstartMusic];
}
else{
[selfpauseMusic];
}
}
//滑塊按鈕
-(void)timeSliderValueChanged:(UISlider*)slider{
NSLog(@"value=%f",slider.value);
_leftTimeLabel.text=[selftimeStrongWithIntegerValue:(NSInteger)(_timeSlider.value)];
}
//收藏按鍵的點擊事件
-(void)likeButtonAction:(UIButton*)button{
button.selected= !button.selected;
//設(shè)置當(dāng)前歌曲的like狀態(tài)
Song*song =_musicData[_index];
song.like= button.selected;
}
//封面按鈕點擊事件的控制
-(void)backgroundButtonAction:(UIButton*)button{
button.enabled=NO;
//判斷當(dāng)前界面是否是隱藏狀態(tài)
BOOLisHidden =_headBackgroundView.frame.origin.y;
//動畫
[UIViewanimateWithDuration:0.5animations:^{
if(isHidden) {
//上邊欄
CGRectframe =_headBackgroundView.frame;
frame.origin.y=0;
_headBackgroundView.frame= frame;
//下邊欄
frame =_musicControlBackGroundView.frame;
frame.origin.y=kScreenHeight-100;
_musicControlBackGroundView.frame= frame;
}
else{
CGRectframe =_headBackgroundView.frame;
frame.origin.y= -64;
_headBackgroundView.frame= frame;
frame =_musicControlBackGroundView.frame;
frame.origin.y=kScreenHeight;
_musicControlBackGroundView.frame= frame;
}
}completion:^(BOOLfinished) {
//動畫播放結(jié)束后,執(zhí)行此block
button.enabled=YES;
}];
}
#pragma mark ---其他方法
//根據(jù)傳入的秒數(shù),計算時間
-(NSString*)timeStrongWithIntegerValue:(NSInteger)value{
if(value <0) {
return@"00:00";
}
//設(shè)置label顯示的時間
NSIntegermin = value/60;
NSIntegersce = value%60;
NSString*timeString = [NSStringstringWithFormat:@"%02li:%02li",min,sce];
returntimeString;
}
//從music.plist文件中讀取歌曲信息
-(void)loadMusicData{
//查找文件路徑
NSString*filePath = [[NSBundlemainBundle]pathForResource:@"music"ofType:@"plist"];
//讀取到數(shù)組
NSArray*array = [NSArrayarrayWithContentsOfFile:filePath];
//NSLog(@"%@",array);
_musicData= [[NSMutableArrayalloc]init];
//處理數(shù)據(jù),包裝成song對象
for(NSDictionary*dicinarray) {
Song*s = [[Songalloc]initWithDic:dic];
[_musicDataaddObject:s];
}
}
@end