播放音乐
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic,strong) AVAudioPlayer *player;
@end
@implementation ViewController
- (IBAction)playClick:(id)sender {
[self.playerplay];
}
- (IBAction)pauseClick:(id)sender {
[self.playerpause];
}
- (void)viewDidLoad {
[superviewDidLoad];
//音乐播放
//告诉播谁?
NSString *path = [[NSBundlemainBundle]pathForResource:@"我爱你你却爱着她.mp3"ofType:nil];
AVAudioPlayer *player = [[AVAudioPlayeralloc]initWithContentsOfURL:[NSURLfileURLWithPath: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.recorderupdateMeters]; //刷新之后又新的分贝
NSLog(@"%lf",[self.recorderaveragePowerForChannel:1]);
CGFloat value = [self.recorderaveragePowerForChannel:1];
if (value > -30) {//在说话
self.timeValue =0;
}else{ //沉默
self.timeValue +=1/60.0;
if (self.timeValue >2) { //可以停止了
[self.recorderstop];
//合适的时间销毁定时器
[self.mainTimeinvalidate];
self.mainTime =nil;
}else{ //不能
}
}
NSLog(@"11111");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_index ++;
//录制完成之后需要保存着沙盒
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:[NSStringstringWithFormat:@"hello%zd.wav",_index]];
//移除之前的文件 如果没有 就什么都不做
//[[NSFileManager defaultManager]removeItemAtPath:path error:nil];
NSLog(@"%@",path);
AVAudioRecorder *recorder = [[AVAudioRecorderalloc]initWithURL:[NSURLfileURLWithPath:path] settings:nilerror:nil];
self.timeValue =0;
self.recorder = recorder;
}
- (void)viewDidLoad {
[superviewDidLoad];
}
- (IBAction)stopClick:(id)sender {
//停止的时候自动保存
[self.recorderstop];
}
- (IBAction)recoderClick:(id)sender {
//定时器
[self.recorderrecord];
self.mainTime = [NSTimerscheduledTimerWithTimeInterval:1/60.0target:selfselector:@selector(mainUpdate)userInfo:nilrepeats:YES];
self.recorder.meteringEnabled =YES;
}
- (void)test
{
//激活会话 设置分类模式 : 就可以在真机上进行录制操作
[[AVAudioSessionsharedInstance]setActive:YESerror:nil];
[[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryRecorderror:nil];
//录制完成之后需要保存着沙盒
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"hellonihao.aac"];
NSLog(@"%@",path);
//参数二 配置音频参数 (控制类型跟大小 ) : 声道 采样率 赫兹 专业的专家
//创建录音对象
//setting 是设置参数 录音 : 编码率 声道 等 关于音频的参数
// //settings 设置参数 录音相关参数 声道 速率 采样率
NSMutableDictionary *setting = [NSMutableDictionarydictionary];
//2.够着 录音参数
// 音频格式
setting[AVFormatIDKey] =@(kAudioFormatMPEG4AAC);
// 音频采样率
setting[AVSampleRateKey] = @(8000.0);
// 音频通道数
setting[AVNumberOfChannelsKey] =@(1);
// 线性音频的位深度
setting[AVLinearPCMBitDepthKey] =@(8);
AVAudioRecorder *recorder = [[AVAudioRecorderalloc]initWithURL:[NSURLfileURLWithPath:path] settings:settingerror: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 {
[superviewDidLoad];
[CZAudioToolplayAudioWithName:@"m_08.wav"];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//音效很多地方都会用到工具类问题: 2.已经创建过的音效文件没必要再创建 1.没有考虑特殊参数
[CZAudioToolplayAudioWithName:@"m_08.wav"];
}
#import "CZAudioTool.h"
#import <AVFoundation/AVFoundation.h>
static NSMutableDictionary *_allAudio;
@implementation CZAudioTool
+(void)load
{
NSLog(@"load");
_allAudio = [NSMutableDictionarydictionary];
}
+(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 = [[NSBundlemainBundle]pathForResource:nameofType:nil];
if (path == nil) {
NSLog(@"音效文件不存在");
return;
}
NSURL *url = [NSURLfileURLWithPath:path]; //file://
//对象都有生命周期
AudioServicesCreateSystemSoundID((__bridgeCFURLRef _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