音乐播放器

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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