kotlin - 在 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
})
}
}
解决方案
推荐阅读
- vue.js - 单个文件组件中的 Vue.js v-slot
- java - Java Web 应用程序在 apache-tomcat-8.5.34-windows-x86 上运行良好,但在 apache-tomcat-8.5.40-windows-x86 上运行良好
- python - 如何将txt文件解析为csv并修改格式
- android - TextInputEditText 没有“setStartIconDrawable”功能,为什么?
- python - 问题将 KeyPoints 与 JSON 进行转换,然后使用 FlannBasedMatcher
- .net-core - 从 Rider 中的“添加 Web 引用”生成代码时出现“重复的编译项”错误
- linux - 如何使 awk 根据哪个与条件匹配来打印 2 个不同字段之一
- swift - Side-menu is not working properly on mapView in swift
- java - Java - 在 Linux 服务器上执行 .jar 文件
- amazon-web-services - Amazon SQS 消息消失了