音乐播放器

音乐播放器
//
// ViewController.m // 播放器 // // Created by apple on 14-7-18. // Copyright (c) 2014年 apple. All rights reserved. // #import "ViewController.h" #import "AVFoundation/AVFoundation.h" @interface ViewController ()<AVAudioPlayerDelegate> { AVAudioPlayer *player;//播放器 NSArray *_musiclist;//歌曲名列表 NSTimer *_timer;//定时器检查播放进度的动态送给进度tiao int _index;//歌曲的多少; } //播放进度的显示 @property IBOutlet UISlider *progressSlider; @end @implementation ViewController //make button - (void)chuangJianBtn { UIButton *start=[UIButton buttonWithType:UIButtonTypeCustom]; start.frame=CGRectMake(100, 300, 100, 30); [start setTitle:@"开/停" forState:UIControlStateNormal]; [start setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [start addTarget:self action:@selector(kaiOrStop) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:start]; UIButton *nextBtn=[UIButton buttonWithType:UIButtonTypeCustom]; nextBtn.frame=CGRectMake(210, 300, 100, 30); [nextBtn setTitle:@"next" forState:UIControlStateNormal]; [nextBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [nextBtn addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:nextBtn]; UIButton *beforeBtn=[UIButton buttonWithType:UIButtonTypeCustom]; beforeBtn.frame=CGRectMake(10, 300, 100, 30); [beforeBtn setTitle:@"before" forState:UIControlStateNormal]; [beforeBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [beforeBtn addTarget:self action:@selector(before:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:beforeBtn]; //[self buttonRect:CGRectMake(210, 300, 100, 30) Title:@"next" TitleColor:[UIColor redColor] Action:@selector(next:)]; //[self buttonRect:CGRectMake(0, 300, 100, 30) Title:@"before" TitleColor:[UIColor redColor] Action:@selector(before:)]; } //下一首按钮 - (void)next:(UIButton *)sender { if (_index==_musiclist.count) { _index=0; } else { _index++; } [self play]; NSLog(@"下一首"); } - (void)before:(UIButton *)sender { if (_index==0) { _index=2; } else { _index--; } [self play]; NSLog(@"上一首"); } //按钮封装 - (UIButton *)buttonRect:(CGRect)rect Title:(NSString *)title TitleColor:(UIColor *)titleColor Action:(SEL)action { UIButton *start=[UIButton buttonWithType:UIButtonTypeCustom]; start.frame=rect; [start setTitle:title forState:UIControlStateNormal]; [start setTitleColor: titleColor forState:UIControlStateNormal]; [start addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:start]; return start; } //创建UISlider -(void)chuangJianSlider { UISlider *shengYinSlider=[[UISlider alloc]initWithFrame:CGRectMake(20, 200, 250, 10)]; shengYinSlider.value=0.5; shengYinSlider.tintColor=[UIColor blueColor]; shengYinSlider.maximumValue=1; shengYinSlider.minimumValue=0; [shengYinSlider addTarget:self action:@selector(shengYin:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:shengYinSlider]; _progressSlider=[[UISlider alloc]initWithFrame:CGRectMake(20, 100, 250, 10)]; _progressSlider.value=0.5; _progressSlider.tintColor=[UIColor blueColor]; _progressSlider.maximumValue=1; _progressSlider.minimumValue=0; [_progressSlider addTarget:self action:@selector(progressSlider:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_progressSlider]; } //声音slider的动作fangf -(void)shengYin:(UISlider *)sender { player.volume=sender.value; NSLog(@"%f",sender.value); } //播放进度调节显示 - (void)progressSlider:(UISlider *)sender { player.currentTime=sender.value*player.duration; } //播放完成代理 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { _index++; if (_index==_musiclist.count) { _index=0; } [self play]; } //开始停止按钮 - (void)kaiOrStop { if(player.playing) { [player stop]; } else { [player play]; } NSLog(@"kaiShiTingZhi"); } //开始播放设置 - (void)play { NSString *name=_musiclist[_index]; NSString *path=[[NSBundle mainBundle] pathForResource:name ofType:@"mp3"]; // NSData *avData=[NSData dataWithContentsOfFile:path]; // player=[[AVAudioPlayer alloc]initWithData:avData error:nil]; //生成一个urld的对象 NSURL *url=[NSURL fileURLWithPath:path]; NSData *data=[NSData dataWithContentsOfURL:url]; //把本地文件给播放器 player=[[AVAudioPlayer alloc]initWithData:data error:nil]; player.delegate=self; //改变速率 //player.enableRate=YES; player.rate=4; //设置开始的时间 player.currentTime=100; player.volume=0.5; [player prepareToPlay]; //开始播放 [player play]; //定时器来调用progress方法 _timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(progress:) userInfo:nil repeats:YES]; } //改变进度条 - (void)progress:(UISlider *)sender { self.progressSlider.value=player.currentTime/player.duration; } - (void)viewDidLoad { [super viewDidLoad]; [self chuangJianBtn]; [self chuangJianSlider]; _musiclist=@[@"Beyond-光辉岁月",@"G.E.M.邓紫棋-你把我灌醉",@"G.E.M.邓紫棋-回忆的沙漏 (国)"]; [self play]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

你可能感兴趣的:(播放器)