此处仅作记录用,demo不是很完善,会陆续完善和更行
//
// ViewController.m
// audioPlayer_2
//
// Created by jinhui005 on 16/9/8.
// Copyright © 2016年 yhl. All rights reserved.
//
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AFNetworking/AFNetworking.h>
#import "ICESongVO.h"
#import "MJExtension.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#define INITVOLUME 0.6
@interface ViewController ()
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) NSArray *songItemArray;
@property (nonatomic, assign) int index;
@property (nonatomic, strong) ICESongVO *songVO;
@property (nonatomic, assign) BOOL isPlaying;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIView *backcornerView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *songName;
@property (nonatomic, strong) UISlider *volumeSlider;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, strong) UIButton *nextBtn;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSString *totleTimeStr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadMusicData:@"n"];
[self.view addSubview:self.imageView];
[self.view addSubview:self.backcornerView];
[self.view addSubview:self.timeLabel];
[self.view addSubview:self.songName];
[self.view addSubview:self.volumeSlider];
[self.view addSubview:self.progressView];
[self.view addSubview:self.btn];
[self.view addSubview:self.nextBtn];
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.equalTo(self.view).offset(30);
make.height.equalTo(self.imageView.mas_width);
}];
[self.backcornerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.imageView);
make.width.height.equalTo(@70);
}];
[self.songName mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(self.view);
make.bottom.equalTo(self.imageView.mas_top);
}];
[self.volumeSlider mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(18);
make.right.equalTo(self.view).offset(-18);
make.top.equalTo(self.imageView.mas_bottom).offset(40);
make.height.equalTo(@20);
}];
[self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(18);
make.right.equalTo(self.view).offset(-18);
make.top.equalTo(self.volumeSlider.mas_bottom).offset(40);
make.height.equalTo(@2);
}];
[self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.top.equalTo(self.progressView.mas_bottom).offset(30);
make.width.equalTo(@100);
make.height.equalTo(@40);
}];
[self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.btn.mas_right).offset(20);
make.top.bottom.equalTo(self.btn);
make.width.equalTo(@60);
}];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(18);
make.top.equalTo(self.imageView.mas_bottom).offset(18);
make.width.equalTo(@160);
make.height.equalTo(@30);
}];
self.isPlaying = NO;
CGRect rect = [UIScreen mainScreen].bounds;
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.borderWidth = 8;
self.imageView.layer.borderColor = [UIColor grayColor].CGColor;
self.imageView.layer.cornerRadius = rect.size.width/2.0;
self.backcornerView.layer.masksToBounds = YES;
self.backcornerView.layer.cornerRadius = 35;
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.02
target:self
selector:@selector(updateProgress)
userInfo:nil
repeats:YES];
}
-(BOOL)prefersStatusBarHidden {
return YES;
}
#pragma mark - 网络请求
-(void)updateProgress{
//专辑图片旋转
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI / 1440);
CMTime currentTime = [self.player currentTime];
Float64 currentPlaybackTime = CMTimeGetSeconds(currentTime);
int currentTimeMinutes = (unsigned)currentPlaybackTime/60;
int currentTimeSeconds = (unsigned)currentPlaybackTime%60;
NSMutableString *currentTimeString;
if (currentTimeSeconds < 10) {
currentTimeString = [NSMutableString stringWithFormat:@"%d:0%d",currentTimeMinutes,currentTimeSeconds];
}
else{
currentTimeString = [NSMutableString stringWithFormat:@"%d:%d",currentTimeMinutes,currentTimeSeconds];
}
NSMutableString *timeLabelString = [NSMutableString stringWithFormat:@"%@/%@",currentTimeString,self.totleTimeStr];
self.timeLabel.text = timeLabelString;
self.progressView.progress = currentPlaybackTime/[self.songVO.length intValue];
}
- (void)loadMusicData:(NSString *)type {
NSString *urlStr = [NSString stringWithFormat:@"http://douban.fm/j/mine/playlist?type=%@&sid=%@&pt=0.000000&channel=0&from=mainsite", type, self.songVO.sid];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//直接使用服务器用该返回的数据,不做任何解析,我们接到数据后自己去转换
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
//解析数据
NSDictionary *dict = responseObject;
NSArray *songArray = [dict objectForKey:@"song"];
self.songVO = [ICESongVO objectWithKeyValues:songArray[0]];
[self configSonginfo];
[self playSong];
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}];
}
#pragma mark - 交互
- (void)volumeslidervaluechange:(UISlider *)sender {
_player.volume = sender.value;
}
- (void)myBtnAction:(UIButton *)sender {
if (self.isPlaying) {
[self pauseSong];
[sender setTitle:@"开始" forState:UIControlStateNormal];
} else {
[self playSong];
[sender setTitle:@"暂停" forState:UIControlStateNormal];
}
}
- (void)nextBtnClick:(UIButton *)sender {
[self loadMusicData:@"s"];
}
- (void)configSonginfo {
//通过一个网络链接播放音乐
NSURL *url = [NSURL URLWithString:self.songVO.url];
AVPlayerItem *songItem = [[AVPlayerItem alloc] initWithURL:url];
self.player = [[AVPlayer alloc] initWithPlayerItem:songItem];
self.player.volume = INITVOLUME;
[self.imageView sd_setImageWithURL:[NSURL URLWithString:self.songVO.picture]];
self.songName.text = [NSString stringWithFormat:@"%@ %@", self.songVO.title, self.songVO.artist];
int totleTimeMinutes = (unsigned)[self.songVO.length intValue]/60;
int totleTimeSeconds = (unsigned)[self.songVO.length intValue]%60;
self.totleTimeStr = [NSString stringWithFormat:@"%02d:%02d", totleTimeMinutes, totleTimeSeconds];
}
- (void)playSong{
self.isPlaying = YES;
[self.player play];
}
- (void)pauseSong {
self.isPlaying = NO;
[self.player pause];
}
- (void)playNext {
[self.player replaceCurrentItemWithPlayerItem:self.songItemArray[self.index+1]];
}
- (void)playPrevious {
[self.player replaceCurrentItemWithPlayerItem:self.songItemArray[self.index-1]];
}
- (UIImageView *)imageView {
if (nil == _imageView) {
_imageView = [[UIImageView alloc] init];
}
return _imageView;
}
- (UIView *)backcornerView {
if (nil == _backcornerView) {
_backcornerView = [[UIView alloc] init];
_backcornerView.backgroundColor = [UIColor whiteColor];
_backcornerView.layer.borderWidth = 8;
_backcornerView.layer.borderColor = [UIColor grayColor].CGColor;
}
return _backcornerView;
}
- (UILabel *)timeLabel {
if (nil == _timeLabel) {
_timeLabel = [[UILabel alloc] init];
}
return _timeLabel;
}
- (UILabel *)songName {
if (nil == _songName) {
_songName = [[UILabel alloc] init];
}
return _songName;
}
- (UISlider *)volumeSlider {
if (nil == _volumeSlider) {
_volumeSlider = [[UISlider alloc] init];
_volumeSlider.value = INITVOLUME;
[_volumeSlider addTarget:self action:@selector(volumeslidervaluechange:) forControlEvents:UIControlEventValueChanged];
}
return _volumeSlider;
}
- (UIProgressView *)progressView {
if (nil == _progressView) {
_progressView = [[UIProgressView alloc] init];
_progressView.tintColor = [UIColor purpleColor];
}
return _progressView;
}
-(UIButton *)btn {
if (nil == _btn) {
_btn = [[UIButton alloc] init];
_btn.backgroundColor = [UIColor orangeColor];
_btn.layer.cornerRadius = 20;
_btn.layer.masksToBounds = YES;
[_btn setTitle:@"暂停" forState:UIControlStateNormal];
[_btn addTarget:self action:@selector(myBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _btn;
}
- (UIButton *)nextBtn {
if (nil == _nextBtn) {
_nextBtn = [[UIButton alloc] init];
_nextBtn.backgroundColor = [UIColor greenColor];
_nextBtn.layer.cornerRadius = 20;
_nextBtn.layer.masksToBounds = YES;
[_nextBtn setTitle:@"下一首" forState:UIControlStateNormal];
[_nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _nextBtn;
}
@end