音乐播放器
来源:互联网 发布:html商城模板源码 编辑:程序博客网 时间:2024/06/11 00:55
在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
阅读全文
0 0
- android音乐播放器播放音乐卡
- Android 音乐播放 类似音乐播放器
- flash音乐播放器
- 常见音乐播放器
- WEB音乐播放器
- Google 音乐播放器
- 单片机音乐播放器
- 简易音乐播放器
- 网页音乐播放器
- 音乐定时播放器
- android 音乐播放器
- 音乐播放器代码
- 音乐播放器01
- 个人音乐播放器
- YOYOPlayer音乐播放器
- Qt音乐播放器
- Android 音乐播放器
- android 音乐播放器
- UOJ #82 [UR #7] 水题生成器
- 优雅的让Fragment整合到ViewPager中
- uva509 RAID
- hdu 1728 逃离迷宫
- hdu 5823(状压DP)
- 音乐播放器
- python中文字符截取乱码
- Android Git 笔记
- Android实现可左右滑动的选择控件
- java导出jar包以及jar内程序读取jar包外配置文件
- Win7下安装Python和Python IDLE
- npm常用命令整理
- 面试题:C++实现一个不能被继承的类
- 第3章 视图