#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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步