Polonius says, "I hear him coming"(3.1.56). Could not Hamlet have heard him speaking? As Isaac Asimov noted, in Act 2, scene 2 Hamlet enters immediately after Polonius proposes the eavesdropping idea. Could Hamlet have been doing some eavesdropping of his own? The speech may then be subtle mockery directed at Claudius.
Sorry - I should have clarified that I agreed with the second post. There are centuries of theatrical tradition behind that moment when a noise is heard behind the arras. Up to that point Hamlet clearly believes he is truly alone with Ophelia - although her contradictory behaviour towards him throughout the play still makes him wary of her. It would be a good idea to look at how that moment is played in film versions of 'Hamlet'. I have seen several and they all play it according to post 2. The Ethan Hawke version is interesting: Hamlet and Ophelia are getting more intimate until hamlet notices that she is wired up with microphones and that every word he is saying can be heard.
I agree with post #2 -- the speech is a sincere and emotional evaluation of action and inaction. I guess I would ask myself, if he thought he was being listened to, what purpose does that serve in the speech? What "message" would he be "sending" to Polonius and Claudius? I can see no message. Also, the speech is in iambic pentameter, but when Hamlet is acting crazy he is speaking in prose.
You are right. Most productions have Polonius sneeze or soemthing behind the arras which leads to hamlet's question - 'Where's your father?'
To b or not ot be - is sad and moving, full of angst and desperation.
Also in Act 1, scene 2, The King says to Hamlet: "How is it that the clouds still hang on you?" Hamlet has been grieving the loss of his father for, it seems, several weeks. Therefore, he may have formed the speech prior to his speaking it.
I do agree with post #4 that the speech is a "sincere and emotional evaluation of action and inaction." Ophelia has a line in response to her brother's interpretation of Hamlet's behavior: "No more but so?"(1.3.9). The editors of the new Arden edition wrote that "the most famous of all soliloquies is not, strictly speaking, a soliloquy at all: three other characters are present, although Hamlet speaks as if he were alone." As someone noted, Hamlet has already expressed the wish that "the Everlasting had not fixed / His canon 'gainst self-slaughter"(1.2.131).
Thanks to all for engaging the topic. The question also has a history. Actor Alec Guinness wrote of his own performances as Hamlet(1938-9 and 1951): "With both Hamlets I played I dismissed the idea that Hamlet was aware he was being spied on either before or after the 'To be or not to be.'" For my own part, I'll dismiss the phrase "subtle mockery" in favor of chiding, scolding or reprimand. Derek Jacobi delivered the speech directly to Ophelia, in part, because when Hamlet enters he couldn't possibly fail to see her. As for the "message," surely killing a King and taking his place bears some correspondence to "enterprises of great pith and moment." Hamlet is suggesting that the thinking of Claudius extended only to either killing himself or killing the King. Hamlet suggests that Claudius has experienced "the proud man's contumely" not long before the speech: "those that would make mows at him"(2.2.360, Oxford ed). The Ghost says that "It's given out that, sleeping in my orchard, / A serpent stung me"(1.5.35), which corresponds a bit to all the talk of sleep in Hamlet's monologue. Kenneth Branagh's film version is also suggestive.
It is the generally held view that Hamlet becomes aware of Pol + Claud when he suddenly asks Oph, 'Where's your father?' This makes sense to me and it's the way we staged it for our Uni performance.
I don't really feel 2b or -2b is a mocking speech, do you?