音乐播放器

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

#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