pedroSG94 / RootEncoder

RootEncoder for Android (rtmp-rtsp-stream-client-java) is a stream encoder to push video/audio to media servers using protocols RTMP, RTSP, SRT and UDP with all code written in Java/Kotlin
Apache License 2.0
2.57k stars 776 forks source link

Microphone not work in background mode at some devices. #1536

Closed MinhMinh8722 closed 2 months ago

MinhMinh8722 commented 3 months ago

Hi @pedroSG94 I have a problem with the microphone. In background, the app was muted on some devices. And this is my code. I set it in ScreenService Please help me. Thanks!!!

fun handleMicrophone() {
        val microphoneSource = (genericStream.audioSource as MicrophoneSource)
        if (isMute) {
            microphoneSource.mute()
        } else {
            microphoneSource.unMute()
        }
// Update UI
    }
pedroSG94 commented 3 months ago

Hello,

If you can't get audio from microphone in background check that your service has this permission: https://github.com/pedroSG94/RootEncoder/blob/master/app/src/main/AndroidManifest.xml#L64 In this case you need add microphone permission like:

android:foregroundServiceType="mediaProjection|microphone"
noho501 commented 3 months ago

Hi @pedroSG94

I am developing an application with screencasting functionality. If I want to mute only the microphone while still recording audio from the app (like game audio), how can I do that? When I mute the microphone, it also mutes the app audio.

pedroSG94 commented 3 months ago

Hello,

If you are using microphone audio and you can hear app audio that means that microphone capture audio from speakers. That is the reason about your case.

You can change to use directly audio from the app instead of mute microphone. For this, you need use GenericStream to stream or any class that extends from StreamBase (screen stream example with GenericStream):

//I think that you can reuse mediaprojection from ScreenSource but you should test it
genericStream.changeAudioSource(InternalAudioSource(mediaProjection)) 

And instead of unmute you should return to Microphone source:

genericStream.changeAudioSource(MicrophoneSource())
TaiBK15 commented 3 months ago

Hi @pedroSG94 , I'm encountering an issue where the code crashes after several calls to the handleMicrophone function. I was wondering if you could take a look when you have a moment.

 fun handleMicrophone(isMute: Boolean) {
        if (isMute) {
            mediaProjection?.let {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                     genericStream.changeAudioSource(InternalAudioSource(it))
                }
            }
        } else {
            genericStream.changeAudioSource(MicrophoneSource())
        }
    }

z5739627519418_1edf1ac16b1b314441d43da638457488

pedroSG94 commented 3 months ago

Hello,

I will check it. Are you re using mediaprojection when you call to that method? Or are you using a new instance each time?

Try to use a new instance each time. I will test it in both cases

noho501 commented 3 months ago

Hi @pedroSG94 ,

We re-using mediaprojection. We will try your suggestions

Thanks

noho501 commented 3 months ago

Hi @pedroSG94 ,

We have tried both cases but it still crash after several calls. Could you please help us check it out?

Thanks so much.

pedroSG94 commented 3 months ago

Hello,

I fixed the error.

Now, you should re-use mediaprojection class. This error is because no matter if you request a second instance of mediaprojection. If you have a mediaprojection working (in this case from ScreenSource), the mediaprojection is same instance until you stop all sources that depend of that mediaprojection. As proof, I never see a modal when I request the second mediaprojection.

I think that share this mediaprojection and call stop while other source is using it produce this crash. So I think that mediaprojection should be stopped after stop all sources that depend of it. Now, I handle this automatically.

pedroSG94 commented 3 months ago

Fix uploaded to version 2.4.8. Update the library to solve it.

TaiBK15 commented 3 months ago

Hi @pedroSG94 Thanks for quick response, we are really appreciate it. After updating to version 2.4.8, the recording works fine the first time. However, when we stop and then start recording again, the app crashes. Additionally, switching microphones is not working. Could you please assist us with this? I’ve attached a file for your reference.

Thank you so much.

code code_2 log

pedroSG94 commented 3 months ago

Hello,

I'm not able to reproduce it in my app example. I added this code to Service:

  var muted = false

  fun toggleMute() {
      if (muted) {
        genericStream.changeAudioSource(MicrophoneSource())
        muted = false
      } else {
        mediaProjection?.let {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            genericStream.changeAudioSource(InternalAudioSource(it))
            muted = true
          }
        }
      }
  }

And added a new button to the activity:

    val test = findViewById<ImageView>(R.id.b_test)
    test.setOnClickListener {
      val service = ScreenService.INSTANCE
      service?.toggleMute()
    }

In xml:

  <ImageView
      android:src="@drawable/record_icon"
      android:layout_width="98dp"
      android:layout_height="98dp"
      android:id="@+id/b_test"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      android:layout_marginBottom="20dp"
      />

Can you tell me how to reproduce your case in my app example?

pedroSG94 commented 3 months ago

Ok, I detected the problem and fixed it. Use version 2.4.9 I tested all cases that I remember:

The only problem is that you will need stop mediaprojection class manually when you finish working with it. I added this feature to toggle audio sources to test it in the screen example. Example of how to release mediaprojection class: https://github.com/pedroSG94/RootEncoder/blob/master/app/src/main/java/com/pedro/streamer/screen/ScreenService.kt#L141 https://github.com/pedroSG94/RootEncoder/blob/master/app/src/main/java/com/pedro/streamer/screen/ScreenService.kt#L148

noho501 commented 2 months ago

Hi @pedroSG94 ,

Thank you very much for your help. We'll review it and let you know if it's working properly.

Best regards

noho501 commented 2 months ago

It works. Thank you so much

pedroSG94 commented 2 months ago

Closing issue as completed