使用AVAudioPlayer播放本地音乐
NSError *error = nil;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error) {
NSLog(@"加载音乐播放器失败 - %@", error.localizedDescription);
return nil;
}
return player;
2.AVAudioPlayer播放时的音量监控
// 1. 加载朗诵音频
_player = [SoundTool audioPlayerWithName:@"上01一东.mp3"];
_player.numberOfLoops = 0;
[_player prepareToPlay];
[_player setDelegate:self];
// 2. 允许音量测量
[_player setMeteringEnabled:YES];
// 3. 实例化音频信息数组
_soundInfo = [NSMutableArray array];
// 4. 启动时钟
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
// 5. 开始播放
[_player play];
3.时钟处理方法
// 1. 更新播放器音量
[_player updateMeters];
// 2. 判断是否需要记录
if ([_player averagePowerForChannel:0] > -50.0) {
if (_shouldRecord) {
NSLog(@"%f", [_player currentTime]);
if (![_soundInfo containsObject:@([_player currentTime])]) {
[_soundInfo addObject:@([_player currentTime])];
}
}
_shouldRecord = NO;
} else {
_shouldRecord = YES;
}