首页 > 代码库 > 简单音乐播放器
简单音乐播放器
今天复习了下导航器,把播放器美化了下
// ViewController.m// 播放器//// Created by apple on 14-7-18.// Copyright (c) 2014年 apple. All rights reserved.//#import "ViewController.h"#import "AVFoundation/AVFoundation.h"#import "SetViewController.h"@interface ViewController ()<AVAudioPlayerDelegate>{ AVAudioPlayer *player;//播放器 NSArray *_musiclist;//歌曲名列表 NSTimer *_timer;//定时器检查播放进度的动态送给进度tiao int _index;//歌曲的多少; UIImageView *imageView;}//播放进度的显示@property IBOutlet UIButton *start;@property IBOutlet UISlider *progressSlider;@property IBOutlet UILabel *musicTitle;@end@implementation ViewController//make backgrounder- (void)backgrouder{ imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 320, 480)]; imageView.image=[UIImage imageNamed:@"1.jpg"]; [self.view addSubview:imageView]; self.view.backgroundColor=[UIColor greenColor];}//make button and label- (void)chuangJianBtnAndLabel{ //start/shop _start=[UIButton buttonWithType:UIButtonTypeCustom]; _start.frame=CGRectMake(110, 400, 100, 35); //[_start setTitle:@"播放" forState:UIControlStateNormal]; [_start setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [_start setImage:[UIImage imageNamed:@"03.jpg"] forState:UIControlStateNormal]; [_start addTarget:self action:@selector(kaiOrStop) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_start]; //next music Btn UIButton *nextBtn=[UIButton buttonWithType:UIButtonTypeCustom]; nextBtn.frame=CGRectMake(210, 400, 100, 35); [nextBtn setImage:[UIImage imageNamed:@"02.jpg"] forState:UIControlStateNormal]; [nextBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [nextBtn addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:nextBtn]; //before music btn UIButton *beforeBtn=[UIButton buttonWithType:UIButtonTypeCustom]; beforeBtn.frame=CGRectMake(10, 400, 100, 35); [beforeBtn setImage:[UIImage imageNamed:@"04.jpg"] forState:UIControlStateNormal]; [beforeBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [beforeBtn addTarget:self action:@selector(before:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:beforeBtn]; UIButton *setBtn=[UIButton buttonWithType:UIButtonTypeCustom]; setBtn.frame=CGRectMake(220, 20, 100, 30); [setBtn setTitle:@"setUp" forState:UIControlStateNormal]; [setBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [setBtn addTarget:self action:@selector(setBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:setBtn]; //[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:)]; //labelChuangJian UILabel *shengYinlabel=[[UILabel alloc]initWithFrame:CGRectMake(14, 350, 80, 30)]; shengYinlabel.text=@"声音"; shengYinlabel.textColor=[UIColor yellowColor]; [self.view addSubview:shengYinlabel]; UILabel *progresslabel=[[UILabel alloc]initWithFrame:CGRectMake(14, 310, 80, 30)]; progresslabel.text=@"进程"; progresslabel.textColor=[UIColor yellowColor]; [self.view addSubview:progresslabel]; _musicTitle=[[UILabel alloc]initWithFrame:CGRectMake(14, 140, 280, 60)]; _musicTitle.textColor=[UIColor greenColor]; _musicTitle.textAlignment=NSTextAlignmentCenter; _musicTitle.text=[NSString stringWithFormat:@"%@",_musiclist[_index]]; [self.view addSubview:_musicTitle];}- (void)setBtn:(UIButton *)sender{ SetViewController *setViewCter=[[SetViewController alloc]init]; setViewCter.view.backgroundColor=[UIColor blueColor]; [self.navigationController pushViewController:setViewCter animated:YES];}//下一首按钮- (void)next:(UIButton *)sender{ if (_index==_musiclist.count-1) { _index=0; } else { _index++; } [self play]; NSLog(@"下一首");}- (void)before:(UIButton *)sender{ if (_index==0) { _index=_musiclist.count-1; } else { _index--; } [self play]; NSLog(@"上一首");}//创建UISlider-(void)chuangJianSlider{ UISlider *shengYinSlider=[[UISlider alloc]initWithFrame:CGRectMake(50, 360, 250, 10)]; shengYinSlider.value=0.3; shengYinSlider.tintColor=[UIColor blueColor]; shengYinSlider.maximumValue=1; shengYinSlider.minimumValue=0; [shengYinSlider addTarget:self action:@selector(shengYin:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:shengYinSlider]; _progressSlider=[[UISlider alloc]initWithFrame:CGRectMake(50, 320, 250, 10)];// _progressSlider.value=http://www.mamicode.com/0.5; _progressSlider.tintColor=[UIColor blueColor]; _progressSlider.maximumValue=1; _progressSlider.minimumValue=0; [_progressSlider addTarget:self action:@selector(progressSlider:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:_progressSlider];}//声音slider的动作方法-(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) { [_start setImage:[UIImage imageNamed:@"03.jpg"] forState:UIControlStateNormal]; [player stop]; } else { [_start setImage:[UIImage imageNamed:@"05.jpg"] forState:UIControlStateNormal]; [player play]; } NSLog(@"kaiShiTingZhi");}//开始播放设置(shunXuPlay)- (void)play{ NSString *name=_musiclist[_index]; //显示不同的图片 NSString *imageName=[NSString stringWithFormat:@"%d.jpg",_index+1]; imageView.image=[UIImage imageNamed:imageName]; //show music title _musicTitle.text=[NSString stringWithFormat:@"%@",_musiclist[_index]]; NSString *path=[[NSBundle mainBundle] pathForResource:name ofType:@"mp3"]; // NSData *avData=http://www.mamicode.com/[NSData dataWithContentsOfFile:path];>// player=[[AVAudioPlayer alloc]initWithData:avData error:nil]; //生成一个urld的对象 NSURL *url=[NSURL fileURLWithPath:path]; NSData *data=http://www.mamicode.com/[NSData dataWithContentsOfURL:url]; //把本地文件给播放器 player=[[AVAudioPlayer alloc]initWithData:data error:nil]; player.delegate=self; //改变速率 //player.enableRate=YES; player.rate=4; //设置开始的时间 player.currentTime=0; 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 backgrouder]; [self chuangJianBtnAndLabel]; [self chuangJianSlider]; _musiclist=@[@"Beyond-光辉岁月",@"G.E.M.邓紫棋-你把我灌醉",@"G.E.M.邓紫棋-回忆的沙漏 (国)",@"Beyond-海阔天空",@"Declan Galbraith-Tell Me Why"]; [self play]; [player stop];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。