您的位置:首页 > 其它

音乐播放器

2017-07-15 20:50 85 查看

在ViewController.m中

#import "ViewController.h"
//导入音频播放器的框架
#import <AVFoundation/AVFoundation.h>
#import "SecondViewController.h"

@interface ViewController ()<AVAudioPlayerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UISlider *processSlider;
@property (weak, nonatomic) IBOutlet UISlider *volumeSlider;
//声明音频播放器对象的属性,便于以下代码的操作
@property(retain)AVAudioPlayer * player;
//音乐的名字
@property(retain)NSString * musicName;

@property(retain)SecondViewController * VC;

@end

@implementation ViewController

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
if (_musicName.length==0) {
_nameLabel.text=@"蓝莲花";
}
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取音频文件的相对路径
NSString * path=[[NSBundle mainBundle]pathForResource:@"蓝莲花" ofType:@"mp3"];
//把相对路径转成url
NSURL * url=[NSURL fileURLWithPath:path];
//初始化音乐播放器
_player =[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
//
_player.delegate=self;
//加载到内存准备播放
[_player prepareToPlay];

[[NSNotificationCenter defaultCenter ]addObserver:self selector:@selector(changeMusic:) name:@"music" object:nil];
}
//播放按钮
- (IBAction)playAction:(UIButton *)sender {
[_player play];
NSTimer * timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updataUI) userInfo:nil repeats:YES];
}
-(void)updataUI
{
// _player.duration  //总时长
//  _player.currentTime  //播放了的时长
//设置进度条的值
_processSlider.value=_player.currentTime/_player.duration;

//总时长的分
int allmm=_player.duration/60;
//总时长的秒数
int allss=(int)_player.duration%60;
//当前时长的分
int cuMM=_player.currentTime/60;
//当前时长的秒
int cuSS=(int)_player.currentTime%60;

_timeLabel.text=[NSString stringWithFormat:@"%.2d:%.2d/%.2d:%.2d",cuMM,cuSS,allmm,allss];

}
//音乐播放完成
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"播放完成");
}
//音乐播放被中断
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
NSLog(@"音乐被突然中断,比如系统电话来了");
}
//中断已结束
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
{
NSLog(@"中断已结束,可以正常播放音乐了");
[self playAction:nil];
}
-(void)changeMusic:(NSNotification *)notification
{
_musicName=notification.object;
_nameLabel.text=_musicName;
//获取音频文件的路劲
NSString*path=[[NSBundle mainBundle]pathForResource:_musicName ofType:@"mp3"];
//把相对路劲转化为url
NSURL*url=[NSURL fileURLWithPath:path];
if (_player) {
_player=nil;
}
//设置代理
_player.delegate=self;
_player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
//加载到内存准备播放
[_player prepareToPlay];
[self playAction:nil];
}
//停止播放
- (IBAction)stopAction:(UIButton *)sender {
[_player stop];
}
//改变进度
- (IBAction)processAction:(UISlider *)sender {
//根据Slider的值直接改变进度,这样会出现卡顿的现象
//_player.currentTime=sender.value*_player.duration;
//这样设置就不会出现卡顿
[_player stop];
_player.currentTime=sender.value * _player.duration;
}
- (IBAction)touchLeaveAction:(UISlider *)sender {
[self playAction:nil];
}
//播放音量
- (IBAction)volmeAction:(UISlider *)sender {
[_player setVolume:sender.value*100];
}
- (IBAction)musicListAction:(UIButton *)sender {
_VC=[[SecondViewController alloc]init];
[self presentViewController:_VC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


在SecondViewController.m中

#import "SecondViewController.h"

@interface SecondViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(retain)NSMutableArray * dataSource;

@end

@implementation SecondViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//获取文件的相对路径
NSString*path=[[NSBundle mainBundle]pathForResource:@"musicList" ofType:@"plist"];
//读取plist文件的内容
NSArray*arr=[NSArray arrayWithContentsOfFile:path];
_dataSource=[NSMutableArray arrayWithArray:arr];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataSource.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString*cellID=@"cell";
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

}
cell.textLabel.text=_dataSource[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter]postNotificationName:@"music" object:_dataSource[indexPath.row]];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  音乐播放器