本地音乐播放

#define viewW self.view.frame.size.width

#define viewH self.view.frame.size.height

@interface MusicViewController ()<AVAudioPlayerDelegate>

@property(nonatomic,strong)UILabel *startTimeLab;

@property(nonatomic,strong)UILabel *totalTimeLab;

@property(nonatomic,strong)NSMutableArray *musicNameList;

@property(nonatomic,assign)NSUInteger musicNumber;

@property(nonatomic,strong)UISlider *slider;

@property(nonatomic,strong)UIButton *onBtn;

@property(nonatomic,strong)UIButton *circularBtn;

@property(nonatomic,strong)UIButton *loveBtn;

@end

@implementation MusicViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.navigationController.navigationBar setTranslucent:NO];

    isPlay=YES;

    isCircle=YES;

    isLove=NO;

    

    

    NSString *imageName=[NSString stringWithFormat:@"zhl%d.png",2];

    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:imageName]];

    [self setupBtn];

    self.musicNameList=[[NSMutableArray alloc] initWithObjects:@"普通人",@"何以爱情",@"有一天我们都会老",@"中国山",@"天涯明月刀", nil];

    

    self.musicNumber=0;

    NSString *str=[[NSBundle mainBundle] pathForResource:_musicNameList[_musicNumber] ofType:@"mp3"];

    self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:str] error:nil];

    _player.delegate=self;

    [_player prepareToPlay];

   

    

    

}

-(void)setupBtn{

    UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, viewW, viewH)];

    lable.backgroundColor=[UIColor grayColor];

    lable.alpha=0.5;

    [self.view addSubview:lable];

    

    CGFloat sliderX=50;

    CGFloat sliderY=viewH-200;

    CGFloat sliderW=viewW-100;

    CGFloat sliderH=30;

    

    //进度滑块

    self.slider=[[UISlider alloc] initWithFrame:CGRectMake(sliderX, sliderY, sliderW, sliderH)];

    _slider.backgroundColor=[UIColor clearColor];

    _slider.value=0.0;

    _slider.minimumValue=0.0;

    _slider.maximumValue=1.0;

    [_slider setThumbImage:[UIImage imageNamed:@"sliderThumb_small.png"] forState:UIControlStateNormal];

    _slider.minimumTrackTintColor=[UIColor whiteColor];

    [_slider addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:_slider];

    //播放开始时间

    self.startTimeLab=[[UILabel alloc] initWithFrame:CGRectMake(5, sliderY, 40, 30)];

    _startTimeLab.font=[UIFont systemFontOfSize:10];

    _startTimeLab.textColor=[UIColor whiteColor];

    _startTimeLab.textAlignment=NSTextAlignmentRight;

   // _startTimeLab.backgroundColor=[UIColor whiteColor];

    [self.view addSubview:_startTimeLab];

    

    //总的播放时间

    self.totalTimeLab=[[UILabel alloc]initWithFrame:CGRectMake(sliderX+sliderW+5, sliderY, 40, 30)];

    _totalTimeLab.font=[UIFont systemFontOfSize:10];

    _totalTimeLab.textColor=[UIColor whiteColor];

   // _totalTimeLab.backgroundColor=[UIColor whiteColor];

    [self.view addSubview:_totalTimeLab];

    

//

    

    CGFloat onBtnX=_slider.center.x-20;

    CGFloat aboveBtnX=onBtnX-50;

    CGFloat nextBtnX=onBtnX+50;

    CGFloat loveBtnX=onBtnX+100;

    CGFloat circularBtnX=onBtnX-100;

    CGFloat circularBtnY=sliderY+40;

    CGFloat circularBtnW=30;

    CGFloat circularBtnH=30;

    

    

    //循环按钮

    

    self.circularBtn=[UIButton buttonWithType: UIButtonTypeCustom];

    _circularBtn.frame=CGRectMake(circularBtnX, circularBtnY, circularBtnW, circularBtnH);

    //circularBtn.backgroundColor=[UIColor whiteColor];

    [self setBtn:_circularBtn imageName:@"circleOpen.png" action:@selector(didClickCircular)];

    [self.view addSubview:_circularBtn];

    //上一曲

    UIButton *aboveBtn=[UIButton buttonWithType:UIButtonTypeCustom];

    

    

    aboveBtn.frame=CGRectMake(aboveBtnX, circularBtnY, circularBtnW, circularBtnH);

    [self setBtn:aboveBtn imageName:@"aboveMusic.png" action:@selector(didClickAbove)];

    [self.view addSubview:aboveBtn];

    

    //开始/暂停

    self.onBtn=[UIButton buttonWithType:UIButtonTypeCustom];

    

    _onBtn.frame=CGRectMake(onBtnX, circularBtnY, circularBtnW, circularBtnH);

   // [self setBtn:_onBtn imageName:@"play.png" action:@selector(didClickStartOrPause)];

    [_onBtn setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

    [_onBtn addTarget:self action:@selector(didClickStartOrPause) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_onBtn];

    

    //下一曲

    UIButton *nextBtn=[UIButton buttonWithType:UIButtonTypeCustom];

   

    nextBtn.frame=CGRectMake(nextBtnX, circularBtnY, circularBtnW, circularBtnH);

    [self setBtn:nextBtn imageName:@"nextMusic.png" action:@selector(didClickNext)];

    [self.view addSubview:nextBtn];

    

    //喜欢

    self.loveBtn=[UIButton buttonWithType:UIButtonTypeCustom];

    _loveBtn.frame=CGRectMake(loveBtnX, circularBtnY, circularBtnW, circularBtnH);

    [self setBtn:_loveBtn imageName:@"like.png" action:@selector(didClickLove)];

    [self.view addSubview:_loveBtn];

  

    //设置监控 每秒刷新一次时间

    [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(showTime) userInfo:nil repeats:YES];

    

}

//进度滑块

-(void)changeValue{

    _player.currentTime=_slider.value*_player.duration;

}

//是否循环播放

-(void)didClickCircular{

    if (isCircle) {

        _circularBtn.alpha=0.5;

        isCircle=NO;

    }else{

        _circularBtn.alpha=1.0;

        isCircle=YES;

    }

}

//上一首

-(void)didClickAbove{

    if (_musicNumber==0) {

        _musicNumber=_musicNameList.count;

    }

    _musicNumber--;

    [self updatePlayerSeting];

}

//下一首

-(void)didClickNext{

    if (_musicNumber==_musicNameList.count-1) {

        _musicNumber=-1;

        

    }

    _musicNumber++;

    [self updatePlayerSeting];

    

}

//开始/暂停

-(void)didClickStartOrPause{

    

    if (isPlay) {

        [_player play];

        [_onBtn setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];

        isPlay=NO;

    }else{

        [_player pause];

        [_onBtn setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

        isPlay=YES;

    

    }

}

//收藏

-(void)didClickLove{

    

    if (isLove) {

        [_loveBtn setBackgroundImage:[UIImage imageNamed:@"like-2.png"] forState:UIControlStateNormal];

        isLove=YES;

    }else{

    

        [_loveBtn setBackgroundImage:[UIImage imageNamed:@"like.png"] forState:UIControlStateNormal];

    }

}

//更新时间

-(void)showTime{

    //小于10秒,要有0;

    if ((int)_player.currentTime%60<10) {

        _startTimeLab.text=[NSString stringWithFormat:@"%d:0%d",(int)_player.currentTime/60,(int)_player.currentTime%60];

    }else{

    

    _startTimeLab.text=[NSString stringWithFormat:@"%d:%d",(int)_player.currentTime/60,(int)_player.currentTime%60];

    

    }

    if ((int)_player.duration%60<10) {

        _totalTimeLab.text=[NSString stringWithFormat:@"%d:0%d",(int)_player.duration/60,(int)_player.duration%60];

    }else{

        

        _totalTimeLab.text=[NSString stringWithFormat:@"%d:%d",(int)_player.duration/60,(int)_player.duration%60];

        

    }

    //进度

    self.slider.value=_player.currentTime/_player.duration;

    

    

    //自动播放下一首

    if (_slider.value>0.999) {

        [self autoPlayNext];

    }

}

//button封装方法

-(void)setBtn:(UIButton *)btn imageName:(NSString *)imageName action:(SEL)action{

    

    

    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

    

    

    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:btn];

    

}

//自动播放下一曲

-(void)autoPlayNext{

    //判断是否是循环播放

    if (isCircle==YES) {

        if (_musicNumber==_musicNameList.count-1) {

            _musicNumber=-1;

        }

        _musicNumber++;

        [self updatePlayerSeting];

    }else{

        [_onBtn setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

        isPlay=NO;

    

    }

}

//更新播放设置

-(void)updatePlayerSeting{

    //更新播放按钮状态

    [_onBtn setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];

    isPlay=NO;

    //更新歌曲

    NSString *filePath=[[NSBundle mainBundle]pathForResource:_musicNameList[_musicNumber] ofType:@"mp3"];

    NSURL *url=[NSURL fileURLWithPath:filePath];

    _player=[[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

    currentMusic=_musicNameList[_musicNumber];

    

    [_player play];

}

//摇一摇随机换歌

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{

    if (motion==UIEventSubtypeMotionShake) {

        _musicNumber=arc4random()%5;

    }

    [self updatePlayerSeting];

}

一个人,一片天,一条路,一瞬间!
原文地址:https://www.cnblogs.com/zcl410/p/4593996.html