歌曲列表页面:
#import "ViewController.h"
#import "SecondView.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate> //添加数据源和代理协议
{
NSArray *list; //歌曲列表数组
SecondView *second; //将second的变为全局,为了使当第二个页面退出(销毁)时,仍然能够占有内存,歌曲能继续播放
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
list = @[@"阿兰 - 遥远的重逢",@"曲婉婷 - 我为你歌唱",@"闹够了没有",@"陈奕迅 - k歌之王",@"李代沫 - 原来爱情没有刚刚好",@"胡歌、白冰 - 美丽的神话",@"周杰伦 - 明明就",@"神话·情话",@"She Looks So Perfect",@"Justin Bieber - Mistletoe",@"丁当 - 甩开",@"丁当 - 白头吟",@"Demi Lovato - Let It Go",@"五月天 - 伤心的人别听慢歌",@"魏晨 - 我为自己代言",@"陈翔 - 烟火",@"张杰 - 他不懂",@"张杰 - 剑心",@"孙燕姿 - 天使的指纹"];
self.view.backgroundColor = [UIColor whiteColor];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, 320, 460) style:UITableViewStylePlain]; //创建对象,尺寸整屏设定为 initWithFram:self.view.bounds
tableView.dataSource = self; //设置数据源,将tableView的数据源定义在当前的类
tableView.delegate = self; //将播放器代理协议定义在当前类
[self.view addSubview:tableView];
}
#pragma mark - UITableViewDataSource //标记,方便查看
//<UITableViewDataSource>协议里的方法,@required,必须实现的
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return list.count; //返回行数,行数为歌词列表的数目
}
// <UITableViewDataSource>协议里的方法,@required,必须实现的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ //需注意indexPath 是这个方法里的参数,是局部变量,指索引路径
//使用重用标示获取已有的Cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
//使用一个重用标示创建一个Cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; //cell代表每一行的空间
}
cell.textLabel.text = [NSString stringWithFormat:@"%d.%@",indexPath.row+1,list[indexPath.row]]; //在cell上显示文字
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//UITableViewDelegate>协议里的方法,点击行数后会执行的动作
second = [[SecondView alloc] initWithNibName:nil bundle:nil]; //创建第二个页面
second.list = list; //把第一页的歌曲列表传给第二页
[second myrow:indexPath.row];//将点击的行的内容显示在第二个页面
[self presentViewController:second animated:YES completion:nil];//显示第二个界面
}
@end
播放页面:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface SecondView () <AVAudioPlayerDelegate>
{
int _myrow; //定义成员变量 行
AVAudioPlayer *player;
NSTimer *timer;
}
@property (nonatomic,strong) IBOutlet UISlider *slider; //创建滑动条的属性,指针对象
@end
- (void)play
{
NSString *path = [[NSBundle mainBundle] pathForResource:_list[_myrow] ofType:@"mp3"] ;
self.lable.text = [NSString stringWithFormat:@"%@",_list[_myrow]];
//data不会区分文件的扩展名,url会识别扩展名
// NSData *data = [NSData dataWithContentsOfFile:path];
// player = [[AVAudioPlayer alloc] initWithData:data error:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *err;
player = [[AVAudioPlayer alloc] initWithData:data error:&err]; //读出错误,把错误传给err
NSLog(@"err:%@",err); //输出错误
player.delegate = self; //代理协议
[player prepareToPlay];
[player play];
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(contrlProgress) userInfo:nil repeats:YES]; //定时器,控制滑动条的自动移动
}
- (void)myrow:(int)row
{
_myrow = row;
}
- (void)creatButtons:(CGRect)frame setImage:(NSString *)image action:(SEL)action type:(UIButtonType)type //按钮封装
{ //需将参数写进来
UIButton *Btn = [UIButton buttonWithType:UIButtonTypeCustom];
Btn.frame = frame;
[Btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[Btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Btn];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 470)];
bgImageView.image = [UIImage imageNamed:@"01.jpg"];
[self.view addSubview:bgImageView];
[self creatButtons:CGRectMake(15, 35, 45, 45) setImage:@"7.png" action:@selector(backClick:) type:UIButtonTypeSystem];
[self creatButtons:CGRectMake(175, 410, 40, 40) setImage:@"6.png" action:@selector(playClick:) type:UIButtonTypeSystem];
[self creatButtons:CGRectMake(40, 410, 40, 40) setImage:@"5.png" action:@selector(foreClick:) type:UIButtonTypeSystem];
[self creatButtons:CGRectMake(240, 410, 40, 40) setImage:@"4.png" action:@selector(nextClick:) type:UIButtonTypeSystem];
[self creatButtons:CGRectMake(110, 410, 40, 40) setImage:@"9.png" action:@selector(stopClick:) type:UIButtonTypeSystem];
self.view.backgroundColor = [UIColor whiteColor];
self.lable = [[UILabel alloc] initWithFrame:CGRectMake(80, 35, 200, 50)];
self.lable.textColor = [UIColor whiteColor];
[self.view addSubview:self.lable];
self.slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 360, 280, 30)]; //给滑动条赋值,属性只是创建了一个对象,错误写法:UISlider *slider = [[UISlider alloc] initWithFrame:...];
[self.slider addTarget:self action:@selector(moveSlider:) forControlEvents:UIControlEventValueChanged]; //移动滑动条的值
[self.view addSubview:self.slider];
[self play];
}
- (void)contrlProgress
{
self.slider.value = player.currentTime / player.duration;
}
- (void)moveSlider:(UISlider *)sender
{
player.currentTime = sender.value * player.duration;
}
- (void)stopClick:(UIButton *)sender // 停止,进度条置0,歌曲播放时间置0
{
self.slider.value = 0;
player.currentTime = 0;
[player stop];
}
- (void)backClick:(UIButton *)sender
{
#if 0
ViewController *viewcon = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:viewcon animated:YES completion:nil]; //不需要重新创建对象,只需返回前一个页面
#endif
[timer invalidate];//结束定时器
[self dismissViewControllerAnimated:YES completion:nil];//解散当前视图,就是返回前一个视图
}
- (void)playClick:(UIButton *)sender
{
if (player.isPlaying)
{
[player pause];
}
else
{
[player play];
}
}
- (void)foreClick:(UIButton *)sender
{
if (_myrow < 1)
{
_myrow = _list.count;
}
_myrow -- ;
[self play];
}
- (void)nextClick:(UIButton *)sender
{
_myrow ++ ;
if (_myrow == _list.count)
{
_myrow = 0;
}
[self play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
_myrow ++;
if (_myrow == _list.count)
{
_myrow = 0;
}
[self play];
}
@end