1:AVAudioPlayer的基本概念
2:AVAudioPlayer的创建
3:AVAudioPlayer的使用
volume:音量设置
numberOfLoops:循环播放次数
prepareToPlay:准备播放
play:播放
stop:停止
pause:暂停
//
// ViewController.m
// AVAudio音频播放
//
// Created by 刘群 on 2018/1/22.
// Copyright © 2018年 刘群. All rights reserved.
//
#import "ViewController.h"
//导入音视频系统库文件
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>
{
//播放按钮
UIButton *_btnPlay;
//暂停按钮
UIButton *_btnPause;
//停止播放
UIButton *_btnStop;
//播放进度条
UIProgressView *_progressV;
//调整音量大小滑动条
UISlider *_volumeSlider;
//静音开关
UISwitch *_jingyin;
//音频播放器对象
AVAudioPlayer *_plyer;
//定义声明一个定时器对象
NSTimer *_timer;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//播放按钮
_btnPlay = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnPlay.frame = CGRectMake(100, 100, 100, 40);
[_btnPlay setTitle:@"播放" forState:0];
[_btnPlay addTarget:self action:@selector(pressPlay) forControlEvents:UIControlEventTouchUpInside];
//暂停按钮
_btnPause= [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnPause.frame = CGRectMake(100, 160, 100, 40);
[_btnPause setTitle:@"暂停" forState:0];
[_btnPause addTarget:self action:@selector(pressPaus) forControlEvents:UIControlEventTouchUpInside];
//停止a按钮
_btnStop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnStop.frame = CGRectMake(100, 220, 100, 40);
[_btnStop setTitle:@"停止" forState:0];
[_btnStop addTarget:self action:@selector(pressStop) forControlEvents:UIControlEventTouchUpInside];
//进度条
_progressV = [[UIProgressView alloc]initWithFrame:CGRectMake(10, 300, 300, 0)];
_progressV.progress = 0;
//声音控制滑动条
_volumeSlider = [[UISlider alloc]initWithFrame:CGRectMake(10, 380, 300, 20)];
_volumeSlider.maximumValue = 100;
_volumeSlider.minimumValue = 0;
[_volumeSlider addTarget:self action:@selector(volumeChange:) forControlEvents:UIControlEventValueChanged];
//静音开关
_jingyin = [[UISwitch alloc]initWithFrame:CGRectMake(200, 420, 0, 0)];
_jingyin.on = YES;
[_jingyin addTarget:self action:@selector(jingyinChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_btnPlay];
[self.view addSubview:_btnPause];
[self.view addSubview:_btnStop];
[self.view addSubview:_progressV];
[self.view addSubview:_volumeSlider];
[self.view addSubview:_jingyin];
[self createAVPlayer];
}
- (void)createAVPlayer{
//获取本地资源文件路径
NSString *musicPath = [[NSBundle mainBundle]pathForResource:@"111" ofType:@"mp3"];
_plyer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:nil];
//准备播放,解码工作
[_plyer prepareToPlay];
//循环播放的次数
//-1:无限循环播放
_plyer.numberOfLoops = -1;
_plyer.volume = 0.5;
//定义一个定时器
//更新进度条
//_timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateT) userInfo:nil repeats:YES];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateT) userInfo:nil repeats:YES];
_plyer.delegate = self;
}
- (void)updateT{
_progressV.progress = _plyer.currentTime/_plyer.duration;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
//音乐播放完成 停掉定时器
[_timer invalidate];
}
- (void)pressPlay{
NSLog(@"播放");
[_plyer play];//开始播放
}
- (void)pressPaus{
NSLog(@"暂停");
[_plyer pause];
}
- (void)pressStop{
NSLog(@"停止");
[_plyer stop];//停止播放
_plyer.currentTime = 0;//当前播放时间清零
}
- (void)volumeChange:(UISlider*)slider{
NSLog(@"%.2f",slider.value);
//音量大小
_plyer.volume = slider.value/100;
}
- (void)jingyinChange:(UISwitch*)sw{
NSLog(@"静音");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end