【代码笔记】iOS-仿QQ空间,歌曲播放
一,效果图。

二,工程图。

三,代码。
RootViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface RootViewController : UIViewController
<AVAudioPlayerDelegate>
{
//存放歌曲数组
NSMutableArray *musicArray;
//名字所在的label
UILabel * musicNameLabel;
//播放器
AVAudioPlayer * _player;
//进度后的背景色
UIView * overLayView;
//声音控制
UISlider * volumeSlider;
//歌曲进度
UISlider * progressSlider;
//语音所在的灰色背景
UIView * functionView;
//前一首按钮
UIButton * prevButton;
//播放按钮
UIButton * playButton;
//下一首按钮
UIButton * nextButton;
//重播按钮
UIButton * repeatButton;
//歌曲播放的时间
UILabel *startTime;
//歌曲剩下的时间
UILabel * currentTime; //存放歌曲在数组中的当前位置
NSInteger currentIndex;
NSTimer *timer; //图片框以及里面的图片
UIView *transitionView;
//灰色的图
UIImageView * borderView;
//中间的空间图片
UIView * middleView; }
@end

RootViewController.m

#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad]; self.navigationController.navigationBarHidden=YES; //歌曲所在的数组
musicArray=[[NSMutableArray alloc] initWithObjects:@"一眼万年",@"中国话", @"夜曲",@"三寸天堂",nil]; //设置背景色
UIImage *image=[UIImage imageNamed:@"audio_bg.png"];
self.view.backgroundColor =[UIColor colorWithPatternImage:image]; //显示歌曲的名字
UIView *navView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
navView.backgroundColor=[UIColor clearColor];
navView.alpha=0.9;
[self.view addSubview:navView]; //歌曲的名字所在的label
musicNameLabel=[[UILabel alloc] initWithFrame:CGRectMake(80, 0, 160, 44)];
musicNameLabel.backgroundColor=[UIColor clearColor];
musicNameLabel.textColor=[UIColor blackColor];
musicNameLabel.text = [musicArray objectAtIndex:0];
musicNameLabel.font=[UIFont boldSystemFontOfSize:12];
musicNameLabel.textAlignment=NSTextAlignmentCenter; [navView addSubview:musicNameLabel]; //歌曲的地址
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[musicArray objectAtIndex:0] ofType:@"mp3"]]; _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//准备播放,先提前把缓存准备好.
[_player prepareToPlay];
//开启变速功能
_player.enableRate = YES;
_player.delegate = self;
[self playerViewInit]; }
#pragma -mark -functions
//设置播放器的画面的排版
-(void)playerViewInit{
//进度后的背景色
overLayView=[[UIView alloc] initWithFrame:CGRectMake(0.0f, 44.0f, 320.0f, 80.0f)];
overLayView.backgroundColor=[UIColor colorWithWhite:0.1f alpha:0.2f];
overLayView.hidden=NO;
[self.view addSubview:overLayView]; //声音调节
volumeSlider=[[UISlider alloc]initWithFrame:CGRectMake(20.0f, 20.0f, 250.0f, 30.0f)];
volumeSlider.backgroundColor=[UIColor clearColor];
[volumeSlider setThumbImage:[UIImage imageNamed:@"com_thumb_max_h.png"] forState:UIControlStateNormal];
[volumeSlider setMinimumTrackImage:[[UIImage imageNamed:@"com_slider_min_l.png"]stretchableImageWithLeftCapWidth:10 topCapHeight:0] forState:UIControlStateNormal];
volumeSlider.alpha=0.6f;
volumeSlider.minimumValue=0.0f;
volumeSlider.maximumValue=1.0f;
volumeSlider.value=0.2f;
[volumeSlider addTarget:self action:@selector(doClickChangeVolume) forControlEvents:UIControlEventValueChanged];
volumeSlider.minimumValueImage=[UIImage imageNamed:@"cell_radio_speaker.png"]; [overLayView addSubview:volumeSlider];
_player.volume = volumeSlider.value; //语音后的黑色按钮
functionView=[[UIView alloc]initWithFrame:CGRectMake(0, 460-90, 320, 90)] ;
functionView.backgroundColor=[UIColor colorWithWhite:0.1f alpha:0.2f];
[self.view addSubview:functionView]; //前一首按钮
prevButton=[UIButton buttonWithType:UIButtonTypeCustom];
prevButton.frame=CGRectMake(50, 10.0f, 40.0f, 40.0f);
[prevButton setImage:[UIImage imageNamed:@"audio_btn_pre_h.png"] forState:UIControlStateNormal];
[prevButton setImage:[UIImage imageNamed:@"audio_btn_pre_n.png"] forState:UIControlStateHighlighted];
[prevButton addTarget:self action:@selector(doClickPrevButton:) forControlEvents:UIControlEventTouchUpInside];
[functionView addSubview:prevButton]; //播放按钮
playButton=[UIButton buttonWithType:UIButtonTypeCustom];
playButton.frame=CGRectMake(140, 10.0f, 40.0f, 40.0f);
playButton.tag = 10;
[playButton setImage:[UIImage imageNamed:@"audio_btn_play_h.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(doClickPlayButton:) forControlEvents:UIControlEventTouchUpInside];
[functionView addSubview:playButton]; //后一首按钮
nextButton=[UIButton buttonWithType:UIButtonTypeCustom];
nextButton.frame=CGRectMake(230.0f, 10.0f, 40.0f, 40.0f);
[nextButton setImage:[UIImage imageNamed:@"audio_btn_next_h.png"] forState:UIControlStateNormal];
[nextButton setImage:[UIImage imageNamed:@"audio_btn_next_n.png"] forState:UIControlStateHighlighted];
[nextButton addTarget:self action:@selector(doClickNextButton:) forControlEvents:UIControlEventTouchUpInside];
[functionView addSubview:nextButton]; //音量控制条
progressSlider =[[UISlider alloc]initWithFrame:CGRectMake(50.0f, 55.0f, 230.0f, 20.0f)];
progressSlider.minimumValue=0.0f;
progressSlider.maximumValue=1.0f;
progressSlider.backgroundColor=[UIColor clearColor];
[progressSlider setThumbImage:[UIImage imageNamed:@"com_thumb_max_h.png"] forState:UIControlStateNormal];
[progressSlider setMinimumTrackImage:[[UIImage imageNamed:@"com_slider_min_l.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0]forState:UIControlStateNormal];
[progressSlider addTarget:self action:@selector(doClickProgressButton) forControlEvents:UIControlEventValueChanged];
[functionView addSubview:progressSlider]; //重复按钮
repeatButton=[UIButton buttonWithType:UIButtonTypeCustom];
repeatButton.frame=CGRectMake(5.0f, 20.0f, 30.0, 30.0f);
repeatButton.tag=20;
[repeatButton setImage:[UIImage imageNamed:@"audio_btn_random_h.png"] forState:UIControlStateNormal];
[repeatButton addTarget:self action:@selector(doClickRepeatButton:) forControlEvents:UIControlEventTouchUpInside];
[functionView addSubview:repeatButton]; //开始播放时间
startTime=[[UILabel alloc] initWithFrame:CGRectMake(10.0f, 50.0f, 40.0f, 30.0)];
startTime.backgroundColor=[UIColor clearColor];
startTime.text=[NSString stringWithFormat:@"%02d:%02d",0,0];
startTime.textColor=[UIColor whiteColor];
startTime.font=[UIFont systemFontOfSize:13.0f];
[functionView addSubview:startTime]; //歌曲当前的剩下的时间
currentTime=[[UILabel alloc]initWithFrame:CGRectMake(285.0f, 50.0f, 40.0f, 30.0f)];
currentTime.backgroundColor=[UIColor clearColor];
currentTime.text=[NSString stringWithFormat:@"%02d:%02d",0,0];
currentTime.font=[UIFont systemFontOfSize:13.0f];
currentTime.textColor=[UIColor whiteColor];
[functionView addSubview:currentTime]; //中间的空间背景图
transitionView=[[UIView alloc] initWithFrame:CGRectMake(0, 88, 320, 416-90-44)];
[self.view addSubview:transitionView]; //边框
borderView=[[UIImageView alloc]initWithFrame:CGRectMake(60.0f, 90.0f-44.0f, 200.0f, 200.0f)];
borderView.image=[UIImage imageNamed:@"audio_cover_bg.png"];
borderView.tag=200;
transitionView.userInteractionEnabled=YES;
[transitionView addSubview:borderView]; //空间图
middleView=[[UIView alloc]initWithFrame:CGRectMake(15.0f, 15.0f, 170.0f, 170.0f)];
middleView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"audio_album_default.png"]];
middleView.alpha=1.0f;
middleView.tag=201;
[borderView addSubview:middleView]; UITapGestureRecognizer *tapGes=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes)];
[transitionView addGestureRecognizer:tapGes]; }
#pragma -mark -AVAudioPlayerDelegate
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
currentIndex++;
if (currentIndex == musicArray.count) {
currentIndex = 0;
} [self loadMusic:[musicArray objectAtIndex:currentIndex] type:@"mp3"];
musicNameLabel.text = [musicArray objectAtIndex:currentIndex];
[_player play];
}
#pragma -mark -functions
-(void)loadMusic:(NSString*)name type:(NSString*)type
{
NSString* path= [[NSBundle mainBundle] pathForResource: name ofType:type]; NSURL* url = [NSURL fileURLWithPath:path]; _player= [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
_player.delegate=self;
_player.volume= volumeSlider.value;
[_player prepareToPlay]; }
#pragma -mark -doClickActions
//点击背景图的时候,歌曲进度所在的部分隐藏
-(void)tapGes{
if (overLayView.hidden == YES) {
overLayView.hidden = NO;
}else{
overLayView.hidden = YES;
}
}
//声音播放的条
-(void)doClickProgressButton{
_player.volume = volumeSlider.value; }
//前一首歌曲
-(void)doClickPrevButton:(UIButton *)sender{
BOOL playFlag;
if (_player.playing) {
playFlag = YES;
[_player stop];
}else{
playFlag = NO;
}
currentIndex--;
if (currentIndex < 0) {
currentIndex =musicArray.count - 1;
}
[self loadMusic:[musicArray objectAtIndex:currentIndex] type:@"mp3"];
musicNameLabel.text = [musicArray objectAtIndex:currentIndex];
if (playFlag == YES) {
[_player play];
}
}
//刷新进度条
-(void)refresh{
progressSlider.value = _player.currentTime/_player.duration;
startTime.text = [NSString stringWithFormat:@"%02d:%02d",(int)_player.currentTime/60,(int)_player.currentTime%60];
currentTime.text = [NSString stringWithFormat:@"%d:%02d",(int)(_player.duration-_player.currentTime)/60,(int)(_player.duration-_player.currentTime)%60];
}
//播放按钮
-(void)doClickPlayButton:(UIButton *)sender{
if (_player.playing) {
[_player pause];
[playButton setImage:[UIImage imageNamed:@"audio_btn_play_h.png"] forState:UIControlStateNormal];
[timer invalidate];
}else{
[_player play];
[playButton setImage:[UIImage imageNamed:@"audio_btn_pause_h.png"] forState:UIControlStateNormal];
//更新下进度条
timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(refresh) userInfo:nil repeats:YES];
}
}
//下一首按钮
-(void)doClickNextButton:(UIButton *)sender{
BOOL playFlag;
if (_player.playing) {
playFlag = YES;
[_player stop];
}else{
playFlag = NO;
}
currentIndex++;
if (currentIndex == musicArray.count) {
currentIndex = 0;
}
[self loadMusic:[musicArray objectAtIndex:currentIndex] type:@"mp3"]; musicNameLabel.text = [musicArray objectAtIndex:currentIndex]; if (playFlag == YES) {
[_player play];
} }
//重复按钮
-(void)doClickRepeatButton:(UIButton *)sender{
;
}
//进度按钮
-(void)doClickChangeVolume{
_player.currentTime = _player.duration *progressSlider.value;
} @end

【代码笔记】iOS-仿QQ空间,歌曲播放的更多相关文章
- iOS传感器集锦、飞机大战、开发调试工具、强制更新、Swift仿QQ空间头部等源码
		iOS精选源码 飞机大作战 MUPhotoPreview -简单易用的图片浏览器 LLDebugTool是一款针对开发者和测试者的调试工具,它可以帮... 多个UIScrollView.UITable ... 
- 仿QQ空间和微信朋友圈,高解耦高复用高灵活
		先看看效果: 用极少的代码实现了 动态详情 及 二级评论 的 数据获取与处理 和 UI显示与交互,并且高解耦.高复用.高灵活. 动态列表界面MomentListFragment支持 下拉刷新与上拉加载 ... 
- 仿QQ空间动态界面分享
		先看看效果: 用极少的代码实现了 动态详情 及 二级评论 的 数据获取与处理 和 UI显示与交互,并且高解耦.高复用.高灵活. 动态列表界面MomentListFragment支持 下拉刷新与上拉加载 ... 
- JS仿QQ空间鼠标停在长图片时候图片自动上下滚动效果
		JS仿QQ空间鼠标停在长图片时候图片自动上下滚动效果 今天是2014年第一篇博客是关于类似于我们的qq空间长图片展示效果,因为一张很长的图片不可能全部把他展示出来,所以外层用了一个容器给他一个高度,超 ... 
- Fragment,仿QQ空间
		转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9023451 在今天的这篇文章当中,我依然会以实战加理论结合 ... 
- Html - 仿QQ空间右下角工具浮动块
		仿QQ空间右下角工具浮动块 <style type="text/css"> .cy-tp-area>.cy-tp-fixbtn>.cy-tp-text { ... 
- iOS --高仿QQ空间页面
		1.首先分析一下qq空间页面的主要2个功能: 1)随着TableView的向上滑动导航栏的颜色渐变,变化过程是从透明变成白色. 2)随着TableView的向下滑动,图片随着offset放大. 2.首 ... 
- 仿QQ空间根据位置弹出PopupWindow显示更多操作效果
		我们打开QQ空间的时候有个箭头按钮点击之后弹出PopupWindow会根据位置的变化显示在箭头的上方还是下方,比普通的PopupWindow弹在屏幕中间显示好看的多. 先看QQ空间效果图: ... 
- mui开发app之多图压缩与上传(仿qq空间说说发表)
		欲实现效果图 提出需求点: 用户可自由添加删除替换多张图片,并且显示相应缩略图,限制为8张 用户可选择压缩图或直接上传原图功能 返回提醒用户会丢失填写的信息 下面一个个实现上述需求,从简单到复杂: 需 ... 
- ScrollView的阻尼回弹效果实现(仿qq空间)
		玩过新浪微博,qq空间等手机客户端的童鞋,都应该清楚,在主界面向下滑动时,会有一个阻尼回弹效果,看起来挺不错,接下来我们就来实现一下这种效果,下拉后回弹刷新界面,先看效果图: 这个是编辑器里面的界面效 ... 
随机推荐
- CSS魔法堂:hasLayout原来是这样!
			前言 过去一直听说旧版本IE下很多诡异bug均由一个神秘角色引起的,那就是hasLayout.趁着最近突然发神经打算好好学习CSS,顺便解答多年来的疑惑. hasLayout到底是何方神圣? hasL ... 
- C#基础01
			ASP.net基础详情 1:Momo就是跨平台的一种.net,借助其Momo可以让其.net网站跑到Lumin和安卓机上面. 2:开发的网站具有安全,速度快,容易配置. 3:互联网开发[网站]和管理系 ... 
- 实时数据显示--SignalR实例演示
			近段时间,有实现一个看板的功能,就是用户更新信息时,即是对数据库的数据进行插入,更新,或是删除时,在墙上的屏幕的数据不需要人为去刷新,用户就能看到更新后的数据. 实现此功能,Insus.NET使用Si ... 
- Web API应用架构设计分析(1)
			Web API 是一种应用接口框架,它能够构建HTTP服务以支撑更广泛的客户端(包括浏览器,手机和平板电脑等移动设备)的框架, ASP.NET Web API 是一种用于在 .NET Framewor ... 
- 三星S4,呼转的来电,在来电界面上不显示的解决方案
			因为工作关系,我有2个号码是呼叫转移到我现在的手机上的,如果有呼叫转移的来电的话,在通话界面里面没有提示该来电是呼叫转移来的.之前我用的诺基亚的,可以看到呼叫转移的来电,在号码的左边有个拐弯的箭头,提 ... 
- PHP函数microtime()
			定义和用法 PHP函数microtime()返回当前 Unix 时间戳和微秒数.PHP函数microtime()语法 microtime(get_as_float)PHP函数microti ... 
- MVC开发经验总结
			网站开发中技术问题的笔记 以下经验基于 MVC 5.2.3 Javascript BUG思考 200 OK真的表示没有问题吗? 有时候Status 200作为WebAPI的返回值,并不表示真的OK,例 ... 
- 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象,  const 指针和指向 const 对象的指针, const 对象的引用
			[源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, con ... 
- form表单及其中元素
			<form method=get/post action="提交路径"> 单行文本域:<input type="text" name=&quo ... 
- linux网络编程 no route to host 解决方案
			linux网络编程 no route to host 解决方案 [整合资料] (2013-05-13 21:38:12) 转载▼ 标签: net iptables it 分类: Linux 参考资料h ... 
