Chào mọi người em đang sử dụng mediaproject để làm hai công việc là chụp màn hình và ghi âm thanh từ loa,khi em bắt đầu ghi màn hình thì hoạt động ghi âm tắt và ngược lại. Có lẽ vì em đã gọi if cho mỗi request code vậy có cách nào để cả hai hoạt động cùng chạy không ạ. Đây là code em gọi hai service với REQUEST_CODE
và MEDIA_PROJECTION_REQUEST_CODE
:
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
})
}
}