//
//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;
//当前播放歌曲的序号
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];
//设置状态栏颜色
//在info.plist文件中加入字段View controller-based status bar appearance=NO
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
_index=0;
[selfloadMusicData];
[selfcreateBGImageView];//1. 1和2的顺序不能对调
[selfcreateHeadView];//2.
[selfcreateMusicControlView];
[selfchangeMusic:_musicData[_index]];
returnYES;
}
#pragma mark ---创建视图界面
//创建背景图片视图
-(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];
}
//创建上边栏视图
-(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];
}
//创建底部视图
-(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];
//进度条
_timeSlider= [[UISlideralloc]initWithFrame:CGRectMake(-12, -10,kScreenWidth+23,20)];
//设置颜色和滑块的样式
_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{
//改变当前播放时间,即左边的label内的值
_timeSlider.value+=0.1;
_leftTimeLabel.text= [selftimeStrongWithIntegerValue:(NSInteger)(_timeSlider.value)];
}
#pragma mark ---播放控制
//开始播放
-(void)startMusic{
//播放按钮状态设置为正在播放状态
_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:@"多远都要在一起"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{
//播放按钮设置为暂停状态
_playButton.selected=NO;
[_playerstop];
//关闭定时器
[_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{
//获取下一首歌曲的数据
_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];
//改变进度条两边的时间
_leftTimeLabel.text=@"00:00";
_rightTimeLabel.text= [selftimeStrongWithIntegerValue:song.length];
[selfplayMusicAction];
//改变收藏按钮的状态
_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;
//设置当前歌曲的like状态
Song*song =_musicData[_index];
song.like= button.selected;
}
//封面按钮点击事件的控制
-(void)backgroundButtonAction:(UIButton*)button{
button.enabled=NO;
//判断当前界面是否是隐藏状态
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) {
//动画播放结束后,执行此block
button.enabled=YES;
}];
}
#pragma mark ---其他方法
//根据传入的秒数,计算时间
-(NSString*)timeStrongWithIntegerValue:(NSInteger)value{
if(value <0) {
return@"00:00";
}
//设置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"];
//读取到数组
NSArray*array = [NSArrayarrayWithContentsOfFile:filePath];
//NSLog(@"%@",array);
_musicData= [[NSMutableArrayalloc]init];
//处理数据,包装成song对象
for(NSDictionary*dicinarray) {
Song*s = [[Songalloc]initWithDic:dic];
[_musicDataaddObject:s];
}
}
@end
网友评论