码迷,mamicode.com
首页 > 其他好文 > 详细

音乐播放器

时间:2014-07-19 09:11:12      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   文件   

bubuko.com,布布扣
//
// 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

音乐播放器,布布扣,bubuko.com

音乐播放器

标签:style   blog   http   color   os   文件   

原文地址:http://www.cnblogs.com/lidongq/p/3854340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!