我一直在嘗試讀取在背景中播放的音樂音軌(通過音樂流媒體/播放應用程序)的音頻幅度級別,以便爲其製作可視化工具。讀取其他應用程序在背景中播放的音頻輸出
到目前爲止,我可以得到當前正在播放的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];
}