I've got a wierd problem on my mac

when using a program that requires sound the sound stops and the computer does a wierd sound

E.g: when using the itunes the music stops compleetly, like if i had stopped it myself.

When using headphones or external speakers and when the problem appears, the sound moves from them to the mac speakers.

This problem has happened several times... but it stops

I did several hardware test and they were all ok

What should i do?

(model: bk macbook 4,1)