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];
    
    // 用來(lái)傳值的
    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
{
    //停止音樂(lè)播放
    [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;
        //開(kāi)始播放
        [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
{
   //導(dǎo)航條
   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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 27、ViewController的didReceiveMemoryWarning是在什么時(shí)候調(diào)用的?默認(rèn)的操作是...
    煙雨平生花飛舞閱讀 695評(píng)論 0 1
  • 1,Search Bar 怎樣去掉背景的顏色(storyboard里只能設(shè)置background顏色,可是發(fā)現(xiàn)cl...
    以德扶人閱讀 2,895評(píng)論 2 50
  • 序言 目前形勢(shì),參加到iOS隊(duì)伍的人是越來(lái)越多,甚至已經(jīng)到供過(guò)于求了。今年,找過(guò)工作人可能會(huì)更深刻地體會(huì)到今年的就...
    麥兜兜買兜兜閱讀 724評(píng)論 1 4
  • iOS 實(shí)戰(zhàn)開(kāi)發(fā)課程筆記 本貼旨在作為對(duì)極客班 《iOS 開(kāi)發(fā)實(shí)戰(zhàn)》第五期期課程視頻重新學(xué)習(xí)的筆記。目標(biāo)是建立一個(gè)...
    黃穆斌閱讀 3,259評(píng)論 12 57
  • 從昨天到今天 光陰白白從我手中溜走 我不是那么聰明 只是有點(diǎn)惋惜 直到天空降落第一滴雨 我就突然清醒了過(guò)來(lái)
    伍月的晴空閱讀 188評(píng)論 6 3

友情鏈接更多精彩內(nèi)容