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

音乐播放器

时间:2014-07-19 11:32:38      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   文件   2014   io   

 

bubuko.com,布布扣bubuko.com,布布扣

//  Copyright (c) 2014年 戴维营教育. All rights reserved.

 

 

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioPlayerDelegate>

{  AVAudioPlayer *player;// 建立一个对象

    NSArray *musicList;  //数组

    NSArray *pic;

    int  index;

    int  pi;            //定义一个全局变量

    NSTimer *timer;

    __weak IBOutlet UILabel *Lab;

    NSString *name; //字符串

    NSString *na;

    int i;

}

@property (weak, nonatomic) IBOutlet UISlider *UISlider;// 滑动条的一个属性

 

 

@end

 

@implementation ViewController

//-(void)puase

//{

//    [self puase];

//}

 

-(void)play

{  name= musicList[index];

    //1. 获取资源文件中的1.mp3的文件路径

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

    //    //2. 将文件读取到内存中

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

    //    //3. 创建一个播放器对象

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

    

    

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

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

    

    //4. 从文件路径生成一个URL对象

    NSURL *url=[NSURL fileURLWithPath:path];

    //AVAudioPlayer只能播放本地文件

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

    

    //使能,启动速率控制

    player.enableRate = YES;

    player.rate = 1;

    player.volume = 0.5;

    

   player.currentTime= player.deviceCurrentTime;

    //准备播放

    [player prepareToPlay];

    [player play];

    //

    player.delegate = self;

    //定时器  每隔0.1秒调用依次refreshProgress

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

}

 

-(void)img

{  na= pic[pi];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 220, 320, 260)];

    

    imageView.image = [UIImage imageNamed:na];

    [self.view addSubview:imageView];

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

   // 自动播放

    //[self  play];

//    pic= [NSArray arrayWithObjects:image1, image2, image3,image4,image5,nil];

    pic=@[@"01.png",@"2.png",@"3.png",@"4.png",@"5.png"];

    musicList = @[@"Nadiya - Roc",@"倩女幽魂",@"千千阙歌",@"愿得一人心(剧场版)",@"小苹果"];

    name= musicList[index];

    na = pic[pi];

    

    UIButton *Btn=[UIButton  buttonWithType:UIButtonTypeSystem];

    Btn.frame=CGRectMake(270, 20, 30, 30);

    [Btn setTitle:@"??" forState:UIControlStateNormal];

    [Btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:Btn];

 

    UIButton *Btn1=[UIButton  buttonWithType:UIButtonTypeSystem];

    Btn1.frame=CGRectMake(20, 20, 30, 30);

    [Btn1 setTitle:@"??" forState:UIControlStateNormal];

    [Btn1 addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:Btn1];

    

    

    UIButton *Btn2=[UIButton  buttonWithType:UIButtonTypeSystem];

    Btn2.frame=CGRectMake(140, 20, 30, 30);

    [Btn2 setTitle:@"??" forState:UIControlStateNormal];

    [Btn2 addTarget:self action:@selector(clicK) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:Btn2];

    

    self->Lab.text=[NSString stringWithFormat:@"%@",name];

    [self img];

 

}

 

 

 //暂停

-(void)clicK

{

    if(~i)

    {

    [self play];

    }

    else

    [player pause];

     i=~i;

 

}

 

-(void)Click{

    index--;//如果为零,将不会播放当前为0的歌曲,所以初值应该为-1

    if (index==-1) {

        index=musicList.count-1;//防止溢出

    }

    [self  play];

    self->Lab.text=[NSString stringWithFormat:@"%@",name];//显示歌名

    pi=index;

    [self  img];

}

 

-(void)click{

    index ++;

    if(index==musicList.count)//赋值并没有用到它

    {

        index=0;

    }

    [self play];

self->Lab.text=[NSString stringWithFormat:@"%@",name];

    pi=index;

    [self img];

}

//滑动条的方法

- (void)refreshProgress

{

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

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//播放之后的操作

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

{  index ++;

    if(index==musicList.count)

    {index=0;}

     [timer invalidate];

    [self play];

    pi=index;

    [self img];

}

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

    //当前时间等于滑动率乘上播放时间

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

}

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

    player.volume=sender.value;

}

@end

音乐播放器,布布扣,bubuko.com

音乐播放器

标签:blog   http   os   文件   2014   io   

原文地址:http://www.cnblogs.com/niang123/p/3854606.html

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