oc中用Avaudio做的简单播放器界面

1 建立工程,并将所用到的音频,图片等文件加入到工程中去。

2写入代码

  1 //
  2 //  ViewController.m
  3 //  avaudio
  4 //
  5 //  Created by apple on 14-7-18.
  6 //  Copyright (c) 2014年 无名. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import <AVFoundation/AVFoundation.h>
 11 
 12 @interface ViewController ()<AVAudioPlayerDelegate>
 13 {
 14     int  index,di,i,ki,dd;
 15     UIImageView *pic;
 16     AVAudioPlayer *player;
 17     NSArray *list,*soundpicture;
 18    //定义一个定时器的变量
 19     NSTimer *t1;
 20     UIButton *but,*but4;
 21     double a;
 22     int random[100];
 23 }
 24 @property (weak, nonatomic) IBOutlet UISlider *proceed;
 25 @property (weak, nonatomic) IBOutlet UILabel *label2;
 26 @property (weak, nonatomic) IBOutlet UISlider *sound;
 27 @property (weak, nonatomic) IBOutlet UILabel *starttime;
 28 @property (weak, nonatomic) IBOutlet UILabel *remaintime;
 29 @property (weak, nonatomic) IBOutlet UIButton *model;
 30 
 31 @end
 32 
 33 
 34 @implementation ViewController
 35 
 36 -(void)createbutton
 37 {
 38     //对歌曲图面这个空间初始化
 39     pic=[[UIImageView alloc] initWithFrame:CGRectMake(20, 125, 280, 240)];
 40     [self.view addSubview:pic];
 41     //播放暂停按钮
 42    but=[UIButton buttonWithType:UIButtonTypeCustom];
 43     but.frame=CGRectMake(125, 430,45,45);
 44     [but setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
 45     [but addTarget:self action:@selector(didplayer:) forControlEvents:UIControlEventTouchUpInside];
 46     [self.view addSubview:but];
 47     //下一曲按钮
 48     UIButton *but1=[UIButton buttonWithType:UIButtonTypeCustom];
 49     but1.frame=CGRectMake(220, 430,45,45);
 50      [but1 setImage:[UIImage imageNamed:@"6"] forState:UIControlStateNormal];
 51     [but1 addTarget:self action:@selector(didnext:) forControlEvents:UIControlEventTouchUpInside];
 52     [self.view addSubview:but1];
 53     //上一曲按钮
 54     UIButton *but2=[UIButton buttonWithType:UIButtonTypeCustom];
 55     but2.frame=CGRectMake(30, 430,45,45);
 56    [but2 setImage:[UIImage imageNamed:@"4"] forState:UIControlStateNormal];
 57     [but2 addTarget:self action:@selector(didprevious:) forControlEvents:UIControlEventTouchUpInside];
 58     [self.view addSubview:but2];
 59     //停止按钮
 60     UIButton *but3=[UIButton buttonWithType:UIButtonTypeCustom];
 61     but3.frame=CGRectMake(125, 480,45,45);
 62     [but3 setImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
 63     [but3 addTarget:self action:@selector(didstop:) forControlEvents:UIControlEventTouchUpInside];
 64     [self.view addSubview:but3];
 65     //声音控制按钮
 66     but4=[UIButton buttonWithType:UIButtonTypeCustom];
 67     but4.frame=CGRectMake(45, 42,25,25);
 68     [but4 setImage:[UIImage imageNamed:@"9"] forState:UIControlStateNormal];
 69     [but4 addTarget:self action:@selector(controlvoice:) forControlEvents:UIControlEventTouchUpInside];
 70     [self.view addSubview:but4];
 71 }
 72 
 73 //停止播放,进度条为零,播放按钮为暂停图标
 74 -(void)didstop:(UIButton *)sender
 75 {
 76     [player stop];
 77     player.currentTime=0;
 78     [but setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
 79 }
 80 //播放或暂停
 81 -(void)didplayer:(UIButton *)sender
 82 {
 83   if(~di)
 84   {
 85       [self createplay];
 86       [but setImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
 87       
 88   }
 89       else
 90   {
 91       [player pause];
 92       [but setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
 93      
 94   }
 95       di=~di;
 96     
 97 }
 98 //下一曲
 99 -(void)didnext:(UIButton *)sender
100 {
101     if(ki==0||ki==1)
102     {
103       if(index==list.count-1)
104           index=0;
105       else
106           index++;
107     }
108     else if (ki==2)
109     {
110         random[dd]=index;
111       index=arc4random()%15;
112         dd++;
113     }
114     //下一曲后进度条清零,这一步骤必须放在播放之前,即在[self createplay]之前
115     self.proceed.value=0;
116     [self createplay];
117 }
118 //上一曲
119 -(void)didprevious:(UIButton *)sender
120 {
121      if(ki==0||ki==1)
122      {
123      if(index==0)
124         index=list.count-1;//一定要注意 减一,否则就溢出。
125       else index--;
126      }
127     if(ki==2)
128     {
129         if(dd!=0)
130         {
131             index=random[dd-1];
132             dd--;
133         }
134         else index=arc4random()%15;
135     }
136        //上一曲后 进度条清零,,这一步骤必须放在播放之前,即在[self createplay]之前
137     self.proceed.value=0;
138     [self createplay];
139 }
140 //播放文件的产生
141 -(void)createplay
142 {
143     NSString *name=list[index];
144     NSString *picture=soundpicture[index];
145     pic.image=[UIImage imageNamed:picture];
146    
147     //1获取资源文件的文件路径
148     NSString *path=[[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
149  
150     //2. 将文件读取到内存中
151     //NSData *data = [NSData dataWithContentsOfFile:path];
152     //3. 创建一个播放器对象
153     //player = [[AVAudioPlayer alloc] initWithData:data error:nil];
154     //网络地址用下面的方法
155     //[NSURL URLWithString:@"http://www.baidu.com"];
156  
157     //4从文件路径生产一个URL对象
158     NSURL *url=[NSURL fileURLWithPath:path];
159    
160     //AVAudioPlayer只能播放本地文件
161     player=[[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
162     //?????
163     player.delegate=self;
164     player.volume=0.5;
165     
166     //这条语句使暂停后在播放,会接着你暂停的点播放,而不是从头开始播放。
167     player.currentTime=self.proceed.value*player.duration;
168     
169     //播放准备
170     [player prepareToPlay];
171     [player play];
172     
173     //显示歌名
174     self.label2.text=name;
175     
176     //时钟,每隔0.1s执行一次times
177     t1=[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(times) userInfo:nil repeats:YES];
178 }
179 //每隔0.1秒执行的函数
180 -(void)times
181 {
182     //滑动条的值等于歌曲正在播放时间的值/该歌曲的总时间。
183     //从而形成了进度条
184     self.proceed.value=player.currentTime/player.duration;
185  
186     //显示开始的时间
187     int startminute=(int)player.currentTime/60;
188     int statesecond=(int)player.currentTime%60;
189     self.starttime.text=[NSString stringWithFormat:@"%02d:%02d",startminute,statesecond];
190   
191     //显示剩余时间
192     int remainminute=(int)(player.duration-player.currentTime)/60;
193     int remainsecond=(int)(player.duration-player.currentTime)%60;
194     self.remaintime.text=[NSString stringWithFormat:@"%02d:%02d",remainminute,remainsecond];
195     
196 }
197 
198 - (void)viewDidLoad {
199     [super viewDidLoad];
200     
201     //将歌名存入list的数组中
202     list=@[@"陈奕迅一丝不挂",@"陈奕迅 - 淘汰",@"陈奕迅 - 单车",@"陈奕迅 - 不要说话",@"陈柏宇 - 你瞒我瞒",@"周杰伦 安静",@"王杰 - 不浪漫罪名",@"王力宏 Can you feel my world",@"林俊杰 不死之身 ",@"卡农  钢琴曲 ",@"海鸣威  老人与海 ",@"付辛博  这一刻,爱吧 ",@"林俊杰 第二天堂",@"庞龙 兄弟抱一下",@"孙燕姿 遇见"];
203    
204     //将片存入数组中去
205     soundpicture=@[@"一丝不挂",@"淘汰",@"单车",@"不要说话",@"你瞒我瞒",@"安静",@"不浪漫的罪名",@"can yon feel my world",@"不死之身",@"卡农",@"老人与海",@"这一刻爱吧",@"第二天堂",@"兄弟抱一下",@"遇见"];
206     [self createbutton];
207    
208     //初始化进度条
209     self.proceed.value=0;
210    //arc4random
211 }
212 
213 //歌曲播放完了执行
214 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
215 {
216     if(ki==0)
217     {
218       index++;
219       if (index==list.count)
220       {
221         index=0;
222       }
223     }
224     //随机播放模式
225     else  if(ki==2)
226     {
227         index=arc4random()%15;
228     }
229     //停止计时器
230     [t1 invalidate];
231     //播放完后是进度条清零,这一步骤必须放在播放之前,即在[self createplay]之前
232     self.proceed.value=0;
233     [self createplay];
234 }
235 
236 - (void)didReceiveMemoryWarning {
237     [super didReceiveMemoryWarning];
238     // Dispose of any resources that can be recreated.
239 }
240 
241 //歌曲进度条控制
242 - (IBAction)proceeding:(UISlider *)sender {
243     player.currentTime=sender.value*player.duration;
244 }
245 
246 //声音进度条的控制
247 - (IBAction)voice:(UISlider *)sender {
248     player.volume=sender.value;
249     
250 }
251 //声音的静音与还原
252 - (IBAction)controlvoice:(UIButton *)sender {
253     
254     if (~i)
255     {
256         player.volume=0;
257         [but4 setImage:[UIImage imageNamed:@"8"] forState:UIControlStateNormal];
258     }
259     else
260     {
261         player.volume=self.sound.value;
262         [but4 setImage:[UIImage imageNamed:@"9"] forState:UIControlStateNormal];
263     }
264     i=~i;
265 }
266 - (IBAction)nodel:(UIButton *)sender {
267     if (ki==0) {
268         [_model setTitle:@"单曲循环" forState:UIControlStateNormal];
269         ki++;
270     }
271     else  if(ki==1)
272     {
273         [_model setTitle:@"随机播放" forState:UIControlStateNormal];
274         ki++;
275     }
276     else   if(ki==2)
277     {
278         [_model setTitle:@"顺序播放" forState:UIControlStateNormal];
279         ki=0;
280     }
281 }
282 
283 @end

直接注意的时记得加入头文件

#import <AVFoundation/AVFoundation.h>

得到的结果为:



posted @ 2014-07-21 19:24  卢灿  阅读(772)  评论(0编辑  收藏  举报