【发布时间】:2016-03-06 12:36:34
【问题描述】:
我一直在尝试读取在后台播放的音乐曲目的音频幅度级别(通过音乐流/播放应用程序),以便为它制作可视化工具。
目前我可以得到当前播放的iPodMusic背景音轨,但是为了分析它,我需要切断外部音乐并在应用内播放,这样我才能分析它。
我真正需要的是访问音频输出频率/幅度级别。如何读取我的应用中的音频输出,以便分析当时正在播放的任何其他应用音乐?
这是迄今为止我使用 iPodMusicPlayer 应用程序所获得的结果:
- (void)playPause
{
__autoreleasing NSError* error;
_audioPlayer = [_audioPlayer initWithContentsOfURL:[[[MPMusicPlayerController systemMusicPlayer] nowPlayingItem] valueForProperty:MPMediaItemPropertyAssetURL] error:&error];
_audioPlayer.currentTime = [[MPMusicPlayerController systemMusicPlayer] currentPlaybackTime];
_audioPlayer.volume = 1;
_audioPlayer.delegate = self;
[_audioPlayer play];
[_audioPlayer setMeteringEnabled:YES];
[_audioPlayer updateMeters];
if (_isPlaying)
{
// Pause audio here
[_audioPlayer pause];
[_toolBar setItems:_playItems]; // toggle play/pause button
}
else
{
// Play audio here
[_audioPlayer play];
[_toolBar setItems:_pauseItems]; // toggle play/pause button
}
_isPlaying = !_isPlaying;
if (!_isUpdating)
{
_isUpdating = TRUE;
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateUILables) userInfo:nil repeats:YES];
}
}
-(void)updateUILables
{
NSMutableArray* amps = [NSMutableArray array];
for (int i = 0; i < _audioPlayer.numberOfChannels; i++) {
[amps addObject:@(([_audioPlayer peakPowerForChannel:i] + 160.0) / 160.0)];
if (i == 0)
self.channel0Label.text = [NSString stringWithFormat:@"%f0.6", [_audioPlayer averagePowerForChannel:i]];
else if (i == 1)
self.channel1Label.text = [NSString stringWithFormat:@"%f0.6", [_audioPlayer averagePowerForChannel:i]];
NSLog(@"Amplitude: %f db for channel %i", [_audioPlayer averagePowerForChannel:i], i);
}
float amp = 0;
for (NSNumber *x in amps) {
amp += [x floatValue];
}
amp /= amps.count;
NSLog(@"amp: %f", amp);
self.cumulativeLabel.text = [NSString stringWithFormat:@"%f0.6", amp];
}
【问题讨论】:
标签: ios objective-c avaudioplayer core-audio avaudiosession