I'm trying to get all the audio from my phone to go through my computer speakers via connecting through USB. Does anyone know how to achieve it? I'm trying to write some codes to let my pc being recognized as a dock for my galaxy s2 cell phone.
There's no standard way of doing that, unless a given vendor has implemented it as a custom extension.
It's actually going to be easier to do some stream-over-wifi mechanism than to do it over the USB cable, unless you leverage the SDK's adb tool or a USB-tethering capability to get a network connection via the cable.
But a key problem that you will face is that 3rd party android apps on non-rooted devices cannot get permission to intercept audio coming from other unrelated applications.
Silly as it sounds, if you want your pc to be a "dock" for arbitrary application audio, you'll probably need a 3.5mm analog stereo patch cord. Or there might be a few PC bluetooth adapters which can be hacked to look like stereo headsets to the phone.
If you are content to play songs chosen by special software, I'm sure you can find a wifi streamer applications on the market or make your own; or you can access the files on the device from the PC using usb mass storage or MTP (depending on Android version) and play them in a PC-based player.
EDIT Guess what just got announced at Google IO 2012
USB Audio Dock Implementation One of the important new features introduced with the ADK 2012 is the ability to play audio over a USB connection. This innovation was introduced as an update to Android Open Accessory (AOA) protocol 2.0 and is available on devices running Android 4.1 (API Level 16) and higher.
The ADK 2012 provides a reference implementation of this functionality for accessory developers. No software application is required to be installed on the connected Android device, accessory developers only need to support AOA v2.
The assumption there is that the USB host would be a small embedded board, but I don't see why you couldn't get a PC to do that end of the task, at least if you manage to get any existing drivers installed on the PC out of the way.
Of course the downside is that you need an Android 4.1 or greater device... not many phones like that out. But if you go with the rooting method, you could try to create a compatible or at least similar interface.