#import "ViewController.h"
@interface ViewController ()<</span>AVAudioPlayerDelegate>
@property(nonatomic,strong)UIProgressView * progressView;
@property(nonatomic,strong)AVAudioPlayer * player;
@property(nonatomic,strong)NSTimer * timer;
-(void)timerprogress;
@property(nonatomic,strong)UISlider * slider;
-(void)tapslider;
@end
@implementation ViewController
- (IBAction)Stop:(id)sender {
[self.player stop];
self.player.currentTime = 0;
}
- (IBAction)zanting:(id)sender {
[self.player pause];
}
- (IBAction)Start:(id)sender {
[self.player play];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(80, 400, 250, 2)];
self.progressView.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.progressView.progress = 0;
[self.view addSubview:self.progressView];
NSString * filePath = [[NSBundle mainBundle]pathForResource:@"tianyaguoke" ofType:@"mp3"];
NSURL * url = [NSURL fileURLWithPath:filePath];
self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:urlerror:nil];
self.player.volume = 0.5;
self.player.numberOfLoops = -1;//循环播放
//准备歌曲播放
[self.player prepareToPlay];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1target:self selector:@selector(timerprogress) userInfo:nilrepeats:YES];
self.slider = [[UISlider alloc]initWithFrame:CGRectMake(80, 450, 210, 2)];
self.slider.maximumValue = 3;
self.slider.minimumValue = 0;
self.slider.value = 0.3;
[self.slider addTarget:self action:@selector(tapslider)forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.slider];
self.player.delegate = self;
}
-(void)timerprogress
{
self.progressView.progress = (self.player.currentTime /self.player.duration);
}
-(void)tapslider
{
self.player.volume = self.slider.value;
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self.timer invalidate];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
转载于:https://www.cnblogs.com/ylg-----/p/4753018.html
相关资源:ios-音乐播放器(AVAudioPlayer).zip