简易音乐播放器

===========================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{
    [_timer invalidate];
    [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


你可能感兴趣的:(简易音乐播放器)