Fix for memory leak with single audio player instance

Change-Id: Idb0d24ac1a4fbe21c5f63871a4c7da2beb90e516
diff --git a/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp b/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp
index f33bd0a..3c9ffa3 100755
--- a/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp
+++ b/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp
@@ -64,6 +64,28 @@
 }
 void VideoEditorAudioPlayer::setSource(const sp<MediaSource> &source) {
     Mutex::Autolock autoLock(mLock);
+
+    // Before setting source, stop any existing source.
+    // Make sure to release any buffer we hold onto so that the
+    // source is able to stop().
+
+    if (mFirstBuffer != NULL) {
+        mFirstBuffer->release();
+        mFirstBuffer = NULL;
+    }
+
+    if (mInputBuffer != NULL) {
+        LOGV("VideoEditorAudioPlayer releasing input buffer.");
+
+        mInputBuffer->release();
+        mInputBuffer = NULL;
+    }
+
+    if (mSource != NULL) {
+        mSource->stop();
+        mSource.clear();
+    }
+
     mSource = source;
     mReachedEOS = false;
 }
@@ -79,7 +101,6 @@
     mObserver = observer;
 }
 
-
 bool VideoEditorAudioPlayer::isStarted() {
     return mStarted;
 }