#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
![]()