//
// ViewController.m
// 音乐播放器
//
// Created by 郭宝 on 16/8/20.
// Copyright © 2016年 郭宝. All rights reserved.
//
import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
// 用到的协议方法
@interface ViewController ()
<AVAudioPlayerDelegate,UITableViewDelegate,UITableViewDataSource>
// 播放器
@property (nonatomic, strong) AVAudioPlayer *player;
// 表示进度的slider
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;
// 计时器
@property (nonatomic, strong) NSTimer *timer;
// 显示时间进度的label
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
// 音乐名称
@property (weak, nonatomic) IBOutlet UILabel *musicNameLabel;
// 存储音乐url的数组
@property (nonatomic, strong) NSArray *musicArray;
// 音乐的下标
@property (nonatomic, assign) NSInteger index;
// 音乐列表的tableView
@property (nonatomic, strong) UITableView *tableView;
// 开始按钮
@property (weak, nonatomic) IBOutlet UIButton *startButton;
// 循环模式的segControl
@property (weak, nonatomic) IBOutlet UISegmentedControl *repeatModelSegControl;
@end
@implementation ViewController
// 懒加载音乐url数组
- (NSArray *)musicArray {
_musicArray = [NSArray array];
NSURL *url0 = [[NSBundle mainBundle] URLForResource:@"演员.mp3" withExtension:nil];
NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"牵线木偶.mp3" withExtension:nil];
NSURL *url2 = [[NSBundle mainBundle] URLForResource:@"演员.mp3" withExtension:nil];
NSURL *url3 = [[NSBundle mainBundle] URLForResource:@"牵线木偶.mp3" withExtension:nil];
NSURL *url4 = [[NSBundle mainBundle] URLForResource:@"演员.mp3" withExtension:nil];
NSURL *url5 = [[NSBundle mainBundle] URLForResource:@"牵线木偶.mp3" withExtension:nil];
_musicArray = @[url0,url1,url2,url3,url4,url5];
return _musicArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 创建音乐列表的tableViewself.index = 0;
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 350, 340, 250) style:UITableViewStylePlain];
self.tableView = tableView;
// 设置代理和数据源
self.tableView.delegate = self;
self.tableView.dataSource = self;
// 添加
[self.view addSubview:tableView];
[self loadMusic];
// 设置子控件们的tintColor
NSArray *array = [self.view subviews];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj setTintColor:[UIColor grayColor]];
}];
}
- (void)loadMusic {
// 创建一个错误对象,用来接收错误信息
NSError *error;
// 创建播放器对象 传入本地url
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.musicArray[self.index] error:&error];
// 设置代理
self.player.delegate = self;
// 打印错误信息
if (error) {
NSLog(@"%@",error);
}
// 创建一个计时器,用于记录播放进度--在计时器方法里把currentTime赋值给slider的value
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sliderDisplay) userInfo:nil repeats:YES];
// 设置slider的最大值
self.progressSlider.maximumValue = self.player.duration;
// 音乐名称
NSURL *url = self.self.musicArray[self.index];
self.musicNameLabel.text = url.lastPathComponent;
}
// 控制音量的slider
- (IBAction)volumeSlider:(UISlider *)sender {
self.player.volume = sender.value;
}
// 开始播放按钮
- (IBAction)startClick:(UIButton *)sender {
if (!sender.selected) {
// 如果player是空的 就读取音乐 否则就是已经在播放 再次点击的时候不能重新读取音乐
if (self.player == nil) {
[self loadMusic];
}
// 准备播放, 可不写 为了规范要写.
[self.player prepareToPlay];
// 播放
[self.player play];
sender.selected = YES;
} else {
[self.player pause];
sender.selected = NO;
}
}
// 停止
- (IBAction)stopClick:(id)sender {
[self.player pause];
self.player.currentTime = 0;
self.startButton.selected = NO;
}
/** 计时器调用的显示slider的方法 */
- (void)sliderDisplay {
// 赋值
self.progressSlider.value = self.player.currentTime;
// 转换时间格式
NSString *curren = [self timeFormatted:self.player.currentTime];
NSString *all = [self timeFormatted:self.player.duration];
// 把时间拼接赋值给显示时间的label
self.progressLabel.text = [NSString stringWithFormat:@"%@/%@",curren,all];
}
/** 拖动进度条 */
- (IBAction)slideProgress:(UISlider *)sender {
//将当前的播放时间设置为slider的value
self.player.currentTime = sender.value;
}
/** 将秒数转换为分秒格式的时间字符串 */
- (NSString *)timeFormatted:(int)totalSeconds
{
// 将秒数转换为时间
NSDate *date = [NSDate dateWithTimeIntervalSince1970:totalSeconds];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
// 设置时间格式
NSDateFormatter *dateformmatter = [[NSDateFormatter alloc] init];
dateformmatter.dateFormat = @"mm:ss";
NSString *time = [dateformmatter stringFromDate:localeDate];
return time;
}
// 上一曲
- (IBAction)lastMusicClick:(id)sender {
self.startButton.selected = YES; if (self.repeatModelSegControl.selectedSegmentIndex == 0) {
//顺序播放
if (self.index <= 0) {
self.index = self.musicArray.count - 1;
} else {
self.index--;
}
} else {
//随机播放
NSInteger index = arc4random() % self.musicArray.count;
self.index = index;
}
[self loadMusic];
[self.player play];
}
// 下一曲
- (IBAction)nextMusicClick:(id)sender {
```
self.startButton.selected = YES;
if (self.repeatModelSegControl.selectedSegmentIndex == 0) {
// 顺序播放
if (self.index >= self.musicArray.count - 1) {
self.index = 0;
} else {
self.index++;
}
} else {
// 随机播放
NSInteger index = arc4random() % self.musicArray.count;
self.index = index;
}
[self loadMusic];
[self.player play];
}
pragma mark - 播放结束调用的方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if (self.repeatModelSegControl.selectedSegmentIndex == 0) {
// 顺序播放
[self nextMusicClick:nil];
} else {
// 随机播放
NSInteger index = arc4random() % self.musicArray.count;
self.index = index;
[self loadMusic];
[self.player play];
}
}
// 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.musicArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"reuse"];
cell.textLabel.text = url.lastPathComponent;
cell.textColor = [UIColor grayColor];
return cell;
}
// 选中cell, tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.startButton.selected = YES;
self.index = indexPath.row;
// 读取音乐 播放音乐
[self loadMusic];
[self.player play];
}
// 用到的音乐文件自行下载, stroyboard自行拖拽.
![000000.png](http://upload-images.jianshu.io/upload_images/2435291-f225e7c1c52f3580.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)