You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

关于Record Internal Audio Service:非Root锁屏录制内部音频可行性咨询(通用SDK)

Can the Universal Classic SDK Record Internal Audio (Like Spotify) on Locked, Non-Rooted Devices?

Short Answer

No, the native Android SDK (what you're calling the "universal classic SDK") doesn't have a direct API to record only a specific app's internal audio. But there is a legitimate workaround using the MediaProjection API that lets you capture system-wide audio (including apps like Spotify) on non-rooted, non-Samsung devices—even when the screen is locked.

The Details, Breakdown by Requirement

1. Lock Screen Recording

Keeping your recording service alive while the screen is locked is totally feasible, but you'll need to handle a few key permissions and system constraints:

  • The FOREGROUND_SERVICE permission (required for Android 8.0+) to stop the system from killing your service in the background.
  • A PowerManager.WakeLock (use PARTIAL_WAKE_LOCK sparingly—keep an eye on battery drain) to keep the audio subsystem active when the screen is off.
  • Proper audio focus handling to ensure your recording doesn't get interrupted by other apps or interrupt playback of the target app.

2. Capturing Internal Audio Without Root (Non-Samsung)

The only non-root, cross-device way to capture audio from other apps on Android is via the MediaProjection API (introduced in Android 5.0, API 21). Here's what you need to know:

  • It requires explicit user authorization: your app will trigger a system dialog asking the user to grant permission for screen/audio capture. Silent access isn't possible (and would violate Android's privacy rules anyway).
  • It captures the system audio mix—meaning you'll get all audio playing on the device (Spotify, notifications, other apps) rather than just a single app's audio. You'll either need to ask users to mute other sounds, or post-process the recording to filter out unwanted audio.
  • Even when the screen is locked, as long as your service runs in the foreground and the MediaProjection session stays active, recording will continue.

Quick Code Snippet to Get Started

// Get the MediaProjectionManager system service
MediaProjectionManager projectionManager = 
    (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

// Launch the system's audio/screen capture authorization dialog
Intent captureIntent = projectionManager.createScreenCaptureIntent();
startActivityForResult(captureIntent, REQUEST_CODE_AUDIO_CAPTURE);

// Handle the user's authorization response
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_AUDIO_CAPTURE && resultCode == RESULT_OK) {
        MediaProjection mediaProjection = 
            projectionManager.getMediaProjection(resultCode, data);
        
        // Set up MediaRecorder to capture audio (we can skip video with dummy dimensions)
        MediaRecorder recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        recorder.setOutputFile(getExternalFilesDir(null) + "/spotify_recording.mp4");
        
        try {
            recorder.prepare();
            // Create a dummy virtual display (required even if we don't need video)
            VirtualDisplay virtualDisplay = mediaProjection.createVirtualDisplay(
                "AudioCapture",
                1, 1, // Tiny width/height since video isn't our goal
                getResources().getDisplayMetrics().densityDpi,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                recorder.getSurface(),
                null, null);
            recorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. Caveats for Apps Like Spotify

Don't expect perfect results with DRM-protected streaming apps:

  • Apps like Spotify use DRM (e.g., Widevine) to encrypt their audio streams. Even if you capture system audio, you might end up with silent or garbled recordings because the audio is only decrypted for playback, not for capture.
  • Some apps actively detect audio/screen capture sessions and will pause playback or reduce quality to prevent recording—this is a deliberate anti-piracy measure.

Final Takeaway

You can't record a single app's internal audio directly with the native SDK, but you can capture system-wide audio (including Spotify) on locked, non-rooted, non-Samsung devices using the MediaProjection API. Just be prepared to handle user authorization, system-wide audio mixing, and potential DRM blocks from streaming apps.

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

火山引擎 最新活动