Issue
I use MediaSession
to respond to headset media buttons, and want to stop the sound when activity is paused. I also want to make the headset media buttons not active when activity is paused.
From the android documentation, it said the setActive
method is use to "set if this session is currently active and ready to receive commands". But it not worked, I can still use head resume the music when activity is paused. Do I misunderstand the function of this method?
My code is as followed:
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class MainActivity : AppCompatActivity() {
private lateinit var mediaSession: MediaSession
private var mediaPlayer: MediaPlayer? = null
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mediaPlayer = MediaPlayer.create(this, R.raw.lapple)
mediaPlayer?.isLooping = true
mediaPlayer?.start()
// Create a MediaSessionCompat
mediaSession = MediaSession(this, "test_log").apply {
// Enable callbacks from MediaButtons and TransportControls
setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS or MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS)
// Do not let MediaButtons restart the player when the app is not visible
setMediaButtonReceiver(null)
// Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player
val stateBuilder = PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY or PlaybackState.ACTION_PAUSE or PlaybackState.ACTION_PLAY_PAUSE)
setPlaybackState(stateBuilder.build())
// MySessionCallback has methods that handle callbacks from a media controller
setCallback(object : MediaSession.Callback() {
override fun onPlay() {
Toast.makeText(
this@MainActivity,
"${[email protected]()}: onPlay",
Toast.LENGTH_SHORT
).show()
mediaPlayer?.start()
}
override fun onPause() {
mediaPlayer?.pause()
Toast.makeText(
this@MainActivity,
"${[email protected]()}: onPause",
Toast.LENGTH_SHORT
).show()
}
})
}
mediaSession.isActive = true
//mediaController = MediaController(this, mediaSession.sessionToken)
}
override fun onPause() {
super.onPause()
mediaSession.isActive = false
mediaPlayer?.pause()
}
override fun onResume() {
super.onResume()
mediaSession.isActive = true
mediaPlayer?.start()
}
override fun onDestroy() {
mediaPlayer?.release()
super.onDestroy()
}
fun newActivity(view: View) {
startActivity(Intent(this, MainActivity::class.java))
}
}
Solution
The documentation says:
Set if this session is currently active and ready to receive commands. If set to false your session's controller may not be discoverable. You must set the session to active before it can start receiving media button events or transport commands.
It does not say that you should set it to false to stop receiving commands. I guess if the controller is already discovered, the value doesn't matter anymore.
So instead you should use release
or setCallback(null)
according to this documentation.
Answered By - francis duvivier
Answer Checked By - Candace Johnson (JavaFixing Volunteer)