The correct option is C James was enjoying a quiet afternoon playing his piano, when Amy and Ted visited him.
Since "playing his piano" is qualified by "when...visited him", these should be placed close together. Hence, option C is correct. The rest of the options do not place the qualifying clauses together, hence these are incorrect.