controller 文件中
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
viewController.m
#import "ViewController.h"
#import "Music.h"
#import "NameView.h"
#import "PlayViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *arr;
NameView *dataView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
arr = [NSMutableArray array];
dataView= [[NameView alloc] initWithFrame:self.view.frame];
self.view = dataView;
dataView.table.delegate = self;
dataView.table.dataSource = self;
Music *music = [[Music alloc] init];
arr = [music getSongArr];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
if (!cell )
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
}
cell.textLabel.text = [arr[indexPath.row]objectForKey:@"name"];
cell.detailTextLabel.text = [arr[indexPath.row]objectForKey:@"singer"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayViewController *thevc = [[PlayViewController alloc] init];
thevc.ss = [[Music alloc] init];
// 用来传值的
thevc.ss.name = [arr[indexPath.row]objectForKey:@"name"];
thevc.ss.singer =[arr[indexPath.row]objectForKey:@"singer"];
[self.navigationController pushViewController:thevc animated:YES];
}
@end
PlayViewController.h
#import <UIKit/UIKit.h>
#import "Music.h"
@interface PlayViewController : UIViewController
@property(nonatomic,strong)Music *ss;
@end
PlayViewController.m
#import "PlayViewController.h"
#import "SingerView.h"
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"
@interface PlayViewController ()
{
SingerView *ppSong;
AppDelegate *app;
NSArray *arr;
}
@end
@implementation PlayViewController
-(void)viewWillDisappear:(BOOL)animated
{
//停止音乐播放
[app.player stop];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = [NSString stringWithFormat:@"%@,%@",self.ss.name,self.ss.singer ];
// 展示视图
ppSong = [[SingerView alloc] initWithFrame:self.view.frame];
self.view = ppSong;
app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSString *str = [[NSBundle mainBundle]pathForResource:self.ss.name ofType:@"mp3"];
app.player = [[AVAudioPlayer alloc]initWithData:[NSData dataWithContentsOfFile:str] error:nil];
[app.player play];
}
-(void)btnPlay:(UIButton *)sender{
if(sender.isSelected==NO)
{
sender.selected=YES;
//停止播放
[app.player stop];
}
else
{
sender.selected=NO;
//开始播放
[app.player play];
}
}
@end
View 文件
NameView.h
#import <UIKit/UIKit.h>
@interface NameView : UIView
@property(nonatomic,strong)UITableView *table;
@end
NameView.m
#import "NameView.h"
@implementation NameView
// 单例方法
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self addSubview:self.table];
}
return self;
}
-(UITableView *)table
{
if (!_table)
{
_table = [[UITableView alloc]initWithFrame:self.frame];
}
return _table;
}
@end
SingerView.h
#import <UIKit/UIKit.h>
@interface SingerView : UIView
@property(nonatomic,strong)UIImageView *image;
@property(nonatomic,strong)UIView *smallView;
@property(nonatomic,strong)UIButton *btnDown,*btnLike,*btnRound,*btnUp,*bthPlay;
@property(nonatomic,strong)UISlider *slider;
@end
SingerView.m
#import "SingerView.h"
@implementation SingerView
// 单列
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self addSubview:self.image];
[self addSubview:self.smallView];
}
return self;
}
-(UIImageView *)image
{
if (!_image)
{
_image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.frame.size.width, self.frame.size.height-64-150)];
_image.image = [UIImage imageNamed:@"1.jpg"];
}
return _image;
}
-(UIView *)smallView
{
if (!_smallView)
{
_smallView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-150, self.frame.size.width, 150)];
_smallView.backgroundColor = [UIColor lightGrayColor];
[_smallView addSubview:self.slider];
[_smallView addSubview:self.btnDown];
[_smallView addSubview:self.btnLike];
[_smallView addSubview:self.btnRound];
[_smallView addSubview:self.btnUp];
[_smallView addSubview:self.bthPlay];
}
return _btnDown;
}
-(UIButton *)btnDown{
if (!_btnDown) {
_btnDown=[UIButton buttonWithType:UIButtonTypeCustom];
_btnDown.frame=CGRectMake(self.frame.size.width/2-30-30+100, 90, 60, 60);
[_btnDown setImage:[UIImage imageNamed:@"btn_right"] forState:UIControlStateNormal];
}
return _btnDown;
}
-(UIButton *)btnLike{
if (!_btnLike) {
_btnLike=[UIButton buttonWithType:UIButtonTypeCustom];
_btnLike.frame=CGRectMake(340, 10, 40, 40);
[_btnLike setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
}
return _btnLike;
}
-(UIButton *)btnRound{
if (!_btnRound) {
_btnRound=[UIButton buttonWithType:UIButtonTypeCustom];
_btnRound.frame=CGRectMake(50, 90, 40, 40);
[_btnRound setImage:[UIImage imageNamed:@"Circulate"] forState:UIControlStateNormal];
}
return _btnRound;
}
-(UIButton *)btnUp
{
if (!_btnUp) {
_btnUp=[UIButton buttonWithType:UIButtonTypeCustom];
_btnUp.frame=CGRectMake(self.frame.size.width/2-30-80, 90, 60, 60);
[_btnUp setImage:[UIImage imageNamed:@"btn_left"] forState:UIControlStateNormal];
}
return _btnUp;
}
-(UIButton *)bthPlay
{
if (!_bthPlay)
{
_bthPlay = [UIButton buttonWithType:UIButtonTypeCustom];
_bthPlay.frame = CGRectMake(self.frame.size.width/2-30, 90, 60, 60);
[_bthPlay setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateNormal];
}
return _bthPlay;
}
-(UISlider *)slider
{
if (!_slider)
{
_slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 60, self.frame.size.width-40, 30)];
_slider.minimumValue = 0;
_slider.minimumValue = 1;
_slider.continuous = NO;
}
return _slider;
}
@end
mode文件
AppDelegate.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,strong)AVAudioPlayer *player;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//导航条
ViewController *theVC = [[ViewController alloc] init];
theVC.title = @"歌曲列表";
UINavigationController *theNav = [[UINavigationController alloc] initWithRootViewController:theVC];
self.window.rootViewController = theNav;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
Music.h
#import <Foundation/Foundation.h>
@interface Music : NSObject
@property(nonatomic,strong)NSString *name,*singer;
-(NSMutableArray *)getSongArr;
@end
Music.m
#import "Music.h"
@implementation Music
-(NSMutableArray *)getSongArr
{
NSString *filepath = [[NSBundle mainBundle]pathForResource:@"music" ofType:@"plist"];
return [NSMutableArray arrayWithContentsOfFile:filepath];
}
@end