(国内知名安卓开发论坛推荐:http://www.eoeandroid.com/)
此代码是IOS一款播放器代码,学习时自己仿照主流播放器写的,该播放器支持各种格式播放,支持上一曲,下一曲,歌词同步播放,音量调节大小,快进,快退等功能,后续功能我会继续完善。代码仅供学习交流,如有写的不好,望各位海涵...希望对刚刚接触这块的童鞋有所帮助......
- 001 #import “ZJViewController.h”
- 002 #import “ZjMusic.h”
- 003
- 004
- 005 @interface ZJViewController ()<AVAudioPlayerDelegate,UITabBarDelegate,UITableViewDataSource>
- 006
- 007 @end
- 008 #define kBtnHeight 50
- 009 #define kBtnWidth 60
- 010 @implementation ZJViewController
- 011
- 012 - (void)viewDidLoad
- 013 {
- 014 [super viewDidLoad];
- 015 [self initView];
- 016 [self initData];
- 017
- 018
- 019
- 020 // NSLog(@“%@”,self.lrcDict);
- 021 }
- 022 -(void)initData
- 023 {
- 024 ZjMusic *music1 = [[ZjMusic alloc] initWithName:@“Right Here Waiting(此情可待)” andType:@“mp3”];
- 025 ZjMusic *music2 = [[ZjMusic alloc] initWithName:@“Beyond-真的爱你” andType:@“mp3”];
- 026 ZjMusic *music3 = [[ZjMusic alloc] initWithName:@“刘德华-爱你一万年” andType:@“mp3”];
- 027 ZjMusic *music4 = [[ZjMusic alloc] initWithName:@“毛宁-涛声依旧” andType:@“mp3”];
- 028 ZjMusic *music5 = [[ZjMusic alloc] initWithName:@“你是我的眼” andType:@“mp3”];
- 029 ZjMusic *music6 = [[ZjMusic alloc] initWithName:@“星星” andType:@“mp3”];
- 030 ZjMusic *music7 = [[ZjMusic alloc] initWithName:@“月光爱人” andType:@“mp3”];
- 031
- 032
- 033 self.musicData = [[NSMutableArray alloc] init];
- 034
- 035 [self.musicData addObject:music1];
- 036 [self.musicData addObject:music2];
- 037 [self.musicData addObject:music3];
- 038 [self.musicData addObject:music4];
- 039 [self.musicData addObject:music5];
- 040 [self.musicData addObject:music6];
- 041 [self.musicData addObject:music7];
- 042 [self loadMusic:music5];
- 043 [self initLrc:music5];
- 044
- 045 self.musicNameLabel.text = music5.name;
- 046
- 047
- 048
- 049 }
- 050 #pragma mark 加载Music
- 051 -(void)loadMusic:(ZjMusic*)music
- 052 {
- 053 NSString *path = [[NSBundle mainBundle] pathForResource:music.name ofType:music.type];
- 054 NSURL *URL = [NSURL fileURLWithPath:path];
- 055
- 056 self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:nil];
- 057 self.audioPlayer.delegate = self;
- 058 self.audioPlayer.volume = 0.5;
- 059 self.volumeSlider.value = self.audioPlayer.volume;
- 060 [self.audioPlayer prepareToPlay];
- 061
- 062 [self returnTotalTime];
- 063
- 064 }
- 065
- 066 #pragma mark 音量slider自动移动,currentTime自动变换
- 067 -(void)currentTimeChange
- 068 {
- 069 [self returnCurrentTime];
- 070 self.durationlSlider.value = self.audioPlayer.currentTime/self.audioPlayer.duration;
- 071 NSLog(@“curtime ---->%d”,(int)self.audioPlayer.currentTime);
- 072
- 073
- 074 // static int index = 0;
- 075
- 076 NSString *key = [NSString stringWithFormat:@“%d”,(int)self.audioPlayer.currentTime];
- 077 NSString *lrc = [self.lrcDict objectForKey:key];
- 078
- 079
- 080
- 081 if(lrc){
- 082 // NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
- 083 // [self.lrcTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
- 084 // index++;
- 085 self.musicLrcLable.text = lrc;
- 086 NSLog(@“index--->%@”,lrc);
- 087
- 088 }
- 089
- 090
- 091 }
- 092
- 093 #pragma mark 上一首
- 094 -(void)previousSound
- 095 {
- 096 BOOL playFlag;
- 097 if(self.audioPlayer.playing){
- 098 playFlag = YES;
- 099 [self.audioPlayer stop];
- 100 }else{
- 101 playFlag = NO;
- 102 }
- 103 _soundIndex --;
- 104 if(self.soundIndex<0){
- 105 self.soundIndex = self.musicData.count-1;
- 106 }
- 107 ZjMusic *music = self.musicData[_soundIndex];
- 108 [self initLrc:music];
- 109 self.musicNameLabel.text = music.name;
- 110 [self loadMusic:music];
- 111
- 112 if(playFlag){
- 113 [self.audioPlayer play];
- 114 }
- 115 }
- 116 #pragma mark 下一曲
- 117 -(void)nextSound
- 118 {
- 119 BOOL playFlag;
- 120 if(self.audioPlayer.playing){
- 121 playFlag = YES;
- 122 [self.audioPlayer stop];
- 123 }else{
- 124 playFlag = NO;
- 125 }
- 126 _soundIndex ++;
- 127 if(self.soundIndex>self.musicData.count-1){
- 128 self.soundIndex = 0;
- 129 }
- 130 ZjMusic *music = self.musicData[_soundIndex];
- 131 [self initLrc:music];
- 132 self.musicNameLabel.text = music.name;
- 133 [self loadMusic:music];
- 134
- 135 if(playFlag){
- 136 [self.audioPlayer play];
- 137 }
- 138 }
- 139 #pragma mark 音量显示
- 140 -(void)showVolumeSlider
- 141 {
- 142 if(self.volumeSlider.hidden == NO){
- 143 self.volumeSlider.hidden = YES;
- 144 }else{
- 145 self.volumeSlider.hidden = NO;
- 146 }
- 147 [self performSelector:@selector(hiddenVolum) withObject:nil afterDelay:5];
- 148 }
- 149 #pragma mark 音量隐藏
- 150 -(void)hiddenVolum
- 151 {
- 152 self.volumeSlider.hidden = YES;
- 153
- 154
- 155 }
- 156 #pragma mark 播放暂停
- 157 -(void)palyAndPause:(UIButton*)button
- 158 {
- 159 NSString *imageName = @“play”;
- 160 if(self.audioPlayer.playing){
- 161 [self.audioPlayer pause];
- 162 imageName = @“play”;
- 163
- 164 [self.timer invalidate];
- 165
- 166 }else{
- 167 [self.audioPlayer play];
- 168 imageName = @“stop”;
- 169
- 170 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(currentTimeChange) userInfo:nil repeats:YES];
- 171 }
- 172
- 173 [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
- 174
- 175 [self returnTotalTime];
- 176 }
- 177 -(void)returnTotalTime
- 178 {
- 179 NSString *totalTime = [NSString stringWithFormat:@“%d:%d”,(int)self.audioPlayer.duration/60,(int)self.audioPlayer.duration%60];
- 180 self.totalTime.text = totalTime;
- 181 }
- 182 #pragma mark 调节音量
- 183 -(void)volumeChange
- 184 {
- 185 self.audioPlayer.volume = self.volumeSlider.value;
- 186 [self returnCurrentTime];
- 187 }
- 188
- 189 -(void)returnCurrentTime
- 190 {
- 191 NSString *currentTime = [NSString stringWithFormat:@“%d:%d”,(int)self.audioPlayer.currentTime/60,(int)self.audioPlayer.currentTime%60];
- 192 if((int)self.audioPlayer.currentTime%60<10){
- 193 currentTime = [NSString stringWithFormat:@“%d:0%d”,(int)self.audioPlayer.currentTime/60,(int)self.audioPlayer.currentTime%60];
- 194 }
- 195 self.currentTimeLabel.text = currentTime;
- 196
- 197
- 198 }
- 199
- 200 #pragma mark 调节时间
- 201 -(void)durationChange
- 202 {
- 203 self.audioPlayer.currentTime = self.durationlSlider.value*self.audioPlayer.duration;
- 204 }
- 205
- 206
- 207 - (void)dealloc {
- 208 [_audioPlayer release];
- 209 [_durationlSlider release];
- 210 [_volumeSlider release];
- 211 [_musicData release];
- 212 [_currentTimeLabel release];
- 213 [_musicNameLabel release];
- 214 [_totalTime release];
- 215 [_lrcDict release];
- 216 [_timeArray release];
- 217 [_lrcTableView release];
- 218 [super dealloc];
- 219 }
- 220 #pragma mark 初始化数据
- 221 -(void)initView
- 222 {
- 223 //当前时间
- 224 self.currentTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 35, 40, 20)];
- 225 self.currentTimeLabel.text = @“0:00”;
- 226 self.currentTimeLabel.backgroundColor = [UIColor clearColor];
- 227 [self.view addSubview:self.currentTimeLabel];
- 228
- 229 //持续时间
- 230 self.durationlSlider = [[UISlider alloc] initWithFrame:CGRectMake(65, 30, 190, 30)];
- 231 [self.durationlSlider addTarget:self action:@selector(durationChange) forControlEvents:UIControlEventValueChanged];
- 232 [self.view addSubview:self.durationlSlider];
- 233
- 234 //总时间
- 235 self.totalTime = [[UILabel alloc] initWithFrame:CGRectMake(265, 35, 40, 20)];
- 236 self.totalTime.text = @“0:00”;
- 237 self.totalTime.backgroundColor = [UIColor clearColor];
- 238 [self.view addSubview:self.totalTime];
- 239
- 240 UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(50, 250, 220, 60)];
- 241 [self.view addSubview:subView];
- 242 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- 243 //上一曲
- 244 button.frame = CGRectMake(0, 0, kBtnWidth, kBtnHeight);
- 245 [button setImage:[UIImage imageNamed:@“left”] forState:UIControlStateNormal];
- 246 [button addTarget:self action:@selector(previousSound) forControlEvents:UIControlEventTouchUpInside];
- 247 [subView addSubview:button];
- 248
- 249 //播放暂停
- 250 button = [UIButton buttonWithType:UIButtonTypeCustom];
- 251 button.frame = CGRectMake(80, 0, kBtnWidth, kBtnHeight);
- 252 [button setImage:[UIImage imageNamed:@“play”] forState:UIControlStateNormal];
- 253 [button addTarget:self action:@selector(palyAndPause:) forControlEvents:UIControlEventTouchUpInside];
- 254 [subView addSubview:button];
- 255
- 256 //下一曲
- 257 button = [UIButton buttonWithType:UIButtonTypeCustom];
- 258 button.frame = CGRectMake(160, 0, kBtnWidth, kBtnHeight);
- 259 [button addTarget:self action:@selector(nextSound) forControlEvents:UIControlEventTouchUpInside];
- 260 [button setImage:[UIImage imageNamed:@“right”] forState:UIControlStateNormal];
- 261 [subView addSubview:button];
- 262
- 263 //显示歌ci
- 264 self.musicLrcLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 320, 44)];
- 265 self.musicLrcLable.textAlignment = NSTextAlignmentCenter;
- 266 self.musicLrcLable.backgroundColor = [UIColor greenColor];
- 267 [self.view addSubview:self.musicLrcLable];
- 268
- 269 //显示歌名
- 270 self.musicNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 40)];
- 271 self.musicNameLabel.backgroundColor = [UIColor clearColor];
- 272 self.musicNameLabel.textAlignment = NSTextAlignmentCenter;
- 273 [self.view addSubview:self.musicNameLabel];
- 274
- 275 //音量
- 276 button = [UIButton buttonWithType:UIButtonTypeCustom];
- 277 button.frame = CGRectMake(10, 390, kBtnWidth, kBtnHeight);
- 278 [button addTarget:self action:@selector(showVolumeSlider) forControlEvents:UIControlEventTouchUpInside];
- 279 [button setImage:[UIImage imageNamed:@“labalan”] forState:UIControlStateNormal];
- 280 [button setImage:[UIImage imageNamed:@“laba”] forState:UIControlStateHighlighted];
- 281 [self.view addSubview:button];
- 282
- 283 self.volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(-80, 280, 220, 5)];
- 284 self.volumeSlider.minimumValue = 0;
- 285 self.volumeSlider.maximumValue = 1;
- 286 self.volumeSlider.hidden = YES;
- 287 self.volumeSlider.transform = CGAffineTransformMakeRotation(-95* M_PI/180);
- 288 [self.view addSubview:self.volumeSlider];
- 289 [self.volumeSlider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];
- 290
- 291
- 292
- 293
- 294 }
- 295
- 296 -(void) initLrc:(ZjMusic*)music
- 297 {
- 298 NSLog(@“%@ ,%@”,music.name,music.type);
- 299 self.timeArray = [[NSMutableArray alloc] init];
- 300 self.lrcDict = [[NSMutableDictionary alloc] init];
- 301
- 302 NSString *lrcPath = [[NSBundle mainBundle] pathForResource:music.name ofType:@“lrc”];
- 303 NSString *contentStr = [NSString stringWithContentsOfFile:lrcPath encoding:NSUTF8StringEncoding error:nil];
- 304
- 305
- 306 NSArray *array = [contentStr componentsSeparatedByString:@“\n”];
- 307 for (int i= 0; i<array.count; i ++) {
- 308 NSString *lineStr = array[i];
- 309 NSArray *lineArray = [lineStr componentsSeparatedByString:@“]”];
- 310 NSString *lrcStr = [lineArray objectAtIndex:1];
- 311 if([lineArray[0] length]>5){
- 312
- 313 NSString *timeStr = [lineArray[0] substringWithRange:NSMakeRange(1, 5)];
- 314
- 315 NSArray *timeArray = [timeStr componentsSeparatedByString:@“:”];
- 316
- 317 NSInteger timeInt = [timeArray[0] intValue]*60 + [timeArray[1] intValue];
- 318 NSString *timeTostr = [NSString stringWithFormat:@“%d”,timeInt];
- 319
- 320 [self.lrcDict setObject:lrcStr forKey:timeTostr];
- 321 [self.timeArray addObject:timeTostr];
- 322
- 323 }
- 324
- 325
- 326
- 327 }
- 328 // NSLog(@“%d”,self.timeArray.count);
- 329
- 330 }
- 331
- 332 @end
2. [图片] iOS 模拟器屏幕快照“2013-9-18 上午11.06.16”.png

3. [图片] iOS 模拟器屏幕快照“2013-9-18 上午10.58.17”.png
