You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

获取活跃AudioSession ID:Visualizer类可视化第三方音频遇阻

How to Capture Third-Party Audio for Visualizer (Android)

Hey there, I feel your pain—trying to get the Visualizer working with Spotify or other third-party apps can be tricky now that the old session ID 0 method is deprecated. Let’s break down how to handle this depending on your target Android version:

For Android 9 (API 28) and Below

Before API 29, you could still access mixed audio output or grab active audio session IDs from other apps, but you need the right permissions and setup:

  • First, add the RECORD_AUDIO permission to your manifest:
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    
  • To fetch the active audio session ID from a third-party app, use AudioManager to query active playback sessions:
    val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
    val activeSessions = audioManager.getActiveAudioPlaybackSessions()
    
    for (session in activeSessions) {
        // Skip your app's own sessions to target third-party apps like Spotify
        if (session.packageName != packageName) {
            val sessionId = session.id
            // Initialize Visualizer with this third-party session ID
            val visualizer = Visualizer(sessionId)
            // Configure your Visualizer callbacks and settings here
            break
        }
    }
    
    Note: This method isn’t 100% reliable across all apps and is restricted starting from API 29.

For Android 10 (API 29) and Above

Due to privacy restrictions, accessing other apps’ audio session IDs directly is no longer allowed. Instead, you need to use the MediaProjection API to capture system-wide audio, then process that data for visualization:

  1. Request MediaProjection Permission:
    Prompt the user to grant permission to capture screen and audio:

    private val REQUEST_CODE_MEDIA_PROJECTION = 101
    val mediaProjectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
    val intent = mediaProjectionManager.createScreenCaptureIntent()
    startActivityForResult(intent, REQUEST_CODE_MEDIA_PROJECTION)
    
  2. Handle Permission Result:
    Once the user approves, create a MediaProjection instance and set up audio capture:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_MEDIA_PROJECTION && resultCode == RESULT_OK) {
            val mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data!!)
            
            // Configure audio format for capture
            val audioFormat = AudioFormat.Builder()
                .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                .setSampleRate(44100)
                .setChannelMask(AudioFormat.CHANNEL_IN_MONO)
                .build()
            
            // Initialize AudioRecord with MediaProjection
            val bufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT)
            val audioRecord = AudioRecord.Builder()
                .setAudioSource(MediaRecorder.AudioSource.REMOTE_SUBMIX)
                .setAudioFormat(audioFormat)
                .setBufferSizeInBytes(bufferSize)
                .setMediaProjection(mediaProjection)
                .build()
            
            // Start capturing audio in a background thread
            audioRecord.startRecording()
            // Read PCM data and compute FFT manually for visualization (replace Visualizer class logic)
        }
    }
    
  3. Manual Visualization Logic:
    Since you can’t use the Visualizer class directly here, you’ll need to compute the frequency spectrum yourself from the captured PCM data. Use an FFT implementation (custom or a lightweight library) to convert time-domain PCM data into frequency-domain data for your visualizer UI.

Key Notes

  • Permissions: Ensure you request RECORD_AUDIO and handle foreground service requirements if your app needs to run in the background (newer Android versions restrict background audio capture).
  • Visualizer Class Limitation: The Visualizer class now primarily works with your app’s own audio sessions. For third-party apps, MediaProjection is the official, supported approach.

I hope this helps you get your audio visualization up and running with Spotify and other apps! Let me know if you hit any specific roadblocks.

内容的提问来源于stack exchange,提问作者DoktorMerlin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:22:21