因为自己的项目领域,基本没有涉及到视频,音频等内容,所以逐渐的很陌生,脑袋也记不太多相关,所以今天又重新温习了一遍。好记性不如烂笔头嘛~hold on。
1.音效的播放
1.获得音效文件的路径
NSURL *url = [[NSBundle mainBundle] URLForResource:@"文件名称" withExtension:nil];
2.加载音效文件,得到对应的音效ID
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
3.播放音效
AudioServicesPlaySystemSound(soundID);
2.录音
-
录音步骤
1.导入AVFoundation框架#import <AVFoundation/AVFoundation.h>
2.使用类AVAudioRecorder进行录音
-
创建录音文件存放路径
一般是沙盒路径NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"text.caf"]; NSURL *url = [NSURL URLWithString:path];
-
设置录音附加设置项(#import <AVFoundation/AVAudioSettings.h>)
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
设置编码格式
[recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];
采样率
[recordSettings setValue :[NSNumber numberWithFloat:11025.0] forKey: AVSampleRateKey];44100.0
通道数
[recordSettings setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];
音频质量,采样质量
[recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
-
根据路径以及设置项, 创建录音对象
_audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:nil];
-
准备录音
[self.audioRecorder prepareToRecord];
-
开始录音
[self.audioRecorder record];
-
停止录音
[self.audioRecorder stop];
3.音乐播放+ 后台+锁屏显示
1. 勾选后台模式 音频播放
2. 激活音频播放会话
- 获取音频会话
AVAudioSession *session = [AVAudioSession sharedInstance]; - 设置会话分类
[session setCategory:AVAudioSessionCategoryPlayback error:nil]; - 激活会话
[session setActive:YES error:nil];
3.设置定时器
__weak typeof(self) weakSelf = self;
//设置无限循环 每0.1秒调用一次block 相当于定时器了吧
_playerTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(0.1*30, 30) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
//获取当前时间
CGFloat currentTime = CMTimeGetSeconds(time);
//获取所有时间
CMTime total = weakSelf.player.currentItem.duration;
CGFloat totalTime = CMTimeGetSeconds(total);
//在定时器里面 进行歌词刷新
}
//监听锁屏状态 lock = 1 则为锁屏状态
uint64_t locked;
__block int token = 0;
notify_register_dispatch("com.apple.springboard.lockstate", &token, dispatch_get_main_queue(), ^(int token) {
NSLog(@"锁屏了");
});
notify_get_state(token, &locked);
//监听屏幕点亮状态 screenLight = 1 则为变暗关闭状态
uint64_t screenLight;
__block int lightToken = 0;
notify_register_dispatch("com.apple.springboard.hasBlankedScreen", &lightToken, dispatch_get_main_queue(), ^(int token) {
});
notify_get_state(lightToken, &screenLight);
BOOL isShowLyricsPoster = NO;
if (screenLight == 0 && locked == 1) {
// 点亮且 锁屏时候
}else if (screenLight) {
//暗屏时候
return;
}
4配置锁屏信息
定时器加载这些信息
NSMutableDictionary *songDict = [[NSMutableDictionary alloc] init];
[songDict setObject:@"歌名" forKey:MPMediaItemPropertyTitle];
[songDict setObject:@"歌手" forKey:MPMediaItemPropertyArtist];
[songDict setObject:@"专辑名" forKey:MPMediaItemPropertyAlbumTitle];
//设置歌曲时长
[songDict setObject:[NSNumber numberWithDouble:totalTime] forKey:MPMediaItemPropertyPlaybackDuration];
//设置已经播放时长
[songDict setObject:[NSNumber numberWithDouble:currentTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
//设置播放速率
[songDict setObject:[NSNumber numberWithInteger:rate] forKey:MPNowPlayingInfoPropertyPlaybackRate];
//设置显示海报图片
//这个可以是截屏
[songDict setObject:[[MPMediaItemArtwork alloc]initWithImage:Image] forKey:MPMediaItemPropertyArtwork];
//获取锁屏信息中心 以及设置锁屏信息
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songDict];
5设置锁屏按钮等
//MPFeedbackCommand 对象反映了当前app所播放的反馈状态 MPRemoteCommandCenter 对象提供了feedback对象用于对媒体文件进行喜欢 不喜欢 标记的操作 类似于 网易云音乐锁屏时的效果
MPRemoteCommandCenter *commandCenter =
[MPRemoteCommandCenter sharedCommandCenter];
MPFeedbackCommand *linkCommand = commandCenter.likeCommand;
linkCommand.enabled = YES;
linkCommand.localizedTitle = @"喜欢";
[linkCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"喜欢");
return MPRemoteCommandHandlerStatusSuccess;
}];
MPFeedbackCommand *disLikeCommand = commandCenter.dislikeCommand;
disLikeCommand.enabled = YES;
disLikeCommand.localizedTitle = @"not like";
[disLikeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"不喜欢");
return MPRemoteCommandHandlerStatusSuccess;
}];
__weak typeof(self) weakSelf = self;
[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
[weakSelf.player pause];
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
[weakSelf.player play];
return MPRemoteCommandHandlerStatusSuccess;
}];