IOS音乐播放器代码

(国内知名安卓开发论坛推荐:http://www.eoeandroid.com/

此代码是IOS一款播放器代码,学习时自己仿照主流播放器写的,该播放器支持各种格式播放,支持上一曲,下一曲,歌词同步播放,音量调节大小,快进,快退等功能,后续功能我会继续完善。代码仅供学习交流,如有写的不好,望各位海涵...希望对刚刚接触这块的童鞋有所帮助......

[html] view plain copy print ?
  1. 001 #import “ZJViewController.h”   
  2. 002 #import “ZjMusic.h”   
  3. 003     
  4. 004     
  5. 005 @interface ZJViewController ()<AVAudioPlayerDelegate,UITabBarDelegate,UITableViewDataSource>   
  6. 006     
  7. 007 @end   
  8. 008 #define kBtnHeight 50   
  9. 009 #define kBtnWidth 60   
  10. 010 @implementation ZJViewController   
  11. 011     
  12. 012 - (void)viewDidLoad   
  13. 013 {   
  14. 014     [super viewDidLoad];   
  15. 015     [self initView];   
  16. 016     [self initData];   
  17. 017         
  18. 018        
  19. 019         
  20. 020    // NSLog(@“%@”,self.lrcDict);   
  21. 021 }   
  22. 022 -(void)initData   
  23. 023 {   
  24. 024     ZjMusic *music1 = [[ZjMusic alloc] initWithName:@“Right Here Waiting(此情可待)” andType:@“mp3”];   
  25. 025     ZjMusic *music2 = [[ZjMusic alloc] initWithName:@“Beyond-真的爱你” andType:@“mp3”];   
  26. 026     ZjMusic *music3 = [[ZjMusic alloc] initWithName:@“刘德华-爱你一万年” andType:@“mp3”];   
  27. 027     ZjMusic *music4 = [[ZjMusic alloc] initWithName:@“毛宁-涛声依旧” andType:@“mp3”];   
  28. 028     ZjMusic *music5 = [[ZjMusic alloc] initWithName:@“你是我的眼” andType:@“mp3”];   
  29. 029     ZjMusic *music6 = [[ZjMusic alloc] initWithName:@“星星” andType:@“mp3”];   
  30. 030     ZjMusic *music7 = [[ZjMusic alloc] initWithName:@“月光爱人” andType:@“mp3”];   
  31. 031         
  32. 032         
  33. 033     self.musicData = [[NSMutableArray alloc] init];   
  34. 034         
  35. 035     [self.musicData addObject:music1];   
  36. 036     [self.musicData addObject:music2];   
  37. 037     [self.musicData addObject:music3];   
  38. 038     [self.musicData addObject:music4];   
  39. 039     [self.musicData addObject:music5];   
  40. 040     [self.musicData addObject:music6];   
  41. 041     [self.musicData addObject:music7];   
  42. 042     [self loadMusic:music5];   
  43. 043     [self initLrc:music5];   
  44. 044         
  45. 045     self.musicNameLabel.text = music5.name;   
  46. 046         
  47. 047         
  48. 048         
  49. 049 }   
  50. 050 #pragma mark 加载Music   
  51. 051 -(void)loadMusic:(ZjMusic*)music   
  52. 052 {   
  53. 053     NSString *path = [[NSBundle mainBundle] pathForResource:music.name ofType:music.type];   
  54. 054     NSURL *URL = [NSURL fileURLWithPath:path];   
  55. 055         
  56. 056     self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:nil];   
  57. 057     self.audioPlayer.delegate = self;   
  58. 058     self.audioPlayer.volume = 0.5;   
  59. 059     self.volumeSlider.value = self.audioPlayer.volume;   
  60. 060     [self.audioPlayer prepareToPlay];   
  61. 061         
  62. 062     [self returnTotalTime];   
  63. 063     
  64. 064 }   
  65. 065     
  66. 066 #pragma mark 音量slider自动移动,currentTime自动变换   
  67. 067 -(void)currentTimeChange   
  68. 068 {   
  69. 069     [self returnCurrentTime];   
  70. 070     self.durationlSlider.value = self.audioPlayer.currentTime/self.audioPlayer.duration;   
  71. 071     NSLog(@“curtime ---->%d”,(int)self.audioPlayer.currentTime);   
  72. 072         
  73. 073         
  74. 074    // static int index = 0;   
  75. 075         
  76. 076     NSString *key = [NSString stringWithFormat:@“%d”,(int)self.audioPlayer.currentTime];   
  77. 077     NSString *lrc = [self.lrcDict objectForKey:key];   
  78. 078         
  79. 079         
  80. 080         
  81. 081     if(lrc){   
  82. 082 //        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];   
  83. 083 //        [self.lrcTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];   
  84. 084 //        index++;   
  85. 085         self.musicLrcLable.text = lrc;   
  86. 086         NSLog(@“index--->%@”,lrc);   
  87. 087     
  88. 088     }   
  89. 089          
  90. 090            
  91. 091 }   
  92. 092     
  93. 093 #pragma mark 上一首   
  94. 094 -(void)previousSound   
  95. 095 {   
  96. 096     BOOL playFlag;   
  97. 097     if(self.audioPlayer.playing){   
  98. 098         playFlag = YES;   
  99. 099         [self.audioPlayer stop];   
  100. 100     }else{   
  101. 101         playFlag = NO;   
  102. 102     }   
  103. 103     _soundIndex --;   
  104. 104     if(self.soundIndex<0){   
  105. 105         self.soundIndex  = self.musicData.count-1;   
  106. 106     }   
  107. 107     ZjMusic *music = self.musicData[_soundIndex];   
  108. 108     [self initLrc:music];   
  109. 109     self.musicNameLabel.text = music.name;   
  110. 110     [self loadMusic:music];   
  111. 111         
  112. 112     if(playFlag){   
  113. 113         [self.audioPlayer play];   
  114. 114     }   
  115. 115 }   
  116. 116 #pragma mark 下一曲   
  117. 117 -(void)nextSound   
  118. 118 {   
  119. 119     BOOL playFlag;   
  120. 120     if(self.audioPlayer.playing){   
  121. 121         playFlag = YES;   
  122. 122         [self.audioPlayer stop];   
  123. 123     }else{   
  124. 124         playFlag = NO;   
  125. 125     }   
  126. 126     _soundIndex ++;   
  127. 127     if(self.soundIndex>self.musicData.count-1){   
  128. 128         self.soundIndex = 0;   
  129. 129     }   
  130. 130     ZjMusic *music = self.musicData[_soundIndex];   
  131. 131     [self initLrc:music];   
  132. 132     self.musicNameLabel.text = music.name;   
  133. 133     [self loadMusic:music];   
  134. 134         
  135. 135     if(playFlag){   
  136. 136         [self.audioPlayer play];   
  137. 137     }   
  138. 138 }   
  139. 139 #pragma mark 音量显示   
  140. 140 -(void)showVolumeSlider   
  141. 141 {   
  142. 142     if(self.volumeSlider.hidden == NO){   
  143. 143         self.volumeSlider.hidden = YES;   
  144. 144     }else{   
  145. 145         self.volumeSlider.hidden = NO;   
  146. 146     }   
  147. 147     [self performSelector:@selector(hiddenVolum) withObject:nil afterDelay:5];   
  148. 148 }   
  149. 149 #pragma mark 音量隐藏   
  150. 150 -(void)hiddenVolum   
  151. 151 {   
  152. 152     self.volumeSlider.hidden = YES;   
  153. 153         
  154. 154     
  155. 155 }   
  156. 156 #pragma mark 播放暂停   
  157. 157 -(void)palyAndPause:(UIButton*)button   
  158. 158 {   
  159. 159     NSString *imageName = @“play”;   
  160. 160     if(self.audioPlayer.playing){   
  161. 161         [self.audioPlayer pause];   
  162. 162         imageName = @“play”;   
  163. 163             
  164. 164         [self.timer invalidate];   
  165. 165            
  166. 166     }else{   
  167. 167        [self.audioPlayer play];   
  168. 168         imageName = @“stop”;   
  169. 169             
  170. 170         self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(currentTimeChange) userInfo:nil repeats:YES];   
  171. 171     }   
  172. 172         
  173. 173     [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];   
  174. 174         
  175. 175     [self returnTotalTime];   
  176. 176 }   
  177. 177 -(void)returnTotalTime   
  178. 178 {   
  179. 179     NSString *totalTime = [NSString stringWithFormat:@“%d:%d”,(int)self.audioPlayer.duration/60,(int)self.audioPlayer.duration%60];   
  180. 180     self.totalTime.text = totalTime;   
  181. 181 }   
  182. 182 #pragma mark 调节音量   
  183. 183 -(void)volumeChange   
  184. 184 {   
  185. 185     self.audioPlayer.volume = self.volumeSlider.value;   
  186. 186     [self returnCurrentTime];   
  187. 187 }   
  188. 188     
  189. 189 -(void)returnCurrentTime   
  190. 190 {   
  191. 191     NSString *currentTime = [NSString stringWithFormat:@“%d:%d”,(int)self.audioPlayer.currentTime/60,(int)self.audioPlayer.currentTime%60];   
  192. 192     if((int)self.audioPlayer.currentTime%60<10){   
  193. 193         currentTime = [NSString stringWithFormat:@“%d:0%d”,(int)self.audioPlayer.currentTime/60,(int)self.audioPlayer.currentTime%60];   
  194. 194     }   
  195. 195     self.currentTimeLabel.text = currentTime;   
  196. 196         
  197. 197         
  198. 198 }   
  199. 199     
  200. 200 #pragma mark 调节时间   
  201. 201 -(void)durationChange   
  202. 202 {   
  203. 203     self.audioPlayer.currentTime = self.durationlSlider.value*self.audioPlayer.duration;   
  204. 204 }   
  205. 205     
  206. 206     
  207. 207 - (void)dealloc {   
  208. 208     [_audioPlayer release];   
  209. 209     [_durationlSlider release];   
  210. 210     [_volumeSlider release];   
  211. 211     [_musicData release];   
  212. 212     [_currentTimeLabel release];   
  213. 213     [_musicNameLabel release];   
  214. 214     [_totalTime release];   
  215. 215     [_lrcDict release];   
  216. 216     [_timeArray release];   
  217. 217     [_lrcTableView release];   
  218. 218     [super dealloc];   
  219. 219 }   
  220. 220 #pragma mark 初始化数据   
  221. 221 -(void)initView   
  222. 222 {   
  223. 223     //当前时间   
  224. 224     self.currentTimeLabel  = [[UILabel alloc] initWithFrame:CGRectMake(20, 35, 40, 20)];   
  225. 225     self.currentTimeLabel.text = @“0:00”;   
  226. 226     self.currentTimeLabel.backgroundColor = [UIColor clearColor];   
  227. 227     [self.view addSubview:self.currentTimeLabel];   
  228. 228         
  229. 229     //持续时间   
  230. 230     self.durationlSlider = [[UISlider alloc] initWithFrame:CGRectMake(65, 30, 190, 30)];   
  231. 231     [self.durationlSlider addTarget:self action:@selector(durationChange) forControlEvents:UIControlEventValueChanged];   
  232. 232     [self.view addSubview:self.durationlSlider];   
  233. 233         
  234. 234     //总时间   
  235. 235     self.totalTime  = [[UILabel alloc] initWithFrame:CGRectMake(265, 35, 40, 20)];   
  236. 236     self.totalTime.text = @“0:00”;   
  237. 237     self.totalTime.backgroundColor = [UIColor clearColor];   
  238. 238     [self.view addSubview:self.totalTime];   
  239. 239         
  240. 240     UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(50, 250, 220, 60)];   
  241. 241     [self.view addSubview:subView];   
  242. 242     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];   
  243. 243     //上一曲   
  244. 244     button.frame = CGRectMake(0, 0, kBtnWidth, kBtnHeight);   
  245. 245     [button setImage:[UIImage imageNamed:@“left”] forState:UIControlStateNormal];   
  246. 246     [button addTarget:self action:@selector(previousSound) forControlEvents:UIControlEventTouchUpInside];   
  247. 247     [subView addSubview:button];   
  248. 248         
  249. 249     //播放暂停   
  250. 250     button = [UIButton buttonWithType:UIButtonTypeCustom];   
  251. 251     button.frame = CGRectMake(80, 0,  kBtnWidth, kBtnHeight);   
  252. 252     [button setImage:[UIImage imageNamed:@“play”] forState:UIControlStateNormal];   
  253. 253     [button addTarget:self action:@selector(palyAndPause:) forControlEvents:UIControlEventTouchUpInside];   
  254. 254     [subView addSubview:button];   
  255. 255         
  256. 256     //下一曲   
  257. 257     button = [UIButton buttonWithType:UIButtonTypeCustom];   
  258. 258     button.frame = CGRectMake(160, 0,  kBtnWidth, kBtnHeight);   
  259. 259     [button addTarget:self action:@selector(nextSound) forControlEvents:UIControlEventTouchUpInside];   
  260. 260     [button setImage:[UIImage imageNamed:@“right”] forState:UIControlStateNormal];   
  261. 261     [subView addSubview:button];   
  262. 262         
  263. 263     //显示歌ci   
  264. 264     self.musicLrcLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, 320, 44)];   
  265. 265     self.musicLrcLable.textAlignment = NSTextAlignmentCenter;   
  266. 266     self.musicLrcLable.backgroundColor = [UIColor greenColor];   
  267. 267     [self.view addSubview:self.musicLrcLable];   
  268. 268         
  269. 269     //显示歌名   
  270. 270     self.musicNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 40)];   
  271. 271     self.musicNameLabel.backgroundColor = [UIColor clearColor];   
  272. 272     self.musicNameLabel.textAlignment = NSTextAlignmentCenter;   
  273. 273     [self.view addSubview:self.musicNameLabel];   
  274. 274           
  275. 275     //音量   
  276. 276     button = [UIButton buttonWithType:UIButtonTypeCustom];   
  277. 277     button.frame = CGRectMake(10, 390,  kBtnWidth, kBtnHeight);   
  278. 278     [button addTarget:self action:@selector(showVolumeSlider) forControlEvents:UIControlEventTouchUpInside];   
  279. 279     [button setImage:[UIImage imageNamed:@“labalan”] forState:UIControlStateNormal];   
  280. 280     [button setImage:[UIImage imageNamed:@“laba”] forState:UIControlStateHighlighted];   
  281. 281     [self.view addSubview:button];   
  282. 282         
  283. 283     self.volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(-80, 280, 220, 5)];   
  284. 284     self.volumeSlider.minimumValue = 0;   
  285. 285     self.volumeSlider.maximumValue = 1;   
  286. 286     self.volumeSlider.hidden = YES;   
  287. 287     self.volumeSlider.transform = CGAffineTransformMakeRotation(-95* M_PI/180);   
  288. 288     [self.view addSubview:self.volumeSlider];   
  289. 289     [self.volumeSlider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];   
  290. 290         
  291. 291         
  292. 292         
  293. 293     
  294. 294 }   
  295. 295     
  296. 296 -(void) initLrc:(ZjMusic*)music   
  297. 297 {   
  298. 298     NSLog(@“%@ ,%@”,music.name,music.type);   
  299. 299     self.timeArray = [[NSMutableArray alloc] init];   
  300. 300     self.lrcDict = [[NSMutableDictionary alloc] init];   
  301. 301         
  302. 302     NSString *lrcPath = [[NSBundle mainBundle] pathForResource:music.name ofType:@“lrc”];   
  303. 303     NSString *contentStr = [NSString stringWithContentsOfFile:lrcPath encoding:NSUTF8StringEncoding error:nil];   
  304. 304         
  305. 305         
  306. 306     NSArray *array = [contentStr componentsSeparatedByString:@“\n”];   
  307. 307     for (int i0; i<array.count; i ++) {   
  308. 308         NSString *lineStr = array[i];   
  309. 309         NSArray *lineArray = [lineStr componentsSeparatedByString:@“]”];   
  310. 310         NSString *lrcStr = [lineArray objectAtIndex:1];   
  311. 311         if([lineArray[0] length]>5){   
  312. 312                 
  313. 313             NSString *timeStr = [lineArray[0] substringWithRange:NSMakeRange(1, 5)];   
  314. 314                 
  315. 315             NSArray *timeArray = [timeStr componentsSeparatedByString:@“:”];   
  316. 316                           
  317. 317             NSInteger timeInt = [timeArray[0] intValue]*60 + [timeArray[1] intValue];   
  318. 318             NSString *timeTostr = [NSString stringWithFormat:@“%d”,timeInt];   
  319. 319                 
  320. 320             [self.lrcDict setObject:lrcStr forKey:timeTostr];   
  321. 321             [self.timeArray addObject:timeTostr];   
  322. 322                 
  323. 323         }   
  324. 324         
  325. 325             
  326. 326         
  327. 327     }   
  328. 328    // NSLog(@“%d”,self.timeArray.count);   
  329. 329         
  330. 330 }   
  331. 331     
  332. 332 @end   


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

IOS音乐播放器代码_第1张图片

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

IOS音乐播放器代码_第2张图片

你可能感兴趣的:(IOS音乐播放器代码)