首页 > 解决方案 > 在 onActivityResult 上启动两个 mediaProjection 服务

问题描述

大家好,我想使用 mediaProjection 捕获屏幕并录制扬声器的声音,但是当我录制扬声器并开始拍摄屏幕时,扬声器会关闭,这意味着它一次只能执行一项任务,有人可以帮我吗?我正在使用if每个requestcode.

class MainActivity : AppCompatActivity() {
    companion object{
        private val REQUEST_CODE = 100
        private const val RECORD_AUDIO_PERMISSION_REQUEST_CODE = 42
        private const val MEDIA_PROJECTION_REQUEST_CODE = 13}
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        title = "Endless Service"

        findViewById<Button>(R.id.btnStartService).let {
            it.setOnClickListener {
                log("START THE FOREGROUND SERVICE ON DEMAND")
                startCapturing()
            }
        }

        findViewById<Button>(R.id.btnStopService).let {
            it.setOnClickListener {
                log("STOP THE FOREGROUND SERVICE ON DEMAND")
                stopCapturing()
                stopProjection()
            }
        }

        // start projection
        val startButton =
            findViewById<Button>(R.id.startButton)
        startButton.setOnClickListener { startProjection() }


    }
    private fun startCapturing() {
        if (!isRecordAudioPermissionGranted()) {
            requestRecordAudioPermission()
        } else {
            startMediaProjectionRequest()
        }
    }
    private fun requestRecordAudioPermission() {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.RECORD_AUDIO),
            RECORD_AUDIO_PERMISSION_REQUEST_CODE
        )
    }
    private fun isRecordAudioPermissionGranted(): Boolean {
        return ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.RECORD_AUDIO
        ) == PackageManager.PERMISSION_GRANTED
    }
    private fun startMediaProjectionRequest() {
        mediaProjectionManager =
            applicationContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
        startActivityForResult(
            mediaProjectionManager.createScreenCaptureIntent(),
            MEDIA_PROJECTION_REQUEST_CODE
        )
    }
    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        if (requestCode == com.robertohuertas.endless.MainActivity.REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                startService(
                    com.robertohuertas.endless.ScreenCaptureService.getStartIntent(
                        this,
                        resultCode,
                        data
                    )
                )
            }
        }
        if (requestCode == MEDIA_PROJECTION_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                Toast.makeText(
                    this,
                    "MediaProjection permission obtained. Foreground service will be started to capture audio.",
                    Toast.LENGTH_SHORT
                ).show()

                val audioCaptureIntent = Intent(this, EndlessService::class.java).apply {
                    action = EndlessService.ACTION_START
                    putExtra(EndlessService.EXTRA_RESULT_DATA, data!!)
                }
                startService(audioCaptureIntent)

                setButtonsEnabled(isCapturingAudio = true)
            } else {
                Toast.makeText(
                    this, "Request to obtain MediaProjection denied.",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }
    private fun startProjection() {
        val mProjectionManager =
            getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
        startActivityForResult(
            mProjectionManager.createScreenCaptureIntent(),
            com.robertohuertas.endless.MainActivity.REQUEST_CODE
        )
    }
    private fun stopProjection() {
        startService(com.robertohuertas.endless.ScreenCaptureService.getStopIntent(this))
    }
    private fun setButtonsEnabled(isCapturingAudio: Boolean) {
        findViewById<Button>(R.id.btnStartService).isEnabled = !isCapturingAudio
        findViewById<Button>(R.id.btnStopService).isEnabled = isCapturingAudio
    }
    private fun stopCapturing() {
        setButtonsEnabled(isCapturingAudio = false)

        startService(Intent(this, EndlessService::class.java).apply {
            action = EndlessService.ACTION_STOP
        })
    }
}

标签: kotlin

解决方案


推荐阅读