三个按钮 一个进度条
贴图(软件中部分图片,来自网络,如果侵犯了您的权益,请联系我,会立刻撤下)
核心代码
// // ViewController.m // 08-10-MusicPlayer // // Created by Ibokan on 15/8/10. // Copyright (c) 2015年 Crazy凡. All rights reserved. // #import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () @property (nonatomic,strong)UIImageView * imageview; @property (nonatomic,strong)UISlider *slider; @property (nonatomic,strong)UIButton *buttonPlay; @property (nonatomic,strong)UIButton *buttonPause; @property (nonatomic,strong)UIButton *buttonStop; @property (nonatomic,strong)AVAudioPlayer *player; @property (nonatomic,strong)NSTimer *timer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; [self.imageview setImage:[UIImage imageNamed:@"bgimg.jpg"]]; self.imageview.backgroundColor = [UIColor groupTableViewBackgroundColor]; self.imageview.contentMode = UIViewContentModeScaleAspectFill; [self.view addSubview:self.imageview]; //初始化背景 self.slider = [[UISlider alloc]initWithFrame:CGRectMake(40, 350, 240, 10)]; self.slider.value = 0.0; [self.view addSubview:self.slider]; [self.slider addTarget:self action:@selector(updateValue) forControlEvents:UIControlEventValueChanged]; self.buttonPlay = [[UIButton alloc]initWithFrame:CGRectMake(30, 400,60, 60)]; [self.view addSubview:self.buttonPlay]; [self.buttonPlay setBackgroundImage:[UIImage imageNamed:@"starten.png"] forState:UIControlStateNormal]; [self.buttonPlay setBackgroundImage:[UIImage imageNamed:@"startun.png"] forState:UIControlStateDisabled]; //初始化并插入A self.buttonPause = [[UIButton alloc]initWithFrame:CGRectMake(130, 400, 60, 60)]; [self.view addSubview:self.buttonPause]; self.buttonPause.enabled = false; [self.buttonPause setBackgroundImage:[UIImage imageNamed:@"stopen.png"] forState:UIControlStateNormal]; [self.buttonPause setBackgroundImage:[UIImage imageNamed:@"stopun.png"] forState:UIControlStateDisabled]; //初始化并插入B self.buttonStop = [[UIButton alloc]initWithFrame:CGRectMake(230, 400, 60, 60)]; [self.view addSubview:self.buttonStop]; self.buttonStop.enabled = false; [self.buttonStop setBackgroundImage:[UIImage imageNamed:@"pauseen.png"] forState:UIControlStateNormal]; [self.buttonStop setBackgroundImage:[UIImage imageNamed:@"pauseun.png"] forState:UIControlStateDisabled]; //初始化并插入C NSBundle *bundle = [NSBundle mainBundle]; NSString *path = [bundle pathForResource:@"tianyaguoke" ofType:@"mp3"]; //NSLog(@"%@",path); NSURL * url = [NSURL fileURLWithPath: path]; self.player= [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; [self.buttonPlay addTarget:self action:@selector(tapA) forControlEvents:UIControlEventTouchUpInside]; [self.buttonPause addTarget:self action:@selector(tabB) forControlEvents:UIControlEventTouchUpInside]; [self.buttonStop addTarget:self action:@selector(tabC) forControlEvents:UIControlEventTouchUpInside]; } - (void)tapA { self.buttonPlay.enabled = false; self.buttonPause.enabled = true; self.buttonStop.enabled = true; [self.player play]; self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeadd) userInfo:nil repeats:YES]; } - (void)tabB { self.buttonPlay.enabled = true; self.buttonPause.enabled = false; self.buttonStop.enabled = false; [self.player stop]; self.player.currentTime = 0 ; } - (void)tabC { self.buttonPlay.enabled = true; self.buttonPause.enabled = true; self.buttonStop.enabled = false; [self.player pause]; } - (void)timeadd { // double alltime = self.player.duration; // double now = self.player.currentTime; self.slider.value = self.player.currentTime/self.player.duration; } - (void)updateValue { self.player.currentTime = self.player.duration * self.slider.value; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
知识点总结:
1、[self.buttonPlay setBackgroundImage:[UIImage imageNamed:@"starten.png"] forState:UIControlStateNormal];
//为按钮添加图片
2、[self.slider addTarget:self action:@selector(updateValue) forControlEvents:UIControlEventValueChanged];
//slider(进度条)添加回调方法
3、self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeadd) userInfo:nil repeats:YES];
//NStimer的使用
4、NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"tianyaguoke" ofType:@"mp3"];
NSURL * url = [NSURL fileURLWithPath: path];
self.player= [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
//NSBundle、NSURL 为AVAudio 添加路径
5、self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.imageview setImage:[UIImage imageNamed:@"bgimg.jpg"]];
self.imageview.backgroundColor = [UIColor groupTableViewBackgroundColor];
//UIImageView 初始化
6、self.imageview.contentMode = UIViewContentModeScaleAspectFill;
self.imageview.contentMode = UIViewContentModeScaleAspectFit;
self.imageview.contentMode = UIViewContentModeScaleToFill;
//UIImageViewv 图片显示方式设定:比例充满 比例缩放 拉伸充满
7、self.player.currentTime;self.player.duration
//音乐播放器时间(当前播放的时间,时间总长)
点我下载源码 因为文件大小问题,文件中的音频被我删掉了,另附文件布局,我想大家可以看懂!