.h文件的全局变量
#import
#import
@interface PlayerViewController : UIViewController
{
AVAudioPlayer *_play;
UISlider *_timeSlider;
UISlider *_volumeSlider;
UILabel *_timeLable;
}
@property(nonatomic,assign)NSInteger time;
@end
#import "PlayerViewController.h"
#import "MPPlayController.h"
@interface PlayerViewController ()
@end
@implementation PlayerViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"音乐播放";
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"安静" ofType:@"mp3"];
NSURL *urlStr = [[NSURL alloc] initFileURLWithPath:filePath];
_play = [[AVAudioPlayer alloc] initWithContentsOfURL:urlStr error:nil];
// _play.currentTime = 0.0;
_play.volume = 20;
//加入播放序列
[_play prepareToPlay];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((375-80)/2, 50, 80, 30);
[button setTitle:@"音乐" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//音量滑动条
_volumeSlider = [[UISlider alloc] init];
_volumeSlider.frame = CGRectMake(100, 150, 130, 20);
_volumeSlider.minimumValue = 0;
_volumeSlider.maximumValue = 100;
_volumeSlider.value = _play.volume;
[_volumeSlider addTarget:self action:@selector(volumeAction) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_volumeSlider];
//音量Lable
UILabel *volumeLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 40, 20)];
volumeLable.text = @"音量";
[self.view addSubview:volumeLable];
//时间进度条
_timeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 250, 320,20)];
// _timeSlider.value = 0.0;
_timeSlider.minimumValue = 0;
_timeSlider.maximumValue = 335;
[_timeSlider addTarget:self action:@selector(sliderAction) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_timeSlider];
//时间
_timeLable = [[UILabel alloc] initWithFrame:CGRectMake(150, 200,80, 20)];
_timeLable.text = @"时间:";
[self.view addSubview:_timeLable];
//视频按钮
UIButton *mediaButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
mediaButton.frame = CGRectMake(140, 400, 50, 30);
[mediaButton setTitle:@"播放" forState:UIControlStateNormal];
[mediaButton addTarget:self action:@selector(mediaButtonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mediaButton];
}
- (void)buttonAction:(UIButton *)btn{
if (_play.playing) {
[btn setTitle:@"暂停" forState:UIControlStateNormal];
[_play pause];
}else{
[btn setTitle:@"开始" forState:UIControlStateNormal];
[_play play];
}
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
}
//播放进度条事件
- (void)sliderAction{
//移动滑动条时让播放的进度达到一致
_play.currentTime = _timeSlider.value;
//让滑动条到播放的位置
_time = _timeSlider.value;
}
//定时器方法
- (void)timeAction:(NSTimer *)timer{
if (_play.playing) {
_time++;
_timeSlider.value = _time;
_timeLable.text = [NSString stringWithFormat:@"时间:%ld",_time];
}else{
[timer invalidate];
}
}
//音量条滑动条的事件
- (void)volumeAction{
_play.volume = _volumeSlider.value;
}
此功能实现了简单的一首歌曲的播放进度随意拖动到想播放的地方,音量大小的的调节。(按住滑动条滑动即可)