音乐播放器
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
相关文章推荐
- iOS开发拓展篇—音频处理(音乐播放器5)
- Ubuntu下安装Audacious音乐播放器详细配置方法(中文化,歌词显示等)
- 再学AS3(六)——音乐播放器(5):…
- Android4.0音乐播放器appwidget新增重复播放按钮图片如何于主应用同步更新
- 51单片机之简单的音乐播放器
- Java 音乐播放器
- Android 音乐播放器的开发教程(七)运用Broadcast实现service与activity的通信 ----- 小达
- 超酷HTML5环形音乐播放器
- 最简单的音乐播放器,实现基本功能(二)
- AVAudioPlayer的一些属性(音乐播放器)
- Exaile 3.4.1 概览:一个全功能的GNOME音乐播放器
- 如何解决音乐播放器重音问题
- 乐乐音乐播放器(三) 第一次引导页面
- Android Service的使用方法 音乐播放器实例
- iOS开发拓展篇—音频处理(音乐播放器6)
- DirectSound初步教程 -- 如何从第三发音乐播放器中HOOK音频数据然后跟自己麦采集数据做同步
- 歌词解析(音乐播放器)
- HTML5+CSS3+jquery实现简单的音乐播放器
- android-音乐播放器实现及源码下载(二)
- Linux中madplay 音乐播放器移植步骤(在redhat中编译)