#一个完整功能的音乐播放器app开源代码,支持音乐播放的全部功能,包括暂停、前进后退、循环播放、歌词同步显示等等,现在分享一下系统的播放器AVAudioPlay#
1. 打开 ViewControler.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController
@property (nonatomic, retain) AVAudioPlayer *player; // 创建一个播放器
@property (nonatomic, retain) UIButton *button; // 播放按钮
@property (nonatomic, retain) UIProgressView *progress;// 进度显示
@property (nonatomic, retain) NSTimer *time; // 时间
@property (nonatomic, retain) UISlider *volumeSlider;
@end
2. 打开ViewControler.m
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
typedef NS_ENUM(NSInteger, playStatus){
playStatusNo,
playStatusYes,
};
@interface ViewController ()<AVAudioPlayerDelegate>
@property (nonatomic, assign) playStatus staus;
@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) UILabel *timeLabel;
@end
@implementation ViewController
- (void)dealloc
{
[_progress release];
[_button release];
[_player release];
[super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 创建一个播放按钮
self.button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
// 给按钮一个颜色,
_button.backgroundColor = [UIColor redColor];
// 按钮titile
[_button setTitle:@"Play" forState:UIControlStateNormal];
// 把按钮变换成圆角的
_button.layer.cornerRadius = 50;
_button.layer.masksToBounds = YES;
// 实现按钮方法
[_button addTarget:self action:@selector(buttonClickStart) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
[_button release];
// 停止按钮
UIButton *stop = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
stop.backgroundColor = [UIColor redColor];
[stop setTitle:@"Stop" forState:UIControlStateNormal];
stop.layer.cornerRadius = 50;
stop.layer.masksToBounds = YES;
[stop addTarget:self action:@selector(buttonClickStop) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stop];
[stop release];
//播放本地
// self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"]] error:nil];
// 播放网络
NSData *mydata=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://a.ali.dongting.com/494a44568a1c7555/1422090214/m4a_32_9/0b/0c/0b4466a12a5af34259fba0090380da0c.m4a?s=t"]];
// 初始化播放器,并将url传给播放器
self.player=[[AVAudioPlayer alloc] initWithData:mydata error:nil];
// 播放器的代理方法
_player.delegate = self;
NSLog(@"currentTime == %f", self.player.currentTime);
//_player.volume =0.8; //音量 0.0-1.0 之间
[_player prepareToPlay]; //分配播放所需的资源,并将其加入内部播放队列 (预播放)
// 初始化进度条
//self.progress = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 50, 320, 20)];
self.slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 60, 320, 20)];
self.slider.maximumValue = self.player.duration;
[self.slider addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.slider];
[self.slider release];
self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
self.timeLabel.backgroundColor = [UIColor redColor];
self.timeLabel.text = @"0:00";
[self.view addSubview:self.timeLabel];
[self.timeLabel release];
// 用NSTimer来监控音频播放进度
// 预订一个Timer,设置一个时间间隔。表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1 self表发送的对象 useInfo此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器 repeat 为YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。
_time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateslider) userInfo:nil repeats:YES];
// [self.view addSubview:_progress];
// [_progress release];
// 播放次数
UISwitch *loop = [[UISwitch alloc] initWithFrame:CGRectMake(100, 230, 60, 40)];
[loop addTarget:self action:@selector(loopYesOrNo) forControlEvents:UIControlEventValueChanged];
loop.on = NO; // 默认状态不打开
[self.view addSubview:loop];
[loop release];
//初始化音量控制
self.volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 70, 200, 20)];
[_volumeSlider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];
//设置最小音量
_volumeSlider.minimumValue = 0.0f;
//设置最大音量
_volumeSlider.maximumValue = 1.0f;
//初始化音量为多少
_volumeSlider.value = 0.5f;
[self.view addSubview:_volumeSlider];
[_volumeSlider release];
// 声音开关
UISwitch *volume = [[UISwitch alloc] initWithFrame:CGRectMake(200, 230, 60, 40)];
[volume addTarget:self action:@selector(volumeOnOrOff:) forControlEvents:UIControlEventValueChanged];
volume.on = YES; // 默认状态打开
[self.view addSubview:volume];
[volume release];
}
- (void)buttonClickStart
{
if (self.staus == playStatusYes) {
self.staus = playStatusNo;
[_player pause]; // 暂停播放;
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateMusicTimeLabel) userInfo:self repeats:
YES];
[_button setTitle:@"暂停播放" forState:UIControlStateNormal];
}
else
{
[_player play]; // 开始播放
self.staus = playStatusYes;
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateMusicTimeLabel) userInfo:self repeats:
YES];
[_button setTitle:@"正在播放" forState:UIControlStateNormal];
}
}
-(void)updateMusicTimeLabel{
if ((int)self.player.currentTime % 60 < 10) {
self.timeLabel.text = [NSString stringWithFormat:@"%d:0%d",(int)self.player.currentTime / 60, (int)self.player.currentTime % 60];
} else {
self.timeLabel.text = [NSString stringWithFormat:@"%d:%d",(int)self.player.currentTime / 60, (int)self.player.currentTime % 60];
}
// NSDictionary *dic = self.list[_count];
// NSArray *array = [dic[@"lrctime"] componentsSeparatedByString:@":"];//把时间转换成秒
// NSUInteger currentTime = [array[0] intValue] * 60 + [array[1] intValue];
}
- (void)buttonClickStop
{
_player.currentTime = 0;// 设值当前播放时间为0;
[_player stop]; // 停止播放
[_player play];
}
// 循环播放
- (void)loopYesOrNo
{
_player.numberOfLoops = -1; // 这里 正数就是播放几次 -1 就是无限循环
}
// 进度条
- (void)updateslider
{
self.slider.value = self.player.currentTime;
}
- (void)sliderValueChanged
{
[self.player stop];
[self.player setCurrentTime:self.slider.value];
[self.player prepareToPlay];
[self.player play];
}
//- (void)setProgress:(float)progress animated:(BOOL)animated
//{
// //通过音频播放时长的百分比,给progress进行赋值;(当前播放时间 / 持续时间)
// // _progress.progress = _player.currentTime / _player.duration;
//
//
// animated = YES;
//}
// 音量大小
- (void)volumeChange
{
_player.volume = _volumeSlider.value;
}
// 播放结束之后的动作 (代理实现)
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
self.staus = playStatusNo;
[_player pause]; // 暂停播放;
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateMusicTimeLabel) userInfo:self repeats:
YES];
[_button setTitle:@"暂停播放" forState:UIControlStateNormal];
}
// 开始播放操作
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
[_player play];
}
// 声音开关
- (void)volumeOnOrOff:(UISwitch *)sender
{
_player.volume = sender.on;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end