QQ音乐(粗糙版)

//

//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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,198评论 6 514
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,334评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,643评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,495评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,502评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,156评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,743评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,659评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,200评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,282评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,424评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,107评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,789评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,264评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,390评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,798评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,435评论 2 359

推荐阅读更多精彩内容

  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,671评论 2 7
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 818评论 0 0
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,435评论 9 5
  • 今天的学习的是Oc中控件的学习,先是对控件方法的了解,然后是在代码板上写代码,实现在界面上图片的转换,还有是对按钮...
    叶家的大树苗阅读 256评论 0 0
  • 我不知道什麽是诗意 只是喜欢李白喜欢他的诗 可我没有他的文才 更不能像他那样寄情山水 我想让自己富有诗意 阳光暖...
    啊泱阅读 229评论 0 0