RFRadioView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // RFRadioView.m
  3. // RadioFree
  4. //
  5. //
  6. #import "RFRadioView.h"
  7. #import "FSPlaylistItem.h"
  8. #import "UIView+Additions.h"
  9. #import "Globle.h"
  10. #import "FMLrcView.h"
  11. #import "FMSongListModel.h"
  12. #import "FMSongModel.h"
  13. #import "MCDataEngine.h"
  14. #import "UIImageView+WebCache.h"
  15. #import "FMPAImageView.h"
  16. #import "FSAudioStream.h"
  17. #import "ProgressHUD.h"
  18. @implementation RFRadioView
  19. @synthesize delegate = _delegate;
  20. @synthesize selectedPlaylistItem = _selectedPlaylistItem;
  21. @synthesize downLoadButton;
  22. - (id)initWithFrame:(CGRect)frame
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. // Initialization code
  27. self.backgroundColor = [UIColor whiteColor];//[UIColor colorWithRed:232.f/255.f green:184.f/255.f blue:10.f/255.f alpha:1.f];
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackgroundControlRadioStatus:) name:FMRFRadioViewStatusNotifiation object:nil];
  29. // audioController = [[FSAudioController alloc] init];
  30. audioPlayer = [[AudioPlayer alloc] init];
  31. audioPlayer.delegate = self;
  32. _currentPlaybackTime = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 50, 25)];
  33. _currentPlaybackTime.font =[UIFont boldSystemFontOfSize:14.0f];
  34. _currentPlaybackTime.textAlignment = NSTextAlignmentCenter;
  35. _currentPlaybackTime.textColor = [UIColor blackColor];
  36. _currentPlaybackTime.text = @"00:00";
  37. [self addSubview:_currentPlaybackTime];
  38. progress = [[UISlider alloc] initWithFrame:CGRectMake(_currentPlaybackTime.size.width+_currentPlaybackTime.origin.x, 18,self.size.width-110, 20)];
  39. progress.continuous = YES;
  40. progress.minimumTrackTintColor = [UIColor colorWithRed:244.0f/255.0f green:147.0f/255.0f blue:23.0f/255.0f alpha:1.0f];
  41. progress.maximumTrackTintColor = [UIColor lightGrayColor];
  42. [progress setThumbImage:[UIImage imageNamed:@"player-progress-point-h"] forState:UIControlStateNormal];
  43. // [progress setThumbImage:nil forState:UIControlStateNormal];
  44. [progress addTarget:self action:@selector(seek) forControlEvents:UIControlEventValueChanged];
  45. [self addSubview:progress];
  46. _totalPlaybackTime = [[UILabel alloc] initWithFrame:CGRectMake(progress.size.width+progress.origin.x, 5, 50, 25)];
  47. _totalPlaybackTime.font =[UIFont boldSystemFontOfSize:14.0f];
  48. _totalPlaybackTime.textAlignment = NSTextAlignmentCenter;
  49. _totalPlaybackTime.textColor = [UIColor blackColor];
  50. _totalPlaybackTime.text = @"00:00";
  51. [self addSubview:_totalPlaybackTime];
  52. lrcView = [[FMLrcView alloc] initWithFrame:CGRectMake(0, progress.size.height+progress.origin.y+10, self.size.width, self.size.height-100)];
  53. [self addSubview:lrcView];
  54. backImageView = [[FMPAImageView alloc] initWithFrame:CGRectMake(frame.size.width/2-64/2, frame.size.height-64-10, 64, 64)];
  55. [self addSubview:backImageView];
  56. _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
  57. _playButton.frame = CGRectMake(frame.size.width/2-64/2, frame.size.height-64-10, 64, 64);
  58. [_playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
  59. [_playButton setImage:[UIImage imageNamed:@"playHight.png"] forState:UIControlStateHighlighted];
  60. [_playButton addTarget:self action:@selector(playButtonEvent) forControlEvents:UIControlEventTouchUpInside];
  61. [self addSubview:_playButton];
  62. _preButton = [UIButton buttonWithType:UIButtonTypeCustom];
  63. _preButton.frame = CGRectMake(_playButton.origin.x-60, _playButton.origin.y+8, 48, 48);
  64. [_preButton setImage:[UIImage imageNamed:@"preSong.png"] forState:UIControlStateNormal];
  65. [_preButton addTarget:self action:@selector(preButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
  66. [self addSubview:_preButton];
  67. _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
  68. _nextButton.frame = CGRectMake(_playButton.origin.x+70, _playButton.origin.y+8, 48, 48);
  69. [_nextButton setImage:[UIImage imageNamed:@"nextSong.png"] forState:UIControlStateNormal];
  70. [_nextButton addTarget:self action:@selector(nextButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
  71. [self addSubview:_nextButton];
  72. _playbackButton = [UIButton buttonWithType:UIButtonTypeCustom];
  73. _playbackButton.frame = CGRectMake(5, _preButton.origin.y, 48, 48);
  74. [_playbackButton setImage:[UIImage imageNamed:@"order.png"] forState:UIControlStateNormal];
  75. [_playbackButton addTarget:self action:@selector(playBackButtonEvent) forControlEvents:UIControlEventTouchUpInside];
  76. [self addSubview:_playbackButton];
  77. isPlayBack = 0;
  78. _playListButton = [UIButton buttonWithType:UIButtonTypeCustom];
  79. _playListButton.frame = CGRectMake(self.size.width-48-5, _preButton.origin.y, 48, 48);
  80. [_playListButton setImage:[UIImage imageNamed:@"playList.png"] forState:UIControlStateNormal];
  81. [_playListButton addTarget:self action:@selector(playListButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
  82. [self addSubview:_playListButton];
  83. _collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
  84. _collectButton.frame = CGRectMake(_currentPlaybackTime.origin.x, _currentPlaybackTime.origin.y+_currentPlaybackTime.size.height, 48, 48);
  85. [_collectButton setImage:[UIImage imageNamed:@"collect"] forState:UIControlStateNormal];
  86. [_collectButton addTarget:self action:@selector(collectButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
  87. [self addSubview:_collectButton];
  88. self.downLoadButton = [UIButton buttonWithType:UIButtonTypeCustom];
  89. self.downLoadButton.frame = CGRectMake(_totalPlaybackTime.origin.x, _totalPlaybackTime.origin.y+_totalPlaybackTime.size.height, 48, 48);
  90. self.downLoadButton.enabled = NO;
  91. [self.downLoadButton setImage:[UIImage imageNamed:@"downLoad"] forState:UIControlStateNormal];
  92. [self.downLoadButton addTarget:self action:@selector(downLoadButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
  93. [self addSubview:self.downLoadButton];
  94. noLrcLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height/2-25/2, frame.size.width, 25)];
  95. noLrcLabel.textAlignment = NSTextAlignmentCenter;
  96. noLrcLabel.textColor =[UIColor colorWithRed:192.0f/255.0f green:37.0f/255.0f blue:62.0f/255.0f alpha:1.0f];
  97. [self addSubview:noLrcLabel];
  98. noLrcLabel.font = [UIFont boldSystemFontOfSize:14.0f];
  99. }
  100. return self;
  101. }
  102. -(void)setOnly:(BOOL)only{
  103. _preButton.hidden = only;
  104. _nextButton.hidden = only;
  105. _playbackButton.hidden = only;
  106. _playListButton.hidden = only;
  107. }
  108. -(void)setRadioViewLrc
  109. {
  110. [backImageView setImageWithURL:[NSURL URLWithString:_songModel.songPicBig]];
  111. if ([_songModel.lrcLink length]!=0) {
  112. noLrcLabel.text = @"";
  113. NSFileManager * manager = [[NSFileManager alloc] init];
  114. NSString *filePath = [DocumentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%lld/%lld/%lld.lrc",_songlistModel.ting_uid,_songlistModel.song_id,_songlistModel.song_id]];
  115. if ([manager fileExistsAtPath:filePath]) {
  116. [lrcView setLrcSourcePath:filePath];
  117. }else{
  118. MCDataEngine * network = [MCDataEngine new];
  119. [network getSongLrcWith:_songlistModel.ting_uid :_songlistModel.song_id :_songModel.lrcLink WithCompletionHandler:^(BOOL sucess, NSString *path) {
  120. if (sucess) {
  121. [lrcView setLrcSourcePath:path];
  122. }
  123. } errorHandler:^(NSError *error) {
  124. }];
  125. }
  126. isLrc = YES;
  127. }else{
  128. noLrcLabel.text = @"无歌词";
  129. [lrcView scrollViewClearSubView];
  130. [lrcView selfClearKeyAndTitle];
  131. isLrc = NO;
  132. }
  133. }
  134. -(void)playButtonEvent
  135. {
  136. if (isPlaying) {
  137. isPlaying = NO;
  138. }else{
  139. isPlaying = YES;
  140. }
  141. if (audioPlayer.state == AudioPlayerStatePaused){
  142. [audioPlayer resume];
  143. [self startTimer];
  144. backImageView.layer.speed = 0.2;
  145. CFTimeInterval pausedTime = [backImageView.layer timeOffset];
  146. CFTimeInterval timeSincePause = [backImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
  147. backImageView.layer.beginTime = timeSincePause;
  148. }
  149. else{
  150. [audioPlayer pause];
  151. [_progressUpdateTimer invalidate];
  152. CFTimeInterval pausedTime = [backImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
  153. backImageView.layer.speed = 0.0;
  154. backImageView.layer.timeOffset = pausedTime;
  155. }
  156. [_playButton setImage:isPlaying?[UIImage imageNamed:@"pasue.png"]:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
  157. [_playButton setImage:isPlaying?[UIImage imageNamed:@"pasueHight.png"]:[UIImage imageNamed:@"playHight.png"] forState:UIControlStateHighlighted];
  158. }
  159. -(void)playBackButtonEvent
  160. {
  161. isPlayBack+=1;
  162. NSString * name = nil;
  163. NSString * title = nil;
  164. switch (isPlayBack) {
  165. case 0:
  166. name = @"order.png";
  167. title = @"顺序播放";
  168. break;
  169. case 1:
  170. name = @"random.png";
  171. title = @"随机播放";
  172. break;
  173. case 2:
  174. name = @"lock.png";
  175. title = @"单曲播放";
  176. isPlayBack=-1;
  177. break;
  178. default:
  179. break;
  180. }
  181. [_playbackButton setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
  182. [ProgressHUD showSuccess:title];
  183. }
  184. -(void)preButtonEvent:(UIButton *)btn
  185. {
  186. [self stopEverything];
  187. [_delegate radioView:self preSwitchMusic:btn];
  188. }
  189. -(void)nextButtonEvent:(UIButton *)btn
  190. {
  191. [self stopEverything];
  192. [_delegate radioView:self nextSwitchMusic:btn];
  193. }
  194. -(void)playListButtonEvent:(UIButton *)btn
  195. {
  196. [_delegate radioView:self playListButton:btn];
  197. }
  198. -(void)downLoadButtonEvent:(UIButton *)btn
  199. {
  200. self.downLoadButton.enabled = NO;
  201. [_delegate radioView:self downLoadButton:btn];
  202. }
  203. -(void)collectButtonEvent:(UIButton *)btn
  204. {
  205. [_delegate radioView:self collectButton:btn];
  206. }
  207. -(void)startTimer
  208. {
  209. _progressUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
  210. target:self
  211. selector:@selector(updatePlaybackProgress)
  212. userInfo:nil
  213. repeats:YES];
  214. [[NSRunLoop currentRunLoop] addTimer:_progressUpdateTimer forMode:NSRunLoopCommonModes];
  215. }
  216. -(void)startMusic
  217. {
  218. [audioPlayer play:[NSURL URLWithString:self.selectedPlaylistItem.url]];
  219. CABasicAnimation *monkeyAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  220. monkeyAnimation.toValue = [NSNumber numberWithFloat:2.0 *M_PI];
  221. monkeyAnimation.duration = 1.5f;
  222. monkeyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  223. monkeyAnimation.cumulative = NO;
  224. monkeyAnimation.removedOnCompletion = NO; //No Remove
  225. monkeyAnimation.repeatCount = FLT_MAX;
  226. [backImageView.layer addAnimation:monkeyAnimation forKey:@"AnimatedKey"];
  227. backImageView.layer.speed = 0.2;
  228. backImageView.layer.beginTime = 0.0;
  229. }
  230. -(NSString*)TimeformatFromSeconds:(int)seconds
  231. {
  232. int totalm = seconds/(60);
  233. int h = totalm/(60);
  234. int m = totalm%(60);
  235. int s = seconds%(60);
  236. if (h==0) {
  237. return [NSString stringWithFormat:@"%02d:%02d", m, s];
  238. }
  239. return [NSString stringWithFormat:@"%02d:%02d:%02d", h, m, s];
  240. }
  241. -(void)stopEverything
  242. {
  243. self.downLoadButton.enabled = NO;
  244. isPlaying = NO;
  245. [audioPlayer stop];
  246. [_progressUpdateTimer invalidate];
  247. CFTimeInterval pausedTime = [backImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
  248. backImageView.layer.speed = 0.0;
  249. backImageView.layer.timeOffset = pausedTime;
  250. }
  251. - (void)setSelectedPlaylistItem:(FSPlaylistItem *)selectedPlaylistItem {
  252. if (_selectedPlaylistItem != selectedPlaylistItem) {
  253. _selectedPlaylistItem = selectedPlaylistItem;
  254. [self stopEverything];
  255. NSFileManager * manager = [[NSFileManager alloc] init];
  256. NSString *filePath = [DocumentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%lld/%lld/%lld.mp3",_songlistModel.ting_uid,_songlistModel.song_id,_songlistModel.song_id]];
  257. if ([manager fileExistsAtPath:filePath]) {
  258. _selectedPlaylistItem.url = [@"file://" stringByAppendingString:filePath];
  259. self.downLoadButton.enabled = NO;
  260. }else{
  261. self.downLoadButton.enabled = YES;
  262. }
  263. _totalPlaybackTime.text = [self TimeformatFromSeconds:_songModel.time];
  264. [self startMusic];
  265. [self startTimer];
  266. }
  267. }
  268. - (FSPlaylistItem *)selectedPlaylistItem {
  269. return _selectedPlaylistItem;
  270. }
  271. -(void)applicationDidEnterBackgroundControlRadioStatus:(NSNotification *)notification
  272. {
  273. NSDictionary *dict = [notification userInfo];
  274. NSString * string = [dict objectForKey:@"keyStatus"];
  275. if ([string isEqualToString:@"UIEventSubtypeRemoteControlPause"]) {
  276. [self playButtonEvent];
  277. }else if ([string isEqualToString:@"UIEventSubtypeRemoteControlPlay"]){
  278. [self playButtonEvent];
  279. }else if ([string isEqualToString:@"UIEventSubtypeRemoteControlPreviousTrack"]){
  280. [self preButtonEvent:_preButton];
  281. }else if ([string isEqualToString:@"UIEventSubtypeRemoteControlNextTrack"]){
  282. [self nextButtonEvent:_nextButton];
  283. }
  284. }
  285. -(void) seek
  286. {
  287. if (!audioPlayer)
  288. {
  289. return;
  290. }
  291. NSLog(@"Slider Changed: %f", progress.value);
  292. [audioPlayer seekToTime:progress.value];
  293. }
  294. -(void) updateControls
  295. {
  296. if (audioPlayer.state == AudioPlayerStateStopped) {
  297. if (isPlaying) {
  298. [_delegate radioView:self musicStop:isPlayBack];
  299. }
  300. isPlaying = NO;
  301. }else if (audioPlayer.state == AudioPlayerStateReady){
  302. }else if (audioPlayer.state == AudioPlayerStateRunning){
  303. }else if (audioPlayer.state == AudioPlayerStatePlaying){
  304. isPlaying = YES;
  305. }else if (audioPlayer.state == AudioPlayerStateError){
  306. }
  307. [_playButton setImage:isPlaying?[UIImage imageNamed:@"pasue.png"]:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
  308. [_playButton setImage:isPlaying?[UIImage imageNamed:@"pasueHight.png"]:[UIImage imageNamed:@"playHight.png"] forState:UIControlStateHighlighted];
  309. }
  310. -(void) updatePlaybackProgress
  311. {
  312. if (!audioPlayer || audioPlayer.duration == 0){
  313. progress.value = 0;
  314. return;
  315. }
  316. progress.minimumValue = 0;
  317. progress.maximumValue = audioPlayer.duration;
  318. progress.value = audioPlayer.progress;
  319. _currentPlaybackTime.text =[self TimeformatFromSeconds:audioPlayer.progress];
  320. if (isLrc) {
  321. [lrcView scrollViewMoveLabelWith:_currentPlaybackTime.text];
  322. }
  323. }
  324. -(void) audioPlayer:(AudioPlayer*)audioPlayer stateChanged:(AudioPlayerState)state
  325. {
  326. [self updateControls];
  327. }
  328. -(void) audioPlayer:(AudioPlayer*)audioPlayer didEncounterError:(AudioPlayerErrorCode)errorCode
  329. {
  330. [self updateControls];
  331. }
  332. -(void) audioPlayer:(AudioPlayer*)audioPlayer didStartPlayingQueueItemId:(NSObject*)queueItemId
  333. {
  334. [self updateControls];
  335. }
  336. -(void) audioPlayer:(AudioPlayer*)audioPlayer didFinishBufferingSourceWithQueueItemId:(NSObject*)queueItemId
  337. {
  338. [self updateControls];
  339. }
  340. -(void) audioPlayer:(AudioPlayer*)audioPlayer didFinishPlayingQueueItemId:(NSObject*)queueItemId withReason:(AudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration
  341. {
  342. [self updateControls];
  343. }
  344. @end