音乐

//// Music.h// 第二单元 音乐播放器//// Created by 爱人闵敬凌 on 2017/6/7.// Copyright © 2017年 百度. All rights reserved.//#import@interface Music : NSObject

@property(nonatomic,strong)NSString *name;

@property(nonatomic,strong)NSString *singer;

//数组

-(NSMutableArray *)getSongArray;

@end


//

//  Music.m

//  第二单元 音乐播放器

//

//  Created by 爱人闵敬凌 on 2017/6/7.

//  Copyright © 2017年 百度. All rights reserved.

//

#import "Music.h"

@implementation Music

//数组

-(NSMutableArray *)getSongArray

{

//获取沙盒里面的数据

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"music" ofType:@"plist"];

return [NSMutableArray arrayWithContentsOfFile:filePath];

}

@end


//// NameView.h// 第二单元 音乐播放器//// Created by 爱人闵敬凌 on 2017/6/7.// Copyright © 2017年 百度. All rights reserved.//#import@interface NameView : UIView

@property(nonatomic,strong)UITableView *tv;

@end

//

//  NameView.m

//  第二单元 音乐播放器

//

//  Created by 爱人闵敬凌 on 2017/6/7.

//  Copyright © 2017年 百度. All rights reserved.

//

#import "NameView.h"

@implementation NameView

-(instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame])

{

[self addSubview:self.tv];

}

return self;

}

-(UITableView *)tv

{

if (!_tv)

{

_tv = [[UITableView alloc]initWithFrame:self.frame];

}

return _tv;

}

@end


//// SingerView.h// 第二单元 音乐播放器//// Created by 爱人闵敬凌 on 2017/6/7.// Copyright © 2017年 百度. All rights reserved.//#import@interface SingerView : UIView

@property(nonatomic,strong)UIImageView *img;

@property(nonatomic,strong)UIView *sv;

@property(nonatomic,strong)UISlider *slider;

@property(nonatomic,strong)UIButton *btnone,*btntwo,*btnthree,*btnfour,*btnfive;

@end


//

//  SingerView.m

//  第二单元 音乐播放器

//

//  Created by 爱人闵敬凌 on 2017/6/7.

//  Copyright © 2017年 百度. All rights reserved.

//

#import "SingerView.h"

@implementation SingerView

//单例方法

-(instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame])

{

[self addSubview:self.img];

[self addSubview:self.sv];

}

return self;

}

-(UIImageView *)img

{

if (!_img)

{

_img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, self.frame.size.width, self.frame.size.height - 64 - 150)];

_img.image = [UIImage imageNamed:@"  .png"];

}

return _img;

}

-(UIView *)sv

{

if (!_sv)

{

_sv = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 150, self.frame.size.width, 150)];

_sv.backgroundColor = [UIColor whiteColor];

[_sv addSubview:self.slider];

[_sv addSubview:self.btnone];

[_sv addSubview:self.btntwo];

[_sv addSubview:self.btnthree];

[_sv addSubview:self.btnfour];

[_sv addSubview:self.btnfive];

}

return _sv;

}

-(UISlider *)slider

{

if (!_slider)

{

_slider = [[UISlider alloc]initWithFrame:CGRectMake(20, 60, self.frame.size.width - 40, 30)];

//最大值

[_slider setMaximumValue:1.0];

//最小值

[_slider setMinimumValue:0.0];

}

return _slider;

}

-(UIButton *)btnone;

{

if (!_btnone)

{

_btnone = [UIButton buttonWithType:UIButtonTypeRoundedRect];

_btnone.frame = CGRectMake(self.frame.size.width/2 - 30, 90, 60, 60);

[_btnone setImage:[UIImage imageNamed:@"36676757842381277.jpg"] forState:UIControlStateNormal];

}

return _btnone;

}

@end


//// ViewController.m// 第二单元 音乐播放器//// Created by 爱人闵敬凌 on 2017/6/7.// Copyright © 2017年 百度. All rights reserved.//#import "ViewController.h"#import "Music.h"#import "NameView.h"#import "SingerView.h"#import "SecViewController.h"@interface ViewController (){

NSMutableArray *arr;

NameView *dataView;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

arr = [NSMutableArray array];

dataView = [[NameView alloc]initWithFrame:self.view.frame];

self.view = dataView;

//之前的表格(懒加载)

dataView.tv.delegate = self;

dataView.tv.dataSource = self;

//获取数据

Music *music = [[Music alloc]init];

//获取路径

arr = [music getSongArray];

}

//展示出来

#pragma -

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return  arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];

if (!cell)

{

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];

}

cell.textLabel.text = [arr[indexPath.row]objectForKey:@"name"];

cell.detailTextLabel.text = [arr[indexPath.row]objectForKey:@"singer"];

return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

SecViewController *vc = [SecViewController new];

vc.m = [[Music alloc]init];

vc.m.name = [arr[indexPath.row]objectForKey:@"name"];

vc.m.singer = [arr[indexPath.row]objectForKey:@"singer"];

[self.navigationController pushViewController:vc animated:YES];

}

@end


//// SecViewController.h// 第二单元 音乐播放器//// Created by 爱人闵敬凌 on 2017/6/7.// Copyright © 2017年 百度. All rights reserved.//#import#import "Music.h"

@interface SecViewController : UIViewController

@property(nonatomic,strong)Music *m;

@end

//// SecViewController.m// 第二单元 音乐播放器//// Created by 爱人闵敬凌 on 2017/6/7.// Copyright © 2017年 百度. All rights reserved.//#import "SecViewController.h"#import "SingerView.h"#import "AppDelegate.h"#import "Music.h"#import "ViewController.h"#import@interface SecViewController ()

{

SingerView *s;

AppDelegate *app;

}

@end

@implementation SecViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.title = [NSString stringWithFormat:@"%@:%@",self.m.name,self.m.singer];

s = [[SingerView alloc]initWithFrame:self.view.frame];

self.view = s;

//强转

app = (AppDelegate *)[[UIApplication sharedApplication]delegate];

NSString *str = [[NSBundle mainBundle]pathForResource:self.m.name ofType:@"mp3"];

app.player = [[AVAudioPlayer alloc]initWithData:[NSData dataWithContentsOfFile:str] error:nil];

[app.player play];

[s.btnone addTarget:self action:@selector(Clickbtn:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)Clickbtn:(UIButton *)ub

{

if(ub.isSelected == YES)

{

ub.selected = NO;

[app.player play];

}

else if(ub.isSelected == NO)

{

ub.selected = YES;

[app.player stop];

}

}

@end

音乐_第1张图片

你可能感兴趣的:(音乐)