BlackBart said:
I've pointed this out before, but, whether or not you know they are casting a spell is just about irrelevant, because you have no way of knowing if they are casting on the defensive.
DM: You hear someone casting a spell right next to you.
Player: I take my AoO.
DM: Not so fast slick, he's casting on the defensive, but go ahead and mark of that AoO for the round - you missed.
If that's the case, the DM should say, "You hear someone casting a spell right next to you (let's assume he didn't hear the caster approach, so he must have either teleported or has rogue levels or something), but you don't realize it until it's too late." Casting on the defensive does not provoke AoO's, so you can't lose your attempt for the round in the scenario you gave.