//數組
// ViewController.m緩存
// AVFoundation音樂播放框架
//ide
// Created by dc008 on 15/12/28.oop
// Copyright © 2015年 lin. All rights reserved.atom
//url
#import "ViewController.h"spa
//引入音樂播放框架.net
#import <AVFoundation/AVFoundation.h>3d
@interface ViewController ()<AVAudioPlayerDelegate>
//{
// AVAudioPlayer *audioPlayer;
//}
{
UIButton *_startButton,*_upButton,*_downButton;
UIProgressView *_progressView;
UILabel *_nameLabel,*_singerLabel,*_beginLabel,*_endLabel;
NSArray *_nameArray,*_singerArray,*_imageArray;
UIImageView *_imageView;
NSTimer *_timer;
int _i;//數組下標
BOOL _choose;//判斷暫停
}
@property (nonatomic,strong)AVAudioPlayer *audioPlayer;//音樂播放器
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_choose = YES;
// [self audioPlayer];
[self layoutUI];
[self.audioPlayer play];
_i = 0;
// [_audioPlayer play];
//播放 play
//暫停 pause
//中止 stop
//當前播放時長 currentTime
//音樂時長 duration
//音量 volume
//是否容許改變播放速率 enableRate
//播放速率 rate (範圍0.5-2.0)
//循環播放次數 numberOfLoops
}
- (void)layoutUI{
_upButton = [[UIButton alloc]initWithFrame:CGRectMake(70, 600, 50, 40)];
[_upButton setTitle:@"⏪" forState:UIControlStateNormal];
[_upButton addTarget:self action:@selector(up) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_upButton];
_startButton = [[UIButton alloc]initWithFrame:CGRectMake(170, 600, 50, 40)];
[_startButton setTitle:@"▶️" forState:UIControlStateNormal];
[_startButton addTarget:self action:@selector(suspend) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_startButton];
_downButton= [[UIButton alloc]initWithFrame:CGRectMake(270, 600, 50, 40)];
[_downButton setTitle:@"⏩" forState:UIControlStateNormal];
[_downButton addTarget:self action:@selector(down) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_downButton];
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(turnAround) userInfo:nil repeats:YES];
_progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(85, 590, 220, 30)];
[self.view addSubview:_progressView];
_nameArray = [NSArray array];
_nameArray = @[@"稻香",@"失戀",@"冰雨(live版)",@"七里香(live版)"];
_singerArray = [NSArray array];
_singerArray = @[@"周杰倫",@"草蜢",@"劉德華",@"周杰倫"];
_imageArray = [NSArray array];
_imageArray = @[@"0",@"1",@"2",@"3"];
_beginLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 500, 60, 30)];
[self.view addSubview:_beginLabel];
_endLabel = [[UILabel alloc]initWithFrame:CGRectMake(280, 500, 60, 30)];
[self.view addSubview:_endLabel];
_nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, 375, 20)];
_nameLabel.text = _nameArray[_i];
_nameLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_nameLabel];
_singerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 375, 20)];
_singerLabel.text = [NSString stringWithFormat:@"歌手:%@",_singerArray[_i]];
_singerLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_singerLabel];
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(40, 170, 300, 300)];
_imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",_imageArray[_i]]];
_imageView.layer.cornerRadius = 150;
_imageView.layer.masksToBounds = YES;
// _imageView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_imageView];
self.view.backgroundColor = [UIColor grayColor];
}
- (void)turnAround{
//CGAffineTransformMakeRotation這個方法是根據原始圖形的transform來轉變的
//CGAffineTransformRotate是根據前一次的狀態來改變
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0f];
_imageView.transform = CGAffineTransformRotate(_imageView.transform, M_PI_4);
[UIView commitAnimations];
_progressView.progress = (float)_audioPlayer.currentTime/(float)_audioPlayer.duration;
_beginLabel.text = [NSString stringWithFormat:@"%.2f",_audioPlayer.currentTime];
}
- (void)up{
NSLog(@"上一首");
_i = _i-1;
if (_i == -1) {
_i = 3;
}
_nameLabel.text = _nameArray[_i];
_singerLabel.text = [NSString stringWithFormat:@"歌手:%@",_singerArray[_i]];
_audioPlayer = nil;
_imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",_imageArray[_i]]];
[self.audioPlayer play];
}
- (void)suspend{
if (_choose) {
[self.audioPlayer pause];
_choose = NO;
_timer.fireDate = [NSDate distantFuture];//暫停
NSLog(@"暫停");
}
else{
[self.audioPlayer play];
_choose = YES;
_timer.fireDate = [NSDate distantPast];//開始
NSLog(@"開始");
}
}
- (void)down{
NSLog(@"下一首");
_i = _i+1;
if (_i == 4) {
_i = 0;
}
_nameLabel.text = _nameArray[_i];
_singerLabel.text = [NSString stringWithFormat:@"歌手:%@",_singerArray[_i]];
_audioPlayer = nil;
_imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",_imageArray[_i]]];
[self.audioPlayer play];
}
#pragma mark audioPlayer屬性的get方法
- (AVAudioPlayer *)audioPlayer{
// NSLog(@"get方法被調用");
if (!_audioPlayer) {
NSLog(@"播放器準備啓動,開始實例化");
//1.獲取音樂文件路徑
NSString *urlStr = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@",_nameArray[_i]] ofType:@"mp3"];
//獲取本地音樂url,⚠️只能播放本地
NSURL *url = [NSURL fileURLWithPath:urlStr];
//2.初始化播放器
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
//設置播放器屬性
_audioPlayer.numberOfLoops = 0;//0爲不循環,負數爲無線循環
_audioPlayer.volume = 1;//音量範圍0-1
NSLog(@"音樂時長:%f",_audioPlayer.duration);
[_audioPlayer prepareToPlay];//加載音頻文件到緩存
_audioPlayer.delegate = self;
_endLabel.text = [NSString stringWithFormat:@"%.2f",_audioPlayer.duration];
}
return _audioPlayer;
}
#pragma mark 播放器代理方法-播放結束時
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"音樂播放完成");
_i = _i+1;
if (_i == 4) {
_i = 0;
}
_nameLabel.text = _nameArray[_i];
_singerLabel.text = [NSString stringWithFormat:@"歌手:%@",_singerArray[_i]];
_audioPlayer = nil;
_imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",_imageArray[_i]]];
[self.audioPlayer play];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end