IOS学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件(2)
来源:未知 责任编辑:责任编辑 发表时间:2015-03-01 01:30 点击:次
{
[super viewDidLoad];
UIButton *musicPlay = [UIButton buttonWithType:UIButtonTypeRoundedRect];
musicPlay.frame = CGRectMake(10, 10, 90, 35);
[musicPlay setTitle:@"Play" forState:UIControlStateNormal];
[musicPlay addTarget:self action:@selector(playMusic) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:musicPlay];
UIButton *pause = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pause.frame = CGRectMake(115, 10, 90, 35);
[pause setTitle:@"Pause" forState:UIControlStateNormal];
[pause addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pause];
UIButton *stop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
stop.frame = CGRectMake(220, 10, 90, 35);
[stop setTitle:@"stop" forState:UIControlStateNormal];
[stop addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stop];
slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 65, 300, 20)];
[slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
//
NSString *path = [[NSBundle mainBundle] pathForResource:@"找一个相爱的理由-晨熙-艾歌" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// 设置循环次数,-1为一直循环
player.numberOfLoops = -1;
// 准备播放
[player prepareToPlay];
// 设置播放音量
player.volume = 50;
// 当前播放位置,即从currentTime处开始播放,相关于android里面的seekTo方法
player.currentTime = 15;
// 设置代理
player.delegate = self;
int dur = player.duration;
slider.maximumValue = dur;
// 一秒一次更新播放进度
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>