您的位置:首页 > 运维架构

多媒体处理AVAudioPlayer

2013-11-28 10:06 387 查看
AVAudioPlayer :只能播放本地音频

AVPlayer :可以播放远程音乐,也可以播放本地,

音频队列:主要处理流媒体播放,提供api接口(C函数接口),处理起来复杂。

//文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"做我老婆好不好" ofType:@"mp3"];
//转换成URL
NSURL *url = [NSURL fileURLWithPath:filePath];
//播放器类
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
[audioPlayer prepareToPlay];
if ([audioPlayer play]){
NSLog(@"begin to play");
}
[audioPlayer release];
- (void)loadView
{
[super loadView];

//文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"做我老婆好不好" ofType:@"mp3"];
//转换成URL
NSURL *url = [NSURL fileURLWithPath:filePath];
//播放器类
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];

[audioPlayer prepareToPlay];

UILabel *sound = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, 50, 20)];
sound.text = @"音量";
[self.view addSubview:sound];
[sound release];

UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(110, 100, 190, 20)];
slider.minimumValue = 0;
slider.maximumValue = 1;
slider.value = 0.3;
slider.tag = 101;
[slider addTarget:self action:@selector(soundAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
[slider release];

UILabel *timer = [[UILabel alloc]initWithFrame:CGRectMake(20, 130, 50, 20)];
timer.tag = 103;
timer.text = [self timerChang:0];
[self.view addSubview:timer];
[timer release];

UISlider *slider_progress = [[UISlider alloc]initWithFrame:CGRectMake(110, 130, 190, 20)];
slider_progress.minimumValue = 0;
slider_progress.maximumValue = audioPlayer.duration;
slider_progress.tag = 102;
[slider_progress addTarget:self action:@selector(currentAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider_progress];
[slider_progress release];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 180, 80, 40);
[button setTitle:@"播放" forState:UIControlStateNormal];
[button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

nsTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

- (void)timerAction:(NSTimer *)timer
{
NSString *currtime = [self timerChang:audioPlayer.currentTime];
UILabel *label = (UILabel *) [self.view viewWithTag:103];
label.text = currtime;

UISlider *slider = (UISlider *)[self.view viewWithTag:102];
slider.value = audioPlayer.currentTime;
}

- (NSString *)timerChang:(float)seconds{
NSInteger temp = (NSInteger)seconds;
return [NSString stringWithFormat:@"%02d:%02d", temp / 60, temp % 60];
}

- (void)soundAction:(UISlider *)slider
{
audioPlayer.volume = slider.value;
}

- (void)currentAction:(UISlider *)slider
{
audioPlayer.currentTime = slider.value;
}

- (void)play:(UIButton *)button
{
if (audioPlayer.playing){
[audioPlayer pause];
[button setTitle:@"播放" forState:UIControlStateNormal];

}else{
[audioPlayer play];
[button setTitle:@"暂停" forState:UIControlStateNormal];
}
}

- (void)dealloc
{
[audioPlayer release];
[super dealloc];
}

#pragma mark AVAudioPlayer delegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (flag){
NSLog(@"播放结束");
[nsTimer invalidate];
}
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: