#import "RootViewController.h" @interface RootViewController () { UITableView * table; NSMutableArray * dataSource; } @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; dataSource = [[NSMutableArray alloc]init]; NSString * path = [[NSBundle mainBundle]pathForResource:@"songsName" ofType:@"plist"]; NSArray * arr = [NSArray arrayWithContentsOfFile:path]; [dataSource addObjectsFromArray:arr]; table = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, 320, 480 - 30) style:UITableViewStylePlain]; table.delegate =self; table.dataSource = self; [self.view addSubview:table]; self.automaticallyAdjustsScrollViewInsets = NO; //爲表格視圖添加背景圖片 UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; imageView.image = [UIImage imageNamed:@"56.jpg"]; table.backgroundView = imageView; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataSource count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * string = @"string"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string]; if(cell == nil) { cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease]; } cell.textLabel.text = [dataSource objectAtIndex:indexPath.row]; cell.backgroundColor = [UIColor clearColor]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString * songName = [dataSource objectAtIndex:indexPath.row]; //向上一個界面發送通知傳遞歌曲名稱 [[NSNotificationCenter defaultCenter] postNotificationName:@"songName" object:songName]; [self dismissViewControllerAnimated:YES completion:nil]; }
// MainViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface MainViewController : UIViewController<AVAudioPlayerDelegate> @property (retain, nonatomic) IBOutlet UILabel *songLabel; - (IBAction)pressPlay:(id)sender; - (IBAction)pressStop:(id)sender; @property (retain, nonatomic) IBOutlet UILabel *timeLabel; - (IBAction)progressSlider:(id)sender; - (IBAction)voiceSlider:(id)sender; - (IBAction)changSong:(id)sender; @property (retain, nonatomic) IBOutlet UISlider *progress; @property (retain, nonatomic) IBOutlet UISlider *voice; @end
// // MainViewController.m #import "MainViewController.h" #import "RootViewController.h" @interface MainViewController () { RootViewController * root; } @property (nonatomic,retain) NSMutableArray * dataSource; //接收歌曲的名稱 @property (nonatomic,retain) NSString * songName; @property (nonatomic,retain) AVAudioPlayer * player; @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; //存放全部的歌曲名稱 self.dataSource = [[NSMutableArray alloc]init]; //讀取plist文件中的內容 NSString * path = [[NSBundle mainBundle]pathForResource:@"songsName" ofType:@"plist"]; NSArray * arr = [NSArray arrayWithContentsOfFile:path]; [self.dataSource addObjectsFromArray:arr]; //判斷此時歌曲名稱是否爲空 if(self.songName.length == 0) { self.songName = @"藍蓮花"; } self.songLabel.text = self.songName; //準備播放音樂 //<1>找到要播放的音樂的路徑 NSString * songPath = [[NSBundle mainBundle]pathForResource:@"藍蓮花" ofType:@"mp3"]; //設置了第一首歌 //<2>將本地路徑轉化成NSURL NSURL * url = [NSURL fileURLWithPath:songPath]; //<3>初始化音樂播放器對象 self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; //<4>設置代理 self.player.delegate = self; //<5>準備播放音樂 //在播放音樂以前 先將音樂存放在緩存區中 防止播放的過程當中出現卡頓 [self.player prepareToPlay]; root = [[RootViewController alloc]init]; //爲通知中心添加觀察者 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSongPlay:) name:@"songName" object:nil]; } //接收到通知觸發的方法 -(void)changeSongPlay:(NSNotification *)notification { self.songName = [notification object]; self.songLabel.text = self.songName; if(self.player != nil)//若是當前音樂正在播放 不設置的話會兩首歌一塊兒 { [self.player stop]; self.player = nil; } NSString *path = [[NSBundle mainBundle] pathForResource:self.songName ofType:@"mp3"]; NSURL * url = [NSURL fileURLWithPath:path]; self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; self.player.delegate = self; [self.player prepareToPlay]; [self pressPlay:nil]; } - (IBAction)pressPlay:(id)sender { [self.player play]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; } -(void)timeChange { //獲取當前音樂的總時間 按秒 NSTimeInterval allTime = self.player.duration; int MM = (int)allTime / 60; int SS = (int)allTime % 60; NSString * allStr = [NSString stringWithFormat:@"%.2d:%.2d",MM,SS]; //.2 一位數時前面會保留一個0 //獲取當前音樂播放到的時間 NSTimeInterval currentTime = self.player.currentTime; int mm = (int)currentTime / 60; int ss = (int)currentTime % 60; NSString * currentStr = [NSString stringWithFormat:@"%.2d:%.2d",mm,ss]; NSString * string = [NSString stringWithFormat:@"%@/%@",currentStr,allStr]; self.timeLabel.text = string; //設置進度 self.progress.value = currentTime / allTime; } - (IBAction)pressStop:(id)sender { if([self.player isPlaying]) { [self.player stop]; //[self.player pause]; } } - (IBAction)progressSlider:(id)sender { //<1>暫停音樂 [self pressStop:nil]; UISlider * slider = (UISlider *)sender; self.player.currentTime = slider.value * self.player.duration; //播放音樂 [self pressPlay:nil]; } - (IBAction)voiceSlider:(id)sender { UISlider * slider = (UISlider *)sender; [self.player setVolume:slider.value * 20]; } - (IBAction)changSong:(id)sender { [self presentViewController:root animated:YES completion:nil]; } @end