===========================h文件========================== // ViewController.h // AVAudioPlayer播放音樂 // // Created on 15/12/28. // Copyright © 2015年 All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIProgressView *pro; @property (weak, nonatomic) IBOutlet UIImageView *image; - (IBAction)xia:(id)sender; - (IBAction)shang:(id)sender; - (IBAction)start:(id)sender; @property (weak, nonatomic) IBOutlet UIButton *start; @end ===========================M文件========================== // // ViewController.m // AVAudioPlayer播放音樂 // // Created by dc0061 on 15/12/28. // Copyright © 2015年 dc0061. All rights reserved. // #import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController ()<AVAudioPlayerDelegate> @property (nonatomic,strong) AVAudioPlayer *audioPlayer; @property (nonatomic,strong) NSArray *array; @property (nonatomic,strong) NSTimer *timer; @end @implementation ViewController //自動生成的get方法 - (AVAudioPlayer *) audioPlayer{ if(!_audioPlayer){ NSLog(@"播放器準備啓動,開始實例化"); //獲取音樂文件路徑 NSString *path=[[NSBundle mainBundle] pathForResource:_array[num] ofType:@"mp3"]; NSURL *url=[NSURL fileURLWithPath:path]; //初始化播放器 NSError *error; _audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error]; _audioPlayer.delegate=self; //設置播放循環次數:0是不循環 -1無限循環 _audioPlayer.numberOfLoops=0; _audioPlayer.volume=0.1;//音量範圍 NSLog(@"音樂時常:%f",_audioPlayer.duration); [_audioPlayer prepareToPlay];//加載音頻文件到緩存、不寫的話會隱式加載 } return _audioPlayer; } static int num=0;//控制自動播放、下一首、上一首讀取數據 - (void)viewDidLoad { [super viewDidLoad]; _pro.progress=0;//設置進度條初始值 _array= @[@"陳奕迅 - 浮誇",@"鄭伊健 - for u me",@"你不是真的快樂"]; //設置圖片裁剪、半徑 _image.layer.cornerRadius=200/2; _image.layer.masksToBounds=YES; } - (void) setProgress{ _pro.progress=self.audioPlayer.currentTime/self.audioPlayer.duration; [self turnAround]; } //播放器代理方法--》音樂播放結束時執行 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ _audioPlayer=nil;//清空音樂播放器 num++; if (num==3) { num=0; } [self.audioPlayer play]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (IBAction)xia:(id)sender { _audioPlayer=nil;//清空音樂播放器 num++; if (num==3) { num=0; } [self.audioPlayer play]; } - (IBAction)shang:(id)sender { _audioPlayer=nil;//清空音樂播放器 num--; if (num==-1) { num=2; } [self.audioPlayer play]; } //點擊開始後將按鈕名稱修改爲暫停 - (IBAction)start:(id)sender { _timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(setProgress) userInfo:nil repeats:YES]; [self.audioPlayer play]; [_start setTitle:@"暫停" forState:UIControlStateNormal]; [_start addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside]; } //中止後點擊開始 - (void) stop{ [self.audioPlayer stop]; [_start setTitle:@"開始" forState:UIControlStateNormal]; [_start addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside]; } //設置圖片旋轉 - (void) turnAround{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:4.0f];//旋轉一週的時間 _image.transform=CGAffineTransformRotate(_image.transform, M_PI_4); [UIView commitAnimations];//開始旋轉 } @end