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;

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • iOS開發(fā)系列--網(wǎng)絡(luò)開發(fā) 概覽 大部分應(yīng)用程序都或多或少會牽扯到網(wǎng)絡(luò)開發(fā),例如說新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 4,028評論 2 7
  • //設(shè)置尺寸為屏幕尺寸的時候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 965評論 0 0
  • 前言 最近忙完項目比較閑,想寫一篇博客來分享一些自學(xué)iOS的心得體會,希望對迷茫的你有所幫助。博主非科班出身,一些...
    GitHubPorter閱讀 1,574評論 9 5
  • 今天的學(xué)習(xí)的是Oc中控件的學(xué)習(xí),先是對控件方法的了解,然后是在代碼板上寫代碼,實現(xiàn)在界面上圖片的轉(zhuǎn)換,還有是對按鈕...
    葉家的大樹苗閱讀 317評論 0 0
  • 我不知道什麼是詩意 只是喜歡李白喜歡他的詩 可我沒有他的文才 更不能像他那樣寄情山水 我想讓自己富有詩意 陽光暖...
    啊泱閱讀 282評論 0 0

友情鏈接更多精彩內(nèi)容