多媒体之本地音乐的播放(利用观察者模式传值)及播放中控件的变化
在RootViewController的m文件里:
1 #import "RootViewController.h" 2 #import "ListViewController.h" 3 @interface RootViewController () 4 5 @end 6 7 @implementation RootViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view from its nib. 12 self.slider_Process.minimumValue=0; 13 if (self.songName.length==0) { 14 self.songName=@"蓝莲花"; 15 } 16 self.lblTitle.text=self.songName; 17 //找歌曲路径 18 NSString *path=[[NSBundle mainBundle]pathForResource:@"蓝莲花" ofType:@"mp3"]; 19 //路径格式转换 20 NSURL *URL=[NSURL fileURLWithPath:path]; 21 player=[[AVAudioPlayer alloc] initWithContentsOfURL:URL error:nil]; 22 player.delegate=self; 23 //把播放的歌曲加到缓存中 24 [player prepareToPlay]; 25 26 //添加观察者(通知中心) 27 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(songNameChange:) name:@"changeMusic" object:nil]; 28 29 } 30 -(void)songNameChange:(NSNotification *)sender{ 31 //获取歌曲名字.sender onject获得通知者的值 32 self.songName=[sender object]; 33 self.lblTitle.text=self.songName; 34 35 NSString *strPath=[[NSBundle mainBundle] pathForResource:_songName ofType:@".mp3"]; 36 NSURL *URL=[NSURL fileURLWithPath:strPath];//从文件 37 // NSURL *URL=[NSURL URLWithString:strPath];//从网上转换路径 38 if (player!=nil) { 39 player=nil; 40 } 41 player=[[AVAudioPlayer alloc] initWithContentsOfURL:URL error:nil]; 42 player.delegate=self; 43 [player play]; 44 45 //更新页面 46 //播放时声音的值就是音量滑块的值 47 [player setVolume:_slider_Volume.value]; 48 //每隔一秒更新播放时间 49 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 50 } 51 //更新播放时间 52 -(void)updateTime{ 53 //获取播放歌曲的总时长,获取到的是秒数 54 NSTimeInterval allTime=player.duration; 55 int minute=allTime/60; 56 int second=(int)allTime%60; 57 //获取当前播放的时间的分、秒 58 NSTimeInterval currentTime=player.currentTime; 59 int currentMintue=currentTime/60; 60 int currentSecond=(int)currentTime%60; 61 //把当前时间和总时间格式化为一个字符串 62 NSString *strCurrentTime=[NSString stringWithFormat:@"%.2d:%.2d",currentMintue,currentSecond]; 63 NSString *strAllTime=[NSString stringWithFormat:@"/%.2d:%.2d",minute,second]; 64 self.lblTime.text=[NSString stringWithFormat:@"%@%@",strCurrentTime,strAllTime]; 65 66 self.slider_Process.value=player.currentTime/player.duration; //进度条随播放移动 67 } 68 #pragma mark - 实现音频播放协议里的函数 69 -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ 70 NSLog(@"当前歌曲播放完毕"); 71 } 72 -(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{ 73 NSLog(@"播放器出现异常"); 74 } 75 - (void)didReceiveMemoryWarning { 76 [super didReceiveMemoryWarning]; 77 // Dispose of any resources that can be recreated. 78 }
在ListViewController.h文件中:
1 #import "ListViewController.h" 2 3 @interface ListViewController () 4 5 @end 6 7 @implementation ListViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view from its nib. 12 [self initData]; 13 [self createTableView]; 14 } 15 -(void)initData{ 16 dataAry=[[NSMutableArray alloc] init]; 17 //从沙盒中读取数据 18 NSString *strPath=[[NSBundle mainBundle]pathForResource:@"musicList" ofType:@".plist"]; 19 NSArray *ary=[NSArray arrayWithContentsOfFile:strPath]; 20 [dataAry addObjectsFromArray:ary]; 21 } 22 -(void)createTableView{ 23 tv=[[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 600) style:UITableViewStylePlain]; 24 UIImageView *iv=[[UIImageView alloc] initWithFrame:tv.frame]; 25 iv.image=[UIImage imageNamed:@"king1"]; 26 [tv setBackgroundView:iv]; 27 tv.delegate=self; 28 tv.dataSource=self; 29 [self.view addSubview:tv]; 30 } 31 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 32 return dataAry.count; 33 } 34 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 35 NSString *identifier=@"song"; 36 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 37 if (!cell) { 38 cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 39 } 40 cell.textLabel.text=dataAry[indexPath.row]; 41 cell.backgroundColor=[UIColor clearColor]; 42 return cell; 43 } 44 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 45 //获得选中行的歌曲名字 46 NSString *songName=dataAry[indexPath.row]; 47 //把名字传回到第一个页面(Root页面)采用观察者模式 48 [[NSNotificationCenter defaultCenter] postNotificationName:@"changeMusic" object:songName]; 49 [self dismissViewControllerAnimated:NO completion:nil]; 50 51 } 52 - (void)didReceiveMemoryWarning { 53 [super didReceiveMemoryWarning]; 54 // Dispose of any resources that can be recreated. 55 }
实现效果:
此例利用Xib快速构建UI并添加控件事件等。实现本地音乐的播放、及播放中各控件如何变化和利用KVC-KVO观察者传值。
TO BE FIRST,BEST OR DIFFERENT.