【问题标题】:Read audio output playing in the background by other app读取其他应用在后台播放的音频输出
【发布时间】: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


    【解决方案1】:

    查看“Inter-App Audio”,它支持做类似的事情。苹果在这里有一些例子,我相信你可以挖掘一些过去的 WWDC 讨论: https://developer.apple.com/library/prerelease/ios/samplecode/InterAppAudioSuite/Introduction/Intro.html

    由于 iOS 应用程序是沙盒化的,因此除非有官方 API(我可能错过了),否则直接访问其他应用程序的音频将受到限制。作为一种解决方法,您可以尝试启用麦克风输入并拾取后台播放的音频;虽然我不确定它是否或如何运作。您必须尝试使用​​ AudioSession 类别,以确保音频在沿着这条路线走下去时是可混合的。

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多