播放音乐
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic, strong) AVAudioPlayer *player;
@end
@implementation ViewController
- (IBAction)playClick:(id)sender {
[self.player play];
}
- (IBAction)pauseClick:(id)sender {
[self.player pause];
}
- (void)viewDidLoad {
[super viewDidLoad];
//音乐播放
//告诉播谁?
NSString *path = [[NSBundle mainBundle]pathForResource:@"我爱你你却爱着她.mp3" ofType:nil];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
self.player = player;
}
==============================================
录音
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic, strong) AVAudioRecorder *recorder;
@property (nonatomic,strong)NSTimer *mainTime;
@property (nonatomic,assign)NSTimeInterval timeValue; //跟2秒进行比较
- (IBAction)stopClick:(id)sender;
@property (nonatomic) NSInteger index;
- (IBAction)recoderClick:(id)sender;
@end
@implementation ViewController
- (void)mainUpdate //1/60.0
{
//1.时刻知道用户是否在说话 2.用户两秒钟不说话就停止录音
//分贝 描述了声音的大小
//刷新分贝值
[self.recorder updateMeters]; //刷新之后又新的分贝
NSLog(@"%lf",[self.recorder averagePowerForChannel:1]);
CGFloat value = [self.recorder averagePowerForChannel:1];
if (value > -30) { //在说话
self.timeValue = 0;
}else{ //沉默
self.timeValue += 1/60.0;
if (self.timeValue > 2) { //可以停止了
[self.recorder stop];
//合适的时间销毁定时器
[self.mainTime invalidate];
self.mainTime = nil;
}else{ //不能
}
}
NSLog(@"11111");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_index ++;
//录制 完成之后需要保存着沙盒
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:[NSString stringWithFormat:@"hello%zd.wav",_index]];
//移除之前的文件 如果没有 就什么 都不做
//[[NSFileManager defaultManager]removeItemAtPath:path error:nil];
NSLog(@"%@",path);
AVAudioRecorder *recorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:nil error:nil];
self.timeValue = 0;
self.recorder = recorder;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)stopClick:(id)sender {
//停止的时候自动保存
[self.recorder stop];
}
- (IBAction)recoderClick:(id)sender {
//定时器
[self.recorder record];
self.mainTime = [NSTimer scheduledTimerWithTimeInterval:1/60.0 target:self selector:@selector(mainUpdate) userInfo:nil repeats:YES];
self.recorder.meteringEnabled = YES;
}
- (void)test
{
//激活 会话 设置 分类模式 : 就可以在真机上进行录制操作
[[AVAudioSession sharedInstance]setActive:YES error:nil];
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryRecord error:nil];
//录制 完成之后需要保存着沙盒
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"hellonihao.aac"];
NSLog(@"%@",path);
//参数二 配置音频参数 (控制类型跟大小 ) : 声道 采样率 赫兹 专业的专家
//创建录音对象
//setting 是设置参数 录音 : 编码率 声道 等 关于音频的参数
// //settings 设置参数 录音相关参数 声道 速率 采样率
NSMutableDictionary *setting = [NSMutableDictionary dictionary];
//2.够着 录音参数
// 音频格式
setting[AVFormatIDKey] = @(kAudioFormatMPEG4AAC);
// 音频采样率
setting[AVSampleRateKey] = @(8000.0);
// 音频通道数
setting[AVNumberOfChannelsKey] = @(1);
// 线性音频的位深度
setting[AVLinearPCMBitDepthKey] = @(8);
AVAudioRecorder *recorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:setting error:nil];
self.recorder = recorder;
}
@end
播放音效
- (void)test
{
//音效 :比较短的音乐效果 30秒以内
//C语言的API
// 直接是函数 实现 输入 参数 输出 返回值 传地址参数
// SystemSoundID soundID = 0; //音效的标识 区分音效文件
//
// NSString *path = [[NSBundle mainBundle]pathForResource:@"normal.aac" ofType:nil];
// NSURL *url = [NSURL fileURLWithPath:path]; //file://
// //[NSURL URLWithString:@""]; http:
// //协议头: http:// tel:// email:// ftp:// file://
// //__bridge 桥接 CF F 之间转换 会用到桥接
// //__bridge 仅仅时候对象的转换
// //__bridge_transfer 桥接 对象所有权的转移 CF -> ocF(ARC)
// //__bridge_retained
//
// //对象都有生命周期
// AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
//
// NSLog(@"%d",soundID);
// //播放音效
// AudioServicesPlaySystemSound(soundID);
//在静音模式下有 震动效果
//AudioServicesPlayAlertSound(soundID);
}
#import "CZAudioTool.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[CZAudioTool playAudioWithName:@"m_08.wav"];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//音效很多地方都会用到 工具类问题: 2.已经创建过的音效文件没必要再创建 1.没有考虑特殊参数
[CZAudioTool playAudioWithName:@"m_08.wav"];
}
#import "CZAudioTool.h"
#import <AVFoundation/AVFoundation.h>
static NSMutableDictionary *_allAudio;
@implementation CZAudioTool
+(void)load
{
NSLog(@"load");
_allAudio = [NSMutableDictionary dictionary];
}
+(void)initialize
{
NSLog(@"initialize");
}
+ (void)playAudioWithName:(NSString *)name
{
if (name == nil) {
NSLog(@"音效文件名称为空!");
return;
}
SystemSoundID soundID = 0; //音效的标识 区分音效文件
//2.保存之前创建好的
//1.取出之前创建好的 不用重新创建
//key : name value : soundID
soundID = [_allAudio[name] unsignedIntValue];
if (soundID == 0) {
NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:nil];
if (path == nil) {
NSLog(@"音效文件不存在");
return;
}
NSURL *url = [NSURL fileURLWithPath:path]; //file://
//对象都有生命周期
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
//保存到字典中
_allAudio[name] = @(soundID);
}
NSLog(@"%d",soundID);
//播放音效
AudioServicesPlaySystemSound(soundID);
}
+(void)disposeAudioName:(NSString *)name
{
SystemSoundID soundID = [_allAudio[name] unsignedIntValue];
if (soundID != 0) {
AudioServicesDisposeSystemSoundID(soundID);
}
}
@end