博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS 手写控件 简单播放器 AVFoundation音乐播放
阅读量:6826 次
发布时间:2019-06-26

本文共 5963 字,大约阅读时间需要 19 分钟。

hot3.png

//

//  ViewController.m

//  AVFoundation音乐播放

//

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

//  Copyright © 2015 lin. All rights reserved.

//

#import "ViewController.h"

//引入音乐播放框架

#import <AVFoundation/AVFoundation.h>

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

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

你可能感兴趣的文章
[Python3网络爬虫开发实战] 1.7.3-Appium的安装
查看>>
magento 购物车 首页 显示
查看>>
mapper.xml
查看>>
微信小程序之滚动图片
查看>>
NTP多种模式的配置
查看>>
html5--4-4 audio元素/格式的转换
查看>>
第 10 章 文件和异常
查看>>
获取物理路径相关
查看>>
用 Flask 来写个轻博客 (2) — Hello World!
查看>>
(2/24) 快速上手一个webpack的demo
查看>>
不高兴的o( ̄ヘ ̄o#)JJ
查看>>
ruby 镜像安装
查看>>
BZOJ4555: [Tjoi2016&Heoi2016]求和
查看>>
如何用Axure快速制作APP交互原型
查看>>
微软正式开源Blazor,将.NET带回到浏览器
查看>>
Linux中普通用户配置sudo权限(带密或免密)
查看>>
poj1753-Flip Game BFS+位运算
查看>>
DeDe调用body文章内容
查看>>
Eclipse的Debug(一)
查看>>
配置虚拟主机(搭建网站)
查看>>