zoukankan      html  css  js  c++  java
  • 音乐播放

    //  ViewController.m

    //  MusicPlayer-葫芦娃专属

    //

    //  Created by Bruce on 15/8/19.

    //  Copyright (c) 2015年 Bruce. All rights reserved.

    //

     

    #import "ViewController.h"

    #import "MusicInfoModel.h"

     

    #import "PlayMusic_ViewController.h"

     

    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

    {

        UITableView *musicListView;

        

        NSArray *musicList;

    }

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.view.backgroundColor = [UIColor whiteColor];

        self.title = @"葫芦娃的歌单";

        

        musicListView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

        musicListView.delegate = self;

        musicListView.dataSource = self;

        [self.view addSubview:musicListView];

        

    }

     

    - (void)viewWillAppear:(BOOL)animated

    {

        [self loadData];

    }

     

    - (void)loadData

    {

        NSString *path = [[NSBundle mainBundle] pathForResource:@"MusicList" ofType:@"plist"];

        

        NSArray *allList = [NSArray arrayWithContentsOfFile:path];

        

        NSMutableArray *modelList = [NSMutableArray array];

        for (NSDictionary *dic in allList) {

            

            MusicInfoModel *model = [[MusicInfoModel alloc]init];

            model.musicName = dic[@"musicName"];

            model.singerName = dic[@"singerName"];

            model.singerHeaderName = dic[@"singerHeaderImage"];

            model.desList = dic[@"desImages"];

            

            [modelList addObject:model];

        }

        

        musicList = [modelList copy];

        

        [musicListView reloadData];

    }

     

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

    {

        return musicList.count;

    }

     

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

    {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"musicCell"];

        if (!cell) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"musicCell"];

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }

        

        MusicInfoModel *model = musicList[indexPath.row];

        

    //    歌手头像

        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",model.singerHeaderName]];

    //    歌曲名

        cell.textLabel.text = [NSString stringWithFormat:@"歌曲名:《%@》",model.musicName];

    //    歌手名

        cell.detailTextLabel.text = model.singerName;

        

        return cell;

    }

     

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return 100;

    }

     

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

    {

        MusicInfoModel *model = musicList[indexPath.row];

        PlayMusic_ViewController *vc = [[PlayMusic_ViewController alloc]init];

        vc.info = model;

        

        vc.allMusic = musicList;

        vc.select = indexPath.row;

        [self.navigationController pushViewController:vc animated:YES];

    }

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

     

    //

    //  PlayMusic_ViewController.m

    //  MusicPlayer-葫芦娃专属

    //

    //  Created by Bruce on 15/8/19.

    //  Copyright (c) 2015年 Bruce. All rights reserved.

    //

    #import "PlayMusic_ViewController.h"

    #import "MusicInfoModel.h"

    #import <AVFoundation/AVFoundation.h>

    @interface PlayMusic_ViewController ()<UIScrollViewDelegate,AVAudioPlayerDelegate>

    {

        UIPageControl *pageControl;

        UILabel *allTimeLabel;

        UILabel *curTimeLabel;

        UISlider *lineView;

        

        AVAudioPlayer *player;

        

    //    刷新播放时间的定时器

        NSTimer *timer;

        

        UIScrollView *myScrollView;

        

        UILabel *name;

        

        NSInteger curIndex;

    }

    @end

    @implementation PlayMusic_ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        curIndex = 0;

        

        self.view.backgroundColor = [UIColor whiteColor];

        self.title = self.info.musicName;

        

        [self createViews];

    }

    #pragma mark ---------切换音乐 更新属性

    - (void)updateAllMessage:(MusicInfoModel *)info

    {

        

        self.title = info.musicName;

        

        myScrollView.contentSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds)*info.desList.count, 0);

        

        if (myScrollView.subviews.count!=0) {

            for (UIView *view in myScrollView.subviews) {

                [view removeFromSuperview];

            }

        }

        

        for (int i=0; i<info.desList.count; i++) {

            NSString *path = [[NSBundle mainBundle] pathForResource:info.desList[i] ofType:nil];

            

            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds)*i, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)/3*2)];

            imageView.contentMode = UIViewContentModeScaleAspectFill;

            imageView.image = [UIImage imageWithContentsOfFile:path];

            [myScrollView addSubview:imageView];

        }

        

        [self playMusicWithName:info.musicName];

        

        name.text = [NSString stringWithFormat:@"  %@ 《%@》",info.singerName,info.musicName];

        

    }

    //进入页面  播放音乐

    - (void)viewWillAppear:(BOOL)animated

    {

        curIndex = self.select;

        [self updateAllMessage:self.info];

        

    }

    //退出页面 停止音乐

    - (void)viewWillDisappear:(BOOL)animated

    {

        if (player) {

            [timer invalidate];

            timer = nil;

            [player stop];

            player = nil;

        }

    }

    #pragma mark ------音乐播放

    - (void)playMusicWithName:(NSString *)names

    {

        

        if (player) {

            [timer invalidate];

            timer = nil;

            [player stop];

            player = nil;

        }

        

        NSString *path = [[NSBundle mainBundle] pathForResource:names ofType:@"mp3"];

        player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];

        player.delegate = self;

        [player prepareToPlay];

        [player play];

        

        lineView.maximumValue = player.duration;

        

        int m = player.duration/60;

        int s = [@(player.duration)intValue]%60;

        allTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",m,s];

        

        

        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCurTime) userInfo:nil repeats:YES];

    }

    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

    {

        if (curIndex == self.allMusic.count-1) {

            curIndex = 0;

            [self updateAllMessage:self.allMusic[curIndex]];

            return;

        }

        

        if (curIndex<self.allMusic.count) {

            curIndex++;

            [self updateAllMessage:self.allMusic[curIndex]];

        }

    }

    #pragma mark ------定时器 更新播放时间

    - (void)updateCurTime

    {

        int m = player.currentTime/60;

        int s = [@(player.currentTime)intValue]%60;

        curTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",m,s];

        

    //    更新滑杆的进度

        lineView.value = player.currentTime;

    }

    #pragma mark -----创建视图

    - (void)createViews

    {

        myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)/3*2)];

        myScrollView.delegate = self;

        myScrollView.pagingEnabled = YES;

        myScrollView.contentSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds)*self.info.desList.count, 0);

        [self.view addSubview:myScrollView];

        

        UIView *toolBGView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(myScrollView.frame), CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)/3)];

        toolBGView.backgroundColor = [UIColor orangeColor];

        [self.view addSubview:toolBGView];

        

    //    分割线

        lineView = [[UISlider alloc]initWithFrame:CGRectMake(5, CGRectGetHeight(toolBGView.frame)/2-2, CGRectGetWidth(toolBGView.frame)-10, 2)];

        lineView.minimumTrackTintColor = [UIColor lightGrayColor];

        lineView.maximumTrackTintColor = [UIColor whiteColor];

        lineView.minimumValue = 0.0;

        [lineView addTarget:self action:@selector(changeCurTime:) forControlEvents:UIControlEventValueChanged];

        [toolBGView addSubview:lineView];

        

    //    艺人名、歌曲名

        name = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, CGRectGetWidth(toolBGView.frame)-20, 50)];

        name.textColor = [UIColor whiteColor];

        

        name.numberOfLines = 2;

        [toolBGView addSubview:name];

        

        pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(name.frame)+5, CGRectGetWidth(toolBGView.frame), 10)];

        pageControl.numberOfPages = self.info.desList.count;

        pageControl.hidesForSinglePage = YES;

        [toolBGView addSubview:pageControl];

        

        

    //    当前时间的lable

        curTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(lineView.frame)+10, 100, 20)];

        curTimeLabel.font = [UIFont systemFontOfSize:15];

        curTimeLabel.text = @"00:00";

        curTimeLabel.textColor = [UIColor whiteColor];

        [toolBGView addSubview:curTimeLabel];

        

    //    总时长

        allTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(toolBGView.frame)-5-100, CGRectGetMaxY(lineView.frame)+10, 100, 20)];

        allTimeLabel.textAlignment = NSTextAlignmentRight;

        allTimeLabel.font = [UIFont systemFontOfSize:15];

        allTimeLabel.text = @"00:00";

        allTimeLabel.textColor = [UIColor whiteColor];

        [toolBGView addSubview:allTimeLabel];

        

        

        NSArray *titles = @[@"上一曲",@"暂停",@"下一曲"];

        for (int i=0; i<3; i++) {

            

            CGFloat w = (CGRectGetWidth(toolBGView.frame)-200)/3;

            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

            button.frame = CGRectMake(100+w*i, CGRectGetMaxY(lineView.frame)+40, w-1, 35);

            button.tag = 100+i;

            [button setTitle:titles[i] forState:UIControlStateNormal];

            if (i==1) {

                [button setTitle:@"播放" forState:UIControlStateSelected];

            }

            button.showsTouchWhenHighlighted = YES;

            [button addTarget:self action:@selector(tickButton:) forControlEvents:UIControlEventTouchUpInside];

            [toolBGView addSubview:button];

        }

    }

    #pragma mark -------滑杆 滑动 触发

    - (void)changeCurTime:(UISlider *)sender

    {

        player.currentTime = sender.value;

        

        int m = player.currentTime/60;

        int s = [@(player.currentTime)intValue]%60;

        curTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",m,s];

    }

    - (void)tickButton:(UIButton *)sender

    {

        switch (sender.tag) {

            case 100:

                if (curIndex>0) {

                    curIndex--;

                    [self updateAllMessage:self.allMusic[curIndex]];

                }

                break;

            case 101:

    //            播放 暂停

                if (player) {

                    

    //                进入播放页面 默认音乐是播放状态

    //                isPlaying 为YES

    //                点击按钮的时候  让selected 变成YES

    //                根据音乐播放的状态  改变按钮选中的状态

                    sender.selected = player.isPlaying;

                    

    //                如果 音乐正在播放(满足 player.isPlaying !=NO)点击按钮  让音乐暂停

                    player.isPlaying !=NO ? [player pause]:[player play];

                    

                    timer.fireDate = player.isPlaying !=YES ? [NSDate distantFuture]:[NSDate distantPast];

                    

                }

                

                

                break;

            case 102:

                if (curIndex == self.allMusic.count-1) {

                    curIndex = 0;

                    [self updateAllMessage:self.allMusic[curIndex]];

                    return;

                }

                

                if (curIndex<self.allMusic.count) {

                    curIndex++;

                    [self updateAllMessage:self.allMusic[curIndex]];

                }

                

                

                break;

            default:

                break;

        }

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

    {

        int curPage = scrollView.contentOffset.x/CGRectGetWidth([UIScreen mainScreen].bounds);

        pageControl.currentPage = curPage;

    }

    /*

    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation

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

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */

    @end

    //

    //  MusicInfoModel.h

    //  MusicPlayer-葫芦娃专属

    //

    //  Created by Bruce on 15/8/19.

    //  Copyright (c) 2015年 Bruce. All rights reserved.

    //

    #import <Foundation/Foundation.h>

     

    @interface MusicInfoModel : NSObject

     

    @property (nonatomic,copy) NSString *musicName;

    @property (nonatomic,copy) NSString *singerName;

    @property (nonatomic,copy) NSString *singerHeaderName;

    @property (nonatomic,copy) NSArray *desList;

     

    @end

  • 相关阅读:
    Java程序:从命令行接收多个数字,求和并输出结果
    大道至简读后感
    大道至简第一章读后感Java伪代码
    Creating a SharePoint BCS .NET Connectivity Assembly to Crawl RSS Data in Visual Studio 2010
    声明式验证超时问题
    Error message when you try to modify or to delete an alternate access mapping in Windows SharePoint Services 3.0: "An update conflict has occurred, and you must re-try this action"
    Upgrading or Redeploying SharePoint 2010 Workflows
    Upgrade custom workflow in SharePoint
    SharePoint 2013中Office Web Apps的一次排错
    How to upgrade workflow assembly in MOSS 2007
  • 原文地址:https://www.cnblogs.com/wukun16/p/4884138.html
Copyright © 2011-2022 走看看