读取其他应用程序在背景中播放的音频输出

问题描述:

我一直在尝试读取在背景中播放的音乐音轨(通过音乐流媒体/播放应用程序)的音频幅度级别,以便为其制作可视化工具。读取其他应用程序在背景中播放的音频输出

到目前为止,我可以得到当前正在播放的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]; 
} 

退房“跨应用音频”,它支持在做类似的事情。苹果有一些例子在这里,我相信你能挖一些关于它的过去的WWDC讲座: https://developer.apple.com/library/prerelease/ios/samplecode/InterAppAudioSuite/Introduction/Intro.html

由于iOS应用程序的沙箱,与其他应用程序的音频直接访问将受到限制,除非有它的官方API(其我可能错过了)。作为解决方法,您可以尝试启用麦克风输入并拾取后台播放的音频;尽管我不确定它是否能够或者如何发挥作用。您必须尝试AudioSession类别以确保音频在沿着该路线行进时可混合。