IOS 手写控件 简单播放器 AVFoundation音乐播放

//

//  ViewController.m

//  AVFoundation音乐播放

//

//  Created by dc008 on 15/12/28.

//  Copyright © 2015 lin. All rights reserved.

//


#import "ViewController.h"

//引入音乐播放框架

#import <AVFoundation/AVFoundation.h>

@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;//音乐播放器



@end


@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


转载于:https://my.oschina.net/u/2499773/blog/552459

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值