Hello everyone. Is it possible to automatically switch recording/playing device (in sound settings) when plugging an earphone to the smartphone? If so, how?

Thanks in advance for any help.