Revert "Swap the order of creating IAudioTrack and thread"

This reverts commit 5d464eb0b8cffb994a754ff108795e858a882414.
It caused the wrong thread ID -1 to be passed to IAudioFlinger::createTrack().

Change-Id: Ic221d2bb4af572d3d2d752af19238c52f6728e3a
diff --git a/media/libmedia/AudioTrack.cpp b/media/libmedia/AudioTrack.cpp
index 0ca035f..73d396e 100644
--- a/media/libmedia/AudioTrack.cpp
+++ b/media/libmedia/AudioTrack.cpp
@@ -278,6 +278,11 @@
     mFlags = flags;
     mCbf = cbf;
 
+    if (cbf != NULL) {
+        mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
+        mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
+    }
+
     // create the IAudioTrack
     status_t status = createTrack_l(streamType,
                                   sampleRate,
@@ -287,13 +292,13 @@
                                   flags,
                                   sharedBuffer,
                                   output);
-    if (status != NO_ERROR) {
-        return status;
-    }
 
-    if (cbf != NULL) {
-        mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
-        mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO);
+    if (status != NO_ERROR) {
+        if (mAudioTrackThread != 0) {
+            mAudioTrackThread->requestExit();
+            mAudioTrackThread.clear();
+        }
+        return status;
     }
 
     mStatus = NO_ERROR;