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

音乐播放器

时间:2014-07-22 00:38:36      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioPlayerDelegate>

{
    AVAudioPlayer *play;
    int index;
    NSArray *audiolist;
    NSMutableArray *imagelist;
    NSTimer *timer;
    int a;
    
}
@property (weak, nonatomic) IBOutlet UISlider *progressslier;

    

@end

@implementation ViewController
-(void)play
{
    NSString *name=audiolist[index];
    audiolist=@[@"1",@"2",@"3",@"4",@"5"];
    NSString *path=[[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
    NSURL *url=[NSURL fileURLWithPath:path];
    play=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    play.volume=0.5;
    play.enableRate=YES;
    play.rate=1;
    play.delegate=self;
    //play.currentTime=128;
    timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refreshprogress) userInfo:nil repeats:YES];
    play.currentTime=self.progressslier.value*play.duration;
    [play prepareToPlay];
    [play play];
   

}
-(void)bgview
{
    UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 160, 320, 480)];
    imagelist=[[NSMutableArray alloc]init];
    for(int i=0;i<5;i++)
    {
        NSString *name=[NSString stringWithFormat:@"%02d",i];
        //NSLog(@"%@",name);
        UIImage *image=[UIImage imageNamed:name];
        [imagelist addObject:image];
    }
    imageview.image=imagelist[index];
    [self.view addSubview:imageview];
    
}
-(void)creatbtn
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(130, 30, 80, 80);
    btn.backgroundColor=[UIColor redColor];
    [btn addTarget:self action:@selector(didclick:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *image=[UIImage imageNamed:@"tu1"];
    [btn setImage:image forState:UIControlStateNormal];
    [self.view addSubview:btn];
    //下一曲按钮
    UIButton *nextbtn=[UIButton buttonWithType:UIButtonTypeCustom];
    nextbtn.frame=CGRectMake(250, 50, 40, 40);
    [nextbtn setImage:[UIImage imageNamed:@"bfzn_002"]forState:UIControlStateNormal];
    [nextbtn addTarget:self action:@selector(didclick1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextbtn];
    //上一曲按钮
    UIButton *forwardbtn=[UIButton buttonWithType:UIButtonTypeCustom];
    forwardbtn.frame=CGRectMake(50, 50, 40, 40);
    [forwardbtn setImage:[UIImage imageNamed:@"bfzn_002"]forState:UIControlStateNormal];
    [forwardbtn addTarget:self action:@selector(didclick2:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forwardbtn];
    
    
}
-(void)refreshprogress
{
    self.progressslier.value=play.currentTime/play.duration;
}
- (IBAction)volumecontroll:(UISlider*)sender
{
    play.volume=sender.value;
}
- (IBAction)durationcontroll:(UISlider *)sender
{
   play.currentTime= play.duration*sender.value;
}
            
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self bgview ];
    [self creatbtn];
   
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    index++;
    if (index==audiolist.count)
    {
        index=0;
    }
  // [self didclick];
    [self play];
    [self bgview];
    [self creatbtn];
}
-(void)didclick2:(id *)sender
{
    if(index==0)
    {
        index=5;
    }
    index--;
    if (index==audiolist.count)
    {
        index=0;
    }
    [self play];
    [self bgview];
    [self creatbtn];
    
}
-(void)didclick1:(id *)sender
{
    index++;
    if (index==audiolist.count)
    {
        index=0;
    }
    [self play];
    [self bgview];
    [self creatbtn];
    
}
-(IBAction)didclick:(UIButton *)sender
{
    if(a==0)
    {
        
        [self play];
        a++;
    }
    else
    {
        [play pause];
        
        a--;
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

bubuko.com,布布扣

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

音乐播放器

标签:style   blog   http   color   os   io   

原文地址:http://www.cnblogs.com/sunxiang1991/p/3857690.html

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