标签:
---恢复内容开始---
第一次写的未修改,先上传了,以后在修改了,简单的实现了功能,代码不好见谅
主要步骤:
1.新建一个歌曲类 歌名,歌手,格式。。。。
2,播放界面viewcontroller
3,单元格的tableviewcell
4,视图adioviewcontroller
播放界面的代码
1 #import "AdioViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3 4 #import "Song.h" 5 @interface AdioViewController () 6 <AVAudioPlayerDelegate> 7 { 8 AVAudioPlayer* _avAudioPlay;//播放器player 9 //UIProgressView* progressV;//播放进度 10 UISlider* progresslider; 11 UISlider* volumeSlider;//声音控制 12 NSTimer* timer;//监控音频播放进度 13 UIButton* btnAnimation;//播放键 14 UIButton* abtnAnimation;//暂停键 15 UIButton* btn2;//单曲循环 16 UIButton* btn3;//取消单曲循环 17 UIButton* btn4;//随机播放 18 UIButton* btn5;//取消随机播放 19 NSInteger aflag;//设置标示,判断播放模式 20 } 21 @property(nonatomic,strong)AVAudioPlayer* avAudioPlay; 22 @end 23 24 @implementation AdioViewController 25 26 - (void)viewDidLoad 27 { 28 [super viewDidLoad]; 29 aflag = 0;//默认顺序播放 30 //生成随机数种子 31 static BOOL seed = NO; 32 if(!seed){ 33 seed = YES; 34 srandom(time(NULL)); 35 } 36 //播放键 37 btnAnimation = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 38 btnAnimation.frame = CGRectMake(120, 450, 50, 40); 39 btnAnimation.layer.borderWidth = 1.5; 40 [btnAnimation setTitle:@"播放" forState:UIControlStateNormal]; 41 [btnAnimation addTarget:self action:@selector(Play:) forControlEvents:UIControlEventTouchUpInside]; 42 [self.view addSubview:btnAnimation]; 43 //暂停 44 abtnAnimation = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 45 abtnAnimation.frame = CGRectMake(120, 450, 50, 40); 46 abtnAnimation.layer.borderWidth = 1.5; 47 [abtnAnimation setTitle:@"暂停" forState:UIControlStateNormal]; 48 [abtnAnimation addTarget:self action:@selector(Pause) forControlEvents:UIControlEventTouchUpInside]; 49 [self.view addSubview:abtnAnimation]; 50 abtnAnimation.hidden = YES; 51 #pragma mark下一曲 52 UIButton* next = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 53 next.frame = CGRectMake(180, 450, 60, 40); 54 next.layer.borderWidth = 1.5; 55 [next setTitle:@"下一曲" forState:UIControlStateNormal]; 56 [next addTarget:self action:@selector(nextsong) forControlEvents:UIControlEventTouchUpInside]; 57 [self.view addSubview:next]; 58 #pragma mark 上一曲 59 UIButton* last = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 60 last.frame = CGRectMake(50, 450, 60, 40); 61 last.layer.borderWidth = 1.5; 62 [last setTitle:@"上一曲" forState:UIControlStateNormal]; 63 [last addTarget:self action:@selector(lastsong) forControlEvents:UIControlEventTouchUpInside]; 64 [self.view addSubview:last]; 65 66 67 #pragma mark 声音开关控件 68 UILabel* alabl = [[UILabel alloc] initWithFrame:CGRectMake(80, 500, 40, 30)]; 69 [alabl setText:@"静音"]; 70 [self.view addSubview:alabl]; 71 UISwitch* switchs = [[UISwitch alloc] initWithFrame:CGRectMake(120, 500, 50, 40)]; 72 [switchs addTarget:self action:@selector(OnorOff:) forControlEvents:UIControlEventValueChanged]; 73 //默认状态打开 74 switchs.on = YES; 75 [self.view addSubview:switchs]; 76 77 #pragma mark初始化音量控制 78 UILabel* labl = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, 40, 30)]; 79 [labl setText:@"音量"]; 80 [self.view addSubview:labl]; 81 volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 350, 200, 20)]; 82 [volumeSlider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged]; 83 //设置最小音量 84 volumeSlider.minimumValue = 0.0f; 85 volumeSlider.maximumValue = 10.0f; 86 volumeSlider.value = 1.0f; 87 [self.view addSubview:volumeSlider]; 88 89 #pragma mark 初始化播放进度条 90 UILabel* lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 40, 30)]; 91 [lab setText:@"进度"]; 92 [self.view addSubview:lab]; 93 _url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_Asong.song ofType:_Asong.format]]; 94 progresslider = [[UISlider alloc] initWithFrame:CGRectMake(50, 415, 200, 20)]; 95 [progresslider addTarget:self action:@selector(progresscontroll) forControlEvents:UIControlEventValueChanged]; 96 progresslider.minimumValue = 0.0f; 97 progresslider.maximumValue = 1.0f; 98 [self.view addSubview:progresslider]; 99 //用nstimer 来监控播放进度 100 timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playProgress) userInfo:nil repeats:YES]; 101 #pragma mark 单曲循环 102 btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 103 btn2.frame = CGRectMake(100,100,100,50); 104 btn2.layer.borderWidth = 1.5; 105 [btn2 setTitle:@"单曲循环" forState:UIControlStateNormal]; 106 [btn2 addTarget:self action:@selector(shezhixunhuan) forControlEvents:UIControlEventTouchUpInside]; 107 [self.view addSubview:btn2]; 108 109 btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 110 btn3.frame = CGRectMake(100,100,100,50); 111 btn3.layer.borderWidth = 1.5; 112 [btn3 setTitle:@"取消单曲循环" forState:UIControlStateNormal]; 113 [btn3 addTarget:self action:@selector(noxunhuan) forControlEvents:UIControlEventTouchUpInside]; 114 [self.view addSubview:btn3]; 115 btn3.hidden = YES; 116 #pragma mark 随机播放 117 btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 118 btn4.frame = CGRectMake(100,200,100,50); 119 btn4.layer.borderWidth = 1.5; 120 [btn4 setTitle:@"随机播放" forState:UIControlStateNormal]; 121 [btn4 addTarget:self action:@selector(shezhisuiji) forControlEvents:UIControlEventTouchUpInside]; 122 [self.view addSubview:btn4]; 123 124 125 btn5 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 126 btn5.frame = CGRectMake(100,200,100,50); 127 btn5.layer.borderWidth = 1.5; 128 [btn5 setTitle:@"取消随机播放" forState:UIControlStateNormal]; 129 [btn5 addTarget:self action:@selector(quxiaosuiji) forControlEvents:UIControlEventTouchUpInside]; 130 [self.view addSubview:btn5]; 131 btn5.hidden = YES; 132 133 134 } 135 //设置随机播放 136 -(void)shezhisuiji{ 137 btnAnimation.hidden = YES; 138 abtnAnimation.hidden = NO; 139 aflag = 2;//随机播放模式 140 btn3.hidden = YES; 141 btn2.hidden = NO; 142 btn4.hidden = YES; 143 btn5.hidden = NO; 144 } 145 //随机播放方法 146 -(void)suiji{ 147 NSInteger x = random()%[self.Temparray count]; 148 NSLog(@"随机曲目:%ld",x+1); 149 self.Asong = [self.Temparray objectAtIndex:x]; 150 self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_Asong.song ofType:_Asong.format]]; 151 self.avAudioPlay = [[AVAudioPlayer alloc]initWithContentsOfURL:_url error:nil]; 152 //设置代理 153 _avAudioPlay.delegate = self; 154 //设置初始音量 155 _avAudioPlay.volume = 1; 156 self.index = x; 157 //预播放 158 [_avAudioPlay prepareToPlay]; 159 [_avAudioPlay play]; 160 161 } 162 //取消随机播放 163 -(void)quxiaosuiji{ 164 btn4.hidden = NO; 165 btn5.hidden = YES; 166 aflag = 0; 167 } 168 //取消循环 169 -(void)noxunhuan{ 170 aflag = 0;//0默认顺序模式 171 btn3.hidden = YES; 172 btn2.hidden = NO; 173 } 174 //设置循环 175 -(void)shezhixunhuan{ 176 //设置音乐播放次数 0 播一次 1播2次 other循环 177 aflag = 1;//自己设定的标示 为循环模式 178 btn2.hidden = YES; 179 btn3.hidden = NO; 180 btn4.hidden = NO; 181 btn5.hidden = YES; 182 } 183 -(void)xunhuan{ 184 NSLog(@"单曲循环曲目位置:%ld",self.index+1); 185 self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_Asong.song ofType:_Asong.format]]; 186 self.avAudioPlay = [[AVAudioPlayer alloc]initWithContentsOfURL:_url error:nil]; 187 //设置代理 188 _avAudioPlay.delegate = self; 189 //设置初始音量 190 _avAudioPlay.volume = 1; 191 btnAnimation.hidden = YES; 192 abtnAnimation.hidden = NO; 193 //预播放 194 [_avAudioPlay prepareToPlay]; 195 [_avAudioPlay play]; 196 197 } 198 - (void)didReceiveMemoryWarning 199 { 200 [super didReceiveMemoryWarning]; 201 // Dispose of any resources that can be recreated. 202 } 203 //播放 204 -(void)Play:(id)sender{ 205 [_avAudioPlay play]; 206 btnAnimation.hidden = YES; 207 abtnAnimation.hidden = NO; 208 } 209 //取消播放 210 -(void)Stop{ 211 _avAudioPlay.currentTime = 0;//当前播放时间为0 212 213 [_avAudioPlay stop]; 214 } 215 //暂停 216 -(void)Pause{ 217 [_avAudioPlay pause]; 218 abtnAnimation.hidden = YES; 219 btnAnimation.hidden = NO; 220 } 221 //设置是否静音 222 -(void)OnorOff:(UISwitch*)sender{ 223 if(sender.isOn){ 224 _avAudioPlay.volume = volumeSlider.value; 225 }else{ 226 _avAudioPlay.volume = 0; 227 } 228 } 229 //改变音量 230 -(void)volumeChange{ 231 _avAudioPlay.volume = volumeSlider.value; 232 } 233 #pragma mark 播放进度改变 234 -(void)playProgress{ 235 progresslider.value = _avAudioPlay.currentTime/_avAudioPlay.duration; 236 } 237 -(void)progresscontroll{ 238 _avAudioPlay.currentTime = progresslider.value*_avAudioPlay.duration; 239 } 240 #pragma mark播放完成时调用的方法 241 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ 242 //自己设定1 单曲循环 2随机播放 0默认顺序 243 if(aflag == 1){ 244 [self xunhuan]; 245 }else if(aflag == 2){ 246 [self suiji]; 247 }else{ 248 [self nextsong]; 249 } 250 251 // [timer invalidate]; 252 // timer = nil; 253 254 } 255 //下一首歌 256 -(void)nextsong{ 257 [_avAudioPlay stop]; 258 259 if(++self.index == [self.Temparray count]){ 260 self.index = 0; 261 } 262 NSLog(@"当前曲目位置:%ld",self.index+1); 263 self.Asong = [self.Temparray objectAtIndex:self.index]; 264 self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_Asong.song ofType:_Asong.format]]; 265 self.avAudioPlay = [[AVAudioPlayer alloc]initWithContentsOfURL:_url error:nil]; 266 //设置代理 267 _avAudioPlay.delegate = self; 268 //设置初始音量 269 _avAudioPlay.volume = 1; 270 btnAnimation.hidden = YES; 271 abtnAnimation.hidden = NO; 272 //预播放 273 [_avAudioPlay prepareToPlay]; 274 [_avAudioPlay play]; 275 276 } 277 //上首歌 278 -(void)lastsong{ 279 [_avAudioPlay stop]; 280 281 if(--self.index == -1){ 282 self.index = [self.Temparray count] -1; 283 } 284 NSLog(@"上一首方法曲目位置:%ld",self.index); 285 self.Asong = [self.Temparray objectAtIndex:self.index]; 286 self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_Asong.song ofType:_Asong.format]]; 287 self.avAudioPlay = [[AVAudioPlayer alloc]initWithContentsOfURL:_url error:nil]; 288 //设置代理 289 _avAudioPlay.delegate = self; 290 //设置初始音量 291 _avAudioPlay.volume = 1; 292 btnAnimation.hidden = YES; 293 abtnAnimation.hidden = NO; 294 //预播放 295 [_avAudioPlay prepareToPlay]; 296 [_avAudioPlay play]; 297 } 298 //视图出现方法 299 -(void)viewWillAppear:(BOOL)animated{ 300 [_avAudioPlay stop]; 301 self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:self.Asong.song ofType:self.Asong.format]]; 302 self.avAudioPlay = [[AVAudioPlayer alloc]initWithContentsOfURL:_url error:nil]; 303 //设置代理 304 _avAudioPlay.delegate = self; 305 //设置初始音量 306 _avAudioPlay.volume = 1; 307 308 //预播放 309 [_avAudioPlay prepareToPlay]; 310 btnAnimation.hidden = YES; 311 abtnAnimation.hidden = NO; 312 [_avAudioPlay play]; 313 }@end
1 #import "ShowViewController.h" 2 #import "MyTableViewCell.h" 3 #import "Song.h" 4 #import "AdioViewController.h" 5 @interface ShowViewController () 6 7 @end 8 9 @implementation ShowViewController 10 11 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 12 { 13 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 14 if (self) { 15 // Custom initialization 16 } 17 return self; 18 } 19 20 - (void)viewDidLoad 21 { 22 [super viewDidLoad]; 23 self.title = @"我的歌单"; 24 Song* A = [[Song alloc] initWithSong:@"我从草原来" andSinger:@"凤凰传奇"andFormat:@"mp3"]; 25 Song* B = [[Song alloc] initWithSong:@"地老天荒" andSinger:@"陈蛋兄" andFormat:@"mp3"]; 26 Song* C = [[Song alloc] initWithSong:@"独家回忆" andSinger:@"陈小春" andFormat:@"mp3"]; 27 Song* D = [[Song alloc] initWithSong:@"关不上的窗" andSinger:@"周传雄" andFormat:@"mp3"]; 28 SongArray = [[NSMutableArray alloc] initWithCapacity:4]; 29 [SongArray addObject:A]; 30 [SongArray addObject:B]; 31 [SongArray addObject:C]; 32 [SongArray addObject:D]; 33 Tables = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 34 Tables.backgroundColor = [UIColor grayColor]; 35 Tables.delegate = self; 36 Tables.dataSource = self; 37 [self.view addSubview:Tables]; 38 adio = [[AdioViewController alloc] init]; 39 // Do any additional setup after loading the view. 40 } 41 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 42 return 1; 43 } 44 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 45 return [SongArray count]; 46 } 47 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 48 static NSString* cellidentify = @"cell"; 49 MyTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellidentify]; 50 if(!cell){ 51 cell = [[MyTableViewCell alloc] initWithStyle:0 reuseIdentifier:cellidentify]; 52 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 53 } 54 Song* asong = [SongArray objectAtIndex:[indexPath row]]; 55 cell.song.text = asong.song; 56 cell.singer.text = asong.singer; 57 return cell; 58 } 59 - (void)didReceiveMemoryWarning 60 { 61 [super didReceiveMemoryWarning]; 62 // Dispose of any resources that can be recreated. 63 } 64 //点击单元格 65 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 66 67 68 if(adio){ 69 adio.Asong = nil; 70 } 71 adio.index = [indexPath row]; 72 adio.Asong = [SongArray objectAtIndex:[indexPath row]]; 73 74 adio.Temparray = SongArray; 75 NSLog(@"当前曲目位置:%ld",adio.index+1); 76 [self.navigationController pushViewController:adio animated:YES]; 77 }@end
---恢复内容结束---
标签:
原文地址:http://www.cnblogs.com/Xiaoshun/p/4818806.html