Hi everyone I am wanting to capture the screen and record the sound from the speaker with mediaProjection, however when I record the speaker and start taking the screen the speaker turns off which means it only does one task at a time, Can someone help me? I am using if for every 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
})
}
}