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

简单本地音乐播放器

时间:2014-07-24 14:37:45      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   文件   io   2014   

//

//  ViewController.m

//  AudioPlayer

//

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

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

// 

 

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

 

@interface ViewController ()<AVAudioPlayerDelegate>

{

    AVAudioPlayer *player;

    NSArray *playlist;

    long int index;

    UIImageView *imageview;

    

    NSTimer *timer;

}

@property (weak, nonatomic) IBOutlet UISlider *didclickedPorgress;

@property IBOutlet UILabel *label;

 

@end

 

@implementation ViewController

 

 

- (void)createLabels

{

    _label=[[UILabel alloc] initWithFrame:CGRectMake(50,360,220,20)];

    _label.textColor=[UIColor whiteColor];

    _label.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:_label];

    

}

- (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

{

    //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];

    

    

    

    //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)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 (index == playlist.count-1)

      {

          index = 0;

      }

      else

      {

          index++;

      }

        [self play];

       

    }

    else

    {

        if (index == playlist.count-1)

        {

           index = 0;

        }

        else

        {

            index++;

           

        }

        [self play];

        [player stop];

    }

    NSLog(@"The next song");

    

}

    

 

    

- (void)didprevclicked:(UIButton *)sender

{

    if (player.playing)

    {

      if(index==0)

        {

         index=playlist.count-1 ;

        }

      else

        {

        index=index-1;

        }

    [self play];

    }

    else

    {

        if(index==0)

        {

            index=playlist.count-1 ;

        }

        else

        {

            index=index-1;

        }

        [self play];

        [player stop];

    }

    NSLog(@"The previous song");

    

}

 

 

 

-(void)refreshProgress

{

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

}

 

 

 

- (IBAction)didvolumechanged:(UISlider *)sender {

    player.volume = sender.value;

}

- (IBAction)didprogresschanged:(UISlider *)sender {

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

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self createbjview];

    [self createLabels];

    [self createButtons];

    

    playlist =@[@"给我一个理由忘记",@"拯救",@"后会无期"];

    

    [self play];

    [player stop];

}

 

- (void)play

{

    //显示专辑图片

    NSString *imageName =[NSString stringWithFormat:@"%ld.jpg", index];

    imageview.image=[UIImage imageNamed:imageName];

    NSString *name = playlist[index];

    //    显示歌名

    _label.text=[NSString stringWithFormat:@"%@",playlist[index]];

    //获取本地音乐的文件路径

    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只能播放本地文件

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

    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;

    //每隔1秒调用依次refreshProgress

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

    

}

 

 

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

@end

file:///Users/apple/Desktop/屏幕快照%202014-07-23%20下午9.59.35.png 

file:///Users/apple/Desktop/屏幕快照%202014-07-23%20下午10.00.05.png

file:///Users/apple/Desktop/屏幕快照%202014-07-23%20下午10.00.36.png

简单本地音乐播放器,布布扣,bubuko.com

简单本地音乐播放器

标签:des   style   http   color   os   文件   io   2014   

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

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