IOS 手写控件 简单播放器 AVFoundation音乐播放
2019独角兽企业重金招聘Python工程师标准>>>
//
// 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
IOS 手写控件 简单播放器 AVFoundation音乐播放相关推荐
- iOS开发UI基础—手写控件,frame,center和bounds属性
iOS开发UI基础-手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...
- iOS开发UI篇—手写控件,frame,center和bounds属性
一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4)如果是button等控件,还需考虑控件的单击事件等 (5)注意:View ...
- 【毕业设计】28-基于单片机的音乐播放器简易音乐播放器设计(原理图+源代码+仿真工程+答辩PPT+答辩论文)
typora-root-url: ./ [毕业设计]28-基于单片机的音乐播放器简易音乐播放器设计(原理图+源代码+仿真工程+答辩PPT+答辩论文) 文章目录 typora-root-url: ./ ...
- php 在线调用音乐播放器,宅音乐播放器,HTML5网页播放器,带后台管理及API调用,使用thinkphp编写...
宅音乐播放器 宅音乐播放器,HTML5网页播放器,集成后台管理及API调用,目前正在开发中,敬请关注 技术栈 后端:thinkphp 5.1 前端:layui 数据库:mysql 演示 整合依赖安装包 ...
- Android的媒体播放器------简易音乐播放器(详解)
一.案例效果 二.欢迎界面的设计与功能 2.1 .案例效果 设计一个倒计时自动跳转的页面 2.2. 布局界面 activity_welcome.xml 参考代码: <?xml version=& ...
- android 小卷毛播放器,Android 音乐播放器的开发教程(四)Activity和Fragment的通信以及Fragment的切换 ----- 小达...
Activity和Fragment的通信以及Fragment的切换 在上一篇的博客中讲到了,播放器的主界面布局,是由一个activity和一个fragment构成的,activity启动的时候,在其o ...
- android4以下的音乐播放器,动静(音乐播放器)
动静app为你带来手机端播放器中最为高品质的软件平台,同时也是全球乐迷的分享交流之地,有着丰富的音乐类型和融洽的音乐交流氛围,分享歌单与原创内容,十分的好用!点击下载动静app开始体验吧! 动静软件介 ...
- python3 音乐播放器_python3 音乐播放器
2.[代码][Python]代码 from tkinter import * from traceback import * from win32com.client import Dispatch ...
- html5卡拉OK音乐播放器,QQ音乐播放器怎么打开卡拉OK模式
QQ音乐播放器怎么打开卡拉OK模式 时间:2020-08-05 12:25:56 责任编辑:随便就行 QQ音乐播放器怎么打开卡拉OK模式?QQ音乐播放器是生活中常用的音乐播放器,很多人在使用QQ音乐播 ...
- Java乐乐_java音乐播放器-乐乐音乐播放器详解
乐乐音乐目前是基于musique开发的一个java音乐播放器,支持桌面歌词,卡拉ok歌词.详细请移步到我的博客. http://my.oschina.net/u/437124/blog/598749 ...
最新文章
- Python hashlib 无法打印
- 技嘉主板万能网卡驱动_技嘉Z490系列主板来袭:16相供电/钽电容,堆料更进一步...
- MyBatis框架 注解
- 维纳滤波的详细讲解(同为小白,相互取暖)
- 母版页Master中Html控件img,a,javascript相对路径问题
- 为什么阿里不收购OFO小黄车,反而让滴滴抢了先机?
- amd显卡风扇调节_解决办法:设置显卡风扇速度的教程方法
- Kubernetes kubeadm 证书到期,更新证书
- xunsearch使用流程
- 如何获取系统下目录的文件系统类型
- 取消IE8打开HTTPS带图片内容后提示安全警告
- GSM系统信令接续流程
- 第七届河南省程序设计大赛~~海岛争霸(nyoj 1248)
- iphone safari 小说网站 去除 弹框广告
- 墨画子卿第四章第2节:如意随心
- USB host 控制芯片CH374
- iOS编程 手动忽略clang编译器警告
- GL-SF1200 路由器 使用 ipv6 的方法
- 计算机经典书籍下载网址集锦,希望对大家有所帮助
- 【玩转微信公众平台之一】序章(纯粹扯淡)