Music播放

屏幕快照 2017-12-21 下午2.59.52.png

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
屏幕快照 2017-12-21 下午3.07.06.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,634评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,951评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,427评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,770评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,835评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,799评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,768评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,544评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,979评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,271评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,427评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,121评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,756评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,375评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,579评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,410评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,315评论 2 352

推荐阅读更多精彩内容

  • 27、ViewController的didReceiveMemoryWarning是在什么时候调用的?默认的操作是...
    烟雨平生花飞舞阅读 572评论 0 1
  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 2,345评论 2 50
  • 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了。今年,找过工作人可能会更深刻地体会到今年的就...
    麦兜兜买兜兜阅读 676评论 1 4
  • iOS 实战开发课程笔记 本贴旨在作为对极客班 《iOS 开发实战》第五期期课程视频重新学习的笔记。目标是建立一个...
    黄穆斌阅读 2,997评论 12 57
  • 从昨天到今天 光阴白白从我手中溜走 我不是那么聪明 只是有点惋惜 直到天空降落第一滴雨 我就突然清醒了过来
    伍月的晴空阅读 168评论 6 3