iOS项目日志 - 音乐播放器

首先上效果图

代码如下:

 

#import "DSNMusicTableViewController.h"

#import "DSNMusicModel.h"

#import "DSNPlayingViewController.h"

 

@interface DSNMusicTableViewController ()

@property (nonatomic,strong)NSArray *musics;

@end

 

@implementation DSNMusicTableViewController

-(NSArray *)musics

{

    if (!_musics) {

        NSString *path = [[NSBundle mainBundle]pathForResource:@"musics.plist" ofType:nil];

        NSArray *arr = [NSArray arrayWithContentsOfFile:path];

        

        

        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count];

        for(NSDictionary *dic in arr){

            DSNMusicModel *model = [DSNMusicModel musicWithDic:dic];

            [arrM addObject:model];

        }

        _musics = [arrM copy];

    }

    return _musics;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.tableView.rowHeight = 80;

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

#pragma mark - Table view data source

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.musics.count;

}

 

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identifier = @"cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

    }

    DSNMusicModel *model = self.musics[indexPath.row];

    cell.textLabel.text = model.singer;

    cell.detailTextLabel.text = model.name;

    

    

    cell.imageView.frame = CGRectMake(10, 0, 30, 30);

    cell.imageView.image = [UIImage imageNamed:model.singericon];

    cell.imageView.layer.masksToBounds = YES;

    cell.imageView.layer.cornerRadius = 40;

    cell.imageView.layer.borderColor = [UIColor greenColor].CGColor;

    cell.imageView.layer.borderWidth = 2;

    return cell;

}

 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    DSNPlayingViewController *plaVC = [self.storyboard instantiateViewControllerWithIdentifier:@"playID"];

    [self.navigationController showViewController:plaVC sender:nil];

    

 

    DSNMusicModel *model = self.musics[indexPath.row];

    plaVC.icon = model.icon;

    plaVC.total = model.time;

    plaVC.name = model.name;

    plaVC.singer = model.singer;

 

}

 

 

 

 

 

 

 

 

 

 

 

#import "DSNPlayingViewController.h"

#import "DSNMusicModel.h"

#import <AVFoundation/AVFoundation.h>

 

@interface DSNPlayingViewController ()

@property (weak, nonatomic) IBOutlet UIButton *playButton;

@property (weak, nonatomic) IBOutlet UILabel *singerNameLabel;

@property (weak, nonatomic) IBOutlet UILabel *songNameLabel;

@property(nonatomic,strong)NSArray *musics;

@property (nonatomic,assign)int currentIndex;

@property (nonatomic,strong)AVAudioPlayer *player;

- (IBAction)playBTNClick:(UIButton *)sender;

 

 

 

@end

 

@implementation DSNPlayingViewController

{

    int flag;

    CADisplayLink *link ;

 

}

-(NSArray *)musics{

    if (!_musics) {

        NSString *path = [[NSBundle mainBundle]pathForResource:@"musics.plist" ofType:nil];

        NSArray *arr = [NSArray arrayWithContentsOfFile:path];

        

        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count];

        for(NSDictionary *dic in arr){

            DSNMusicModel *model = [DSNMusicModel musicWithDic:dic];

            [arrM addObject:model];

        

        }

        _musics = [arrM copy];

    }

    return _musics;

 

}

 

 

- (IBAction)play {

//    CADisplayLink *link = nil;

    flag+=1;

    if (flag%2==1) {

        [self.player play];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            

            link = [CADisplayLink displayLinkWithTarget:self selector:@selector(transformCircle)];

            [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];

            

            [UIView animateWithDuration:1.0 animations:^{

                

                self.playButton.transform = CGAffineTransformRotate(self.playButton.transform, M_PI_4);

                

            }];

        });

 

    }else if(flag %2 == 0){

        [self.player pause];

        [link invalidate];

        

        [UIView animateWithDuration:1.0 animations:^{

            self.playButton.transform = CGAffineTransformRotate(self.playButton.transform, -M_PI_4);

        }];

    }

 

 

}

- (IBAction)back {

    [self.player stop];

//    [link invalidate];

    self.currentIndex--;

    if (self.currentIndex == self.musics.count) {

        self.currentIndex = 0;

    }

    [self.player play];

}

- (IBAction)forward {

    [self.player stop];

    

    self.currentIndex ++;

    if (self.currentIndex == 0) {

        self.currentIndex = (int)self.musics.count;

    }

    [self.player stop];

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

       NSURL *url = [[NSBundle mainBundle]URLForResource:self.name withExtension:@".mp3"];

    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];

    self.currentIndex = 0;

    

 

    [self initSubviews];

    // Do any additional setup after loading the view.

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    if ([segue.identifier isEqualToString:@"back"]) {

        [self.player stop];

    }

 

 

}

-(void)transformCircle{

    

    self.singerView.transform = CGAffineTransformRotate(self.singerView.transform, M_PI_2/100);

 

}

-(void)initSubviews

{

    self.singerView.layer.cornerRadius = 100;

    self.singerView.layer.masksToBounds = YES;

    self.singerView.layer.borderColor = [UIColor blackColor].CGColor;

    self.singerView.layer.borderWidth = 30;

    self.singerView.image = [UIImage imageNamed:self.icon];

    

    self.totalTime.text = self.total;

    self.singerNameLabel.text = self.singer;

    self.songNameLabel.text = self.name;

    

    self.playButton.transform = CGAffineTransformRotate(self.playButton.transform, -M_PI_4);

    

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updatePro) userInfo:nil repeats:YES];

    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];

    

}

-(void)updatePro{

    

    NSString *xiaoshi= [self.total substringToIndex:1];

    NSString *fenzhong = [self.total substringToIndex:2];

    int t = [xiaoshi intValue] *60 + [fenzhong intValue];

    self.barProgress.progress = self.player.currentTime/t;

    

    int currentFenzhong = (int)self.player.currentTime % 60;

    int currentXiaoshi = (int)self.player.currentTime /60;

    self.currentTime.text = [NSString stringWithFormat:@"0%d:%d",currentXiaoshi,currentFenzhong];;

 

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

 

 

 

-(instancetype)initWithDic:(NSDictionary *)dic

{

    if (self = [super init]) {

        [self setValuesForKeysWithDictionary:dic];

    }

    return self;

}

+(instancetype)musicWithDic:(NSDictionary *)dic

{

    return [[self alloc]initWithDic:dic];

}

 

posted @ 2016-03-01 15:58  small-elephant_A-Do  阅读(205)  评论(0编辑  收藏  举报