Japanese researchers have developed a set of prototype headphones that use proximity sensors to detect the left and right ear, eliminating the need for user differentiation between earbuds, along with some other pretty nifty uses for the technology.
Researchers at the Igarashi Design Interface Project are the minds behind the project, and uses proximity sensors mounted on the side of the earphones, mounted at a right angle to the ear. With this, the sensor can detect when and where the surrounding air is obstructed by the presence of an ear, on the left or right hand side, which alters the side of stereo sound that you hear.
The situation of shared earphones required a different approach though. Using skin conductivity, an electrical charge is sent through the earphones, with a sensor detecting whether the current flows along the user's skin and can be detected from one side to the other, allowing stero sound. However, if these criteria aren't met, and these earphones are being used by separate people, then no connection is formed, and each person get's a mono version of music output.