码迷,mamicode.com
首页 > 其他好文 > 详细

升级版音乐播放器

时间:2014-07-26 01:35:17      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   使用   os   文件   数据   

//

//  secondViewController.m

//  AudioPlayer

//

//  Created by apple on 14-7-24.

//  Copyright (c) 2014年 苹果IOS软件开发者. All rights reserved.

//

 

#import "secondViewController.h"

#import "ViewController.h"

 

@interface secondViewController ()<UITableViewDataSource,UITableViewDelegate>

 

 

 

@end

 

@implementation secondViewController

{

    NSArray *playlist;

    

}

 

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    //(0,0,self.view.frame.size.width,self.view.frame.height)

    UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 14, 320, 466) style:UITableViewStylePlain];

    //设置数据源

    tableview.dataSource = self;

    tableview.delegate = self;

    tableview.rowHeight = 30;

    [self.view addSubview:tableview];

    playlist = @[@"拯救",@"给我一个理由忘记",@"小苹果",@"江南Style"];

    

    

}

 

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

#pragma mark - UITableViewDataSource

//这是UITableViewDataSource里面的方法

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

{

    //返回表格的行数

    return playlist.count;

}

 

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

{

    //使用重用标示获取已有的Cell

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

    if (cell == nil)

    {

        //使用一个重要标示创建一个Cell

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

    }

    

    //在cell上显示文字

    cell.textLabel.text = [NSString stringWithFormat:@"%ld.%@",(long)indexPath.row,playlist[indexPath.row]];

    return cell;

}

 

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

{

    

       NSLog(@"did select row: %d",indexPath.row);

    ViewController *Ctrl = [[ViewController alloc] initWithNibName:nil bundle:nil];

    [Ctrl myRow:indexPath.row];

    //延时加载

    [self presentViewController:Ctrl animated:YES completion:^{NSLog(@"...completion");}];

}

 

 

#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

 

 

 

 

//

//  ViewController.m

//  AudioPlayer

//

//  Created by apple on 14-7-18.

//  Copyright (c) 2014年 苹果IOS软件开发者. All rights reserved.

// 

 

#import "ViewController.h"

#import "secondViewController.h"

#import <AVFoundation/AVFoundation.h>

 

 

@interface ViewController ()<AVAudioPlayerDelegate>

{

    int _myRow;

    AVAudioPlayer *player;

    UIImageView *imageview;

    NSTimer *timer;

    NSArray *playlist;

}

@property IBOutlet UISlider *progressSlider;

@property IBOutlet UILabel  *musicname;

 

@end

 

@implementation ViewController

 

- (void)myRow:(int)row

{

    _myRow = row;

    

}

 

 

- (void)createbjview

{

    //设置音量图标

    UIImageView *volimageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 15, 64, 64)];

    volimageview.image = [UIImage imageNamed:@"vol"];

    [self.view addSubview:volimageview];

    

    

   imageview = [[UIImageView alloc ]initWithFrame:CGRectMake(0, 0, 320, 480)];

       imageview.image = [UIImage imageNamed:@"0"];

    [self.view addSubview:imageview];

    

    [self.view sendSubviewToBack:imageview];

}

 

-(void)createButtons

{

    

    

    //2.上一曲/下一曲按钮

    UIButton *nextbtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [nextbtn setImage:[UIImage imageNamed:@"next"] forState:UIControlStateNormal];

    nextbtn.frame = CGRectMake(224, 410, 64, 64);

    [nextbtn addTarget:self action:@selector(didnextclicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:nextbtn];

    

    UIButton *prevbtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [prevbtn setImage:[UIImage imageNamed:@"prev"] forState:UIControlStateNormal];

    prevbtn.frame = CGRectMake(32, 410, 64, 64);

    [prevbtn addTarget:self action:@selector(didprevclicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:prevbtn];

    

}

 

- (void)createSlider

{

    UISlider *volumeSlider=[[UISlider alloc] initWithFrame:CGRectMake(60, 35, 220, 10)];

    volumeSlider.value = 0.5;

    volumeSlider.maximumValue= 1 ;

    volumeSlider.minimumValue= 0 ;

    [volumeSlider addTarget:self action:@selector(Volume:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:volumeSlider];

    

    _progressSlider=[[UISlider alloc]initWithFrame:CGRectMake(30,390 ,260 ,10 )];

    _progressSlider.tintColor = [UIColor blueColor];_progressSlider.minimumValue = 0;

    _progressSlider.maximumValue = 1;

    [_progressSlider addTarget:self action:@selector(Progress:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:_progressSlider];

}

 

- (void)createLabel

{

    _musicname=[[UILabel alloc]initWithFrame:CGRectMake(60, 350, 200, 20)];

    _musicname.textColor=[UIColor whiteColor];

    _musicname.textAlignment=NSTextAlignmentCenter;

    _musicname.text=[NSString stringWithFormat:@"%@",playlist[_myRow]];

    [self.view addSubview:_musicname];

}

        

 

    

 

 

 

- (void)didclicked:(id)sender

{

    UIButton *playorstopbtn =(UIButton *)sender;

        //判断当前的播放状态

    if(player.playing)

    {

        

        playorstopbtn.selected = NO;

        [player pause];

    }

    else

    {

        playorstopbtn.selected = YES;

        [player play];

    }

 

}

- (void)didnextclicked:(UIButton *)sender

{

    if(player.playing)

    {

         if (_myRow == playlist.count-1)

      {

          _myRow = 0;

      }

      else

      {

          _myRow++;

      }

        [self play];

    }

    else{

        if (_myRow == playlist.count-1)

        {

            _myRow = 0;

        }

        else

        {

            _myRow++;

        }

        [player pause];

    }

    NSLog(@"The next song");

    

}

    

 

    

- (void)didprevclicked:(UIButton *)sender

{

    if(player.playing)

     {

         if(_myRow==0)

        {

         _myRow=playlist.count-1 ;

        }

      else

        {

        _myRow=_myRow-1;

        }

       [self play];

     }

    else

    {

        if(_myRow==0)

        {

            _myRow=playlist.count-1 ;

        }

        else

        {

            _myRow=_myRow-1;

        }

        [player pause];

 

    }

 

    NSLog(@"The previous song");

    

}

 

- (void)Volume:(UISlider *)sender

{

    player.volume = sender.value;

    NSLog(@"%f",sender.value);

}

- (void)Progress:(UISlider *)sender {

    player.currentTime =sender.value *player.duration;

}

 

- (void)refreshProgress

{

     self.progressSlider.value=player.currentTime/player.duration;

}

- (void)viewDidLoad {

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

    [btn setTitle:@"Back" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(didClicked) forControlEvents:UIControlEventTouchUpInside];

    btn.frame = CGRectMake(280, 20, 40, 20);

    [self.view addSubview:btn];

    [super viewDidLoad];

    [self createbjview];

    [self createButtons];

    [self createSlider];

    [self createLabel];

        playlist = @[@"拯救",@"给我一个理由忘记",@"小苹果",@"江南Style"];

    [self play];

//    [player stop];

}

 

- (void)didClicked

{

    [timer invalidate];

    //使模态视图消失

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

- (void)play

{

    //1.播放/暂停按钮

    UIButton *playorstopbtn = [UIButton buttonWithType:UIButtonTypeCustom];

    //设置按钮的图片

    [playorstopbtn setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];

    [playorstopbtn setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateSelected];

    playorstopbtn.frame = CGRectMake(128, 410, 64, 64);

    [playorstopbtn addTarget:self action:@selector(didclicked:) forControlEvents:UIControlEventTouchUpInside];

    playorstopbtn.adjustsImageWhenHighlighted =FALSE;

    [self.view addSubview:playorstopbtn];

    

    

    

    //显示专辑图片

    NSString *imageName =[NSString stringWithFormat:@"%d.jpg", _myRow];

    imageview.image=[UIImage imageNamed:imageName];

    NSString *name = playlist[_myRow];

     _musicname.text=[NSString stringWithFormat:@"%@",playlist[_myRow]];        //获取本地音乐的文件路径

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

    //将文件读入内存中

    //    NSData *data = [NSData dataWithContentsOfFile:path];

    //创建一个播放器对象

    //player = [[AVAudioPlayer alloc] initWithData:data error:nil];

    //网络地址用下面的方法

    //[NSURL URWithString:@"http://www.baidu.com"];

    //    //从文件路径生成一个url对象

    NSURL *url = [NSURL fileURLWithPath:path];

    //AVAudioPlayer只能播放本地文件

    NSError *err;

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];

    NSLog(@"err:%@",err);

    player.delegate = self;

    

    //使能,播放速率控制,音量初始值

    player.enableRate = YES;

    player.rate = 1;

    player.volume = 0.5;

    

    NSLog(@"%f",player.volume);

    

    [player prepareToPlay];

    [player play];

       //从currentTime开始播放

    //player.currentTime = 100;

    if(player.playing)

    {

    

        playorstopbtn.selected = YES;

    }

    

    [timer invalidate];

        //每隔1秒调用依次refreshProgress

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

    

}

 

 

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

@end

bubuko.com,布布扣 

 

升级版音乐播放器,布布扣,bubuko.com

升级版音乐播放器

标签:des   style   http   color   使用   os   文件   数据   

原文地址:http://www.cnblogs.com/z-j-w/p/3869095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!