(一)准备工作
1.导入AVFoundation框架2.工程导入一个音频文件,向拖图片一样导入工程里。
#import "FHAudioViewController.h"
#import <AVFoundation/AVFoundation.h>
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface FHAudioViewController ()<AVAudioPlayerDelegate>{
AVAudioPlayer *_avAudioPlayer; // 播放器palyer
UISlider *_pregressSlider; // 播放进度
UILabel *_pregressLabel; // 进度
UISlider *_volumeSlider; // 声音控制
NSTimer *_timer; // 监控音频播放进度
}
@end
@implementation FHAudioViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.初识化UI
// (1)初始化三个Button;
NSArray *titleArr = @[@"播放",@"暂停",@"停止"];
for (int i = 0; i < titleArr.count; i++ ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:button];
[button setFrame:CGRectMake(20 + i * 50, 130 , 60, 40)];
[button setTitle:titleArr[i] forState:UIControlStateNormal];
button.tag = i+100;
[button addTarget:self action:@selector(controlAVAudioAction:) forControlEvents:UIControlEventTouchUpInside];
}
// (2)初始化进度条
_pregressSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 50, ScreenWidth - 130 - 20, 20)];
[self.view addSubview: _pregressSlider];
_pregressSlider.minimumValue = 0.0f;
_pregressSlider.maximumValue = 1.0f;
[_pregressSlider addTarget:self action:@selector(pregressChange) forControlEvents:UIControlEventValueChanged];
_pregressLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 120, 50, 100, 20)];
_pregressLabel.text = @"00:00/00:00";
[self.view addSubview:_pregressLabel];
// (3)用NSTimer来监控音频播放进度
_timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(playProgress) userInfo:nil repeats:YES];
// (4)初始化音量
_volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 90, ScreenWidth - 130 - 20, 20)];
[_volumeSlider addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];
_volumeSlider.minimumValue = 0.0f;
_volumeSlider.maximumValue = 10.0f;
_volumeSlider.value = 1.0f;
[self.view addSubview:_volumeSlider];
UILabel *volumeLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 120, 90, 40, 20)];
volumeLabel.text = @"音量";
[self.view addSubview:volumeLabel];
// 2.播放本地音频文件
// (1)从boudle路径下读取音频文件 陈小春 - 独家记忆文件名,mp3文件格式
NSString *path = [[NSBundle mainBundle] pathForResource:@"陈小春 - 独家记忆" ofType:@"mp3"];
// (2)把音频文件转化成url格式
NSURL *url = [NSURL fileURLWithPath:path];
// (3)初始化音频类 并且添加播放文件
_avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// (4) 设置代理
_avAudioPlayer.delegate = self;
// (5) 设置初始音量大小 默认1,取值范围 0~1
_avAudioPlayer.volume = 1;
// (6)设置音乐播放次数 负数为一直循环,直到stop,0为一次,1为2次,以此类推
_avAudioPlayer.numberOfLoops = 0;
// (5)准备播放
[_avAudioPlayer prepareToPlay];
}
效果
分析:用两个Slider来分别表示和控制播放进程和音量。三个按钮分别表示播放、暂停、控制。用NSTimer来每0.01秒改变一次播放进度。
(二)自定义方法
// 音频控制
- (void)controlAVAudioAction : (UIButton *)button {
NSInteger tag = button.tag;
// 播放
if (tag == 100) {
[_avAudioPlayer play];
}
// 暂停
if (tag == 101) {
[_avAudioPlayer pause];
}
// 停止
if (tag == 102) {
_avAudioPlayer.currentTime = 0;
[_avAudioPlayer stop];
}
}
// 播放进度控制
- (void)pregressChange{
_avAudioPlayer.currentTime = _pregressSlider.value * _avAudioPlayer.duration;
}
// 播放进度条
- (void)playProgress {
// 更改当前播放时间
NSString *currentMStr = [self FormatTime:_avAudioPlayer.currentTime / 60];
NSString *currentSStr = [self FormatTime:(int)_avAudioPlayer.currentTime % 60];
NSString *durationMStr = [self FormatTime:_avAudioPlayer.duration / 60];
NSString *durationSStr = [self FormatTime:(int)_avAudioPlayer.duration % 60];
_pregressLabel.text = [NSString stringWithFormat:@"%@:%@/%@:%@",currentMStr,currentSStr,durationMStr,durationSStr];
// 播放进度条
_pregressSlider.value = _avAudioPlayer.currentTime / _avAudioPlayer.duration;
}
- (NSString *)FormatTime: (int)time {
if (time < 10) {
return [NSString stringWithFormat:@"0%d",time];
}else {
return [NSString stringWithFormat:@"%d",time];
}
}
// 音量控制
- (void)volumeChange {
_avAudioPlayer.volume = _volumeSlider.value;
}
分析:我主要说一下音量,其他的方法写的都很清楚了。音量这个比较特殊。
_avAudioPlayer.volume = 1;
:
这个值是百分比,设置的就是视频播放器音量是系统音量的百分之多少。好比说系统音量是0.5,这个值设置成0.6,此时音频播放器的音量也就是你听到的音量相当于系统音量的0.3;这里系统音量指:
那个中间喇叭的音量。它的取值范围也是0~1;
// 获取当前系统音量
- (float)getCurrentDeviceVolumu {
float volume = [[AVAudioSession sharedInstance] outputVolume]; NSLog(@"getCurrentDeviceVolumu = %f",volume);
return volume;
}
(三)AVAudioPlayerDelegate
// 当播放完成时执行的代理
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
NSLog(@"audioPlayerDidFinishPlaying");
_timer = nil;
[_timer invalidate];
}
// 当播放发生错误时调用
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error {
NSLog(@"播放发生错误%@",error);
}
// 当播放器发生中断时调用 如来电
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
NSLog(@"audioPlayerBeginInterruption");
// 暂停播放 用户不暂停,系统也会帮你暂停。但是如果你暂停了,等来电结束,需要再开启
[_avAudioPlayer pause];
}
// 当中断停止时调用 如来电结束
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags {
NSLog(@"audioPlayerEndInterruption");
// 你可以帮用户开启 也可以什么都不执行,让用户自己决定
[_avAudioPlayer play];
}