I am using Macrodroid and Automate to demonstrate what you ask.
With Macrodroid
You will need to create 2 macros.
1st macro: Headset plugged
In the Trigger tab, select Headphones Insert/Remove, then choose Headphones Inserted followed by Any.
In the Actions tab, select Volume Change, then give the appropriate permissions. Set the level of each audio stream (see below picture on the left).
Give the macro a name, then save it. It will look like the picture on the right.
2nd macro: Headset unplugged
In the Trigger tab, select Headphones Insert/Remove, then choose Headphones Removed.
In the Actions tab, select Volume Change, then give the appropriate permissions. Set the level of each audio stream to 0 (see below picture on the left).
Give the macro a name, then save it. It will look like the picture below on the right.
With Automate
- In the Interface section, select "Wire headset plugged?", then link its
IN
with the OK
of the "Flow beginning" block.
In the Camera & Sound section, select "Audio stream mute state" twice.
Click on the first block, set the state to Unmute
, select an audio stream, then click Done. Link the IN
to the Yes
of the "When wire headset plugged" block, then Link the OK
to the IN
of the "When wire headset plugged" block.
Click on the second block, set the state to Mute
, then select an audio stream. Link the IN
to the No
of the "When wire headset plugged" block, then Link the OK
to the IN
of the "When wire headset plugged" block.
It will look like the image below. Give your flow a name then save it, then click on Start.
Note: I have selected Music stream for this example. Only the music will be muted/unmuted. Add more blocks for the other audio streams (voice call, alarm, system, etc).