summaryrefslogtreecommitdiff
path: root/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/CodecTest.java
blob: b663ceabe8f768b720029b3a253b873658e8c0a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mediaframeworktest.functional;



//import android.content.Resources;
import com.android.mediaframeworktest.MediaFrameworkTest;
import com.android.mediaframeworktest.MediaNames;

import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.io.FileOutputStream;
import java.util.Random;
/**
 * Junit / Instrumentation test case for the media player api
 
 */  
public class CodecTest {    
    private static String TAG = "MediaPlayerApiTest";
    private static MediaPlayer mMediaPlayer;
    private MediaPlayer.OnPreparedListener mOnPreparedListener;
    
    private static int WAIT_FOR_COMMAND_TO_COMPLETE = 60000;  //1 min max.
    private static boolean mInitialized = false;
    private static boolean mPrepareReset = false;
    private static Looper mLooper = null;
    private static final Object lock = new Object();
    private static final Object prepareDone = new Object();
    private static final Object videoSizeChanged = new Object();
    private static final Object onCompletion = new Object();
    private static boolean onPrepareSuccess = false;
    private static boolean onCompleteSuccess = false;

    public static String printCpuInfo(){      
        String cm = "dumpsys cpuinfo";
        String cpuinfo =null;
        int ch;
        try{
            Process  p = Runtime.getRuntime().exec(cm);
            InputStream in = p.getInputStream();        
            StringBuffer sb = new StringBuffer(512);
            while ( ( ch = in.read() ) != -1 ){  
                sb.append((char) ch); 
            }
            cpuinfo = sb.toString();      
        }catch (IOException e){
            Log.v(TAG, e.toString());
        }
        return cpuinfo;
    }


    public static int getDuration(String filePath) {
        Log.v(TAG, "getDuration - " + filePath);
        MediaPlayer mp = new MediaPlayer();
        try{
            mp.setDataSource(filePath);
            mp.prepare(); 
        }catch (Exception e){
            Log.v(TAG, e.toString());
        }
        int duration = mp.getDuration();
        Log.v(TAG, "Duration " + duration);
        mp.release();
        Log.v(TAG, "release");      
        return duration;
    }

    public static boolean getCurrentPosition(String filePath){
        Log.v(TAG, "GetCurrentPosition - " + filePath);
        int currentPosition = 0;
        long t1=0;
        long t2 =0;
        MediaPlayer mp = new MediaPlayer();
        try{
            mp.setDataSource(filePath);
            Log.v(TAG, "start playback");
            mp.prepare();
            mp.start();
            t1=SystemClock.uptimeMillis();
            Thread.sleep(10000);
            mp.pause();
            Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
            t2=SystemClock.uptimeMillis();
        }catch (Exception e){
            Log.v(TAG, e.toString());
        }
        currentPosition = mp.getCurrentPosition();
        mp.stop();
        mp.release();   
        Log.v(TAG, "mp currentPositon = " + currentPosition + " play duration = " + (t2-t1));
        //The currentposition should be within 10% of the sleep time
        //For the very short mp3, it should return the length instead of 10 seconds
        if (filePath.equals(MediaNames.SHORTMP3)){
            if (currentPosition < 1000 )
                return true;
        }
        if ((currentPosition < ((t2-t1) *1.2)) && (currentPosition > 0)) 
            return true;
        else
            return false;
    }  

    public static boolean seekTo(String filePath){
        Log.v(TAG, "seekTo " + filePath);
        int currentPosition = 0;
        MediaPlayer mp = new MediaPlayer();
        try{
            mp.setDataSource(filePath);
            mp.prepare();
            mp.start();
            mp.seekTo(MediaNames.SEEK_TIME);
            Thread.sleep(MediaNames.WAIT_TIME);
            currentPosition = mp.getCurrentPosition();
        }catch (Exception e){
            Log.v(TAG, e.getMessage());
        }      
        mp.stop();
        mp.release();
        Log.v(TAG, "CurrentPosition = " + currentPosition);
        //The currentposition should be at least greater than the 80% of seek time
        if ((currentPosition > MediaNames.SEEK_TIME *0.8)) 
            return true;
        else
            return false;
    }

    public static boolean setLooping(String filePath){
        int currentPosition = 0;
        int duration = 0;
        long t1 =0;
        long t2 =0;
        Log.v (TAG, "SetLooping - " + filePath);
        MediaPlayer mp = new MediaPlayer();
        try{
            mp.setDataSource(filePath);
            mp.prepare();
            duration = mp.getDuration(); 
            Log.v(TAG, "setLooping duration " + duration);
            mp.setLooping(true);
            mp.start();
            Thread.sleep(5000);
            mp.seekTo(duration - 5000);
            t1=SystemClock.uptimeMillis();
            Thread.sleep(20000);
            t2=SystemClock.uptimeMillis();
            Log.v(TAG, "pause");
            //Bug# 1106852 - IllegalStateException will be thrown if pause is called 
            //in here
            //mp.pause();
            currentPosition = mp.getCurrentPosition();
            Log.v(TAG, "looping position " + currentPosition + "duration = " + (t2-t1));
        }catch (Exception e){
            Log.v(TAG, "Exception : " + e.toString());
        }      
        mp.stop();
        mp.release();
        //The current position should be within 20% of the sleep time
        //and should be greater than zero.
        if ((currentPosition < ((t2-t1-5000)*1.2)) && currentPosition > 0)
            return true;
        else
            return false;
    }  

    public static boolean pause(String filePath) throws Exception {
        Log.v(TAG, "pause - " + filePath);
        boolean misPlaying = true;
        boolean pauseResult = false;
        long t1=0;
        long t2=0;
        MediaPlayer mp = new MediaPlayer();
        mp.setDataSource(filePath);
        mp.prepare();    
        int duration = mp.getDuration();
        mp.start();
        t1=SystemClock.uptimeMillis();
        Thread.sleep(5000);
        mp.pause();
        Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
        t2=SystemClock.uptimeMillis();
        misPlaying = mp.isPlaying();
        int curPosition = mp.getCurrentPosition();
        Log.v(TAG, filePath + " pause currentPositon " + curPosition);
        Log.v(TAG, "isPlaying "+ misPlaying + " wait time " + (t2 - t1) );
        String cpuinfo = printCpuInfo();
        Log.v(TAG, cpuinfo);
        if ((curPosition>0) && (curPosition < ((t2-t1) * 1.3)) && (misPlaying == false))
            pauseResult = true;
        mp.stop();
        mp.release();
        return pauseResult;
    }

    public static void prepareStopRelease(String filePath) throws Exception {
        Log.v(TAG, "prepareStopRelease" + filePath);
        MediaPlayer mp = new MediaPlayer();
        mp.setDataSource(filePath);
        mp.prepare();
        mp.stop();
        mp.release();
    }

    public static void preparePauseRelease(String filePath) throws Exception {
        Log.v(TAG, "preparePauseRelease" + filePath);
        MediaPlayer mp = new MediaPlayer();
        mp.setDataSource(filePath);
        mp.prepare();
        mp.pause();
        mp.release();
    }
    
    static MediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangedListener =
        new MediaPlayer.OnVideoSizeChangedListener() {
            public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                synchronized (videoSizeChanged) {
                    Log.v(TAG, "sizechanged notification received ...");
                    videoSizeChanged.notify();
                }
            }
    };

    //Register the videoSizeChanged listener
    public static int videoHeight(String filePath) throws Exception {
        Log.v(TAG, "videoHeight - " + filePath);
        int videoHeight = 0;    
        synchronized (lock) {
            initializeMessageLooper();
            try {
                lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
            } catch(Exception e) {
                Log.v(TAG, "looper was interrupted.");
                return 0;
            }
        }
        try {
            mMediaPlayer.setDataSource(filePath);
            mMediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
            mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
            synchronized (videoSizeChanged) {
                try {
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                    videoSizeChanged.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
                } catch (Exception e) {
                    Log.v(TAG, "wait was interrupted");
                }
            }
            videoHeight = mMediaPlayer.getVideoHeight();
            terminateMessageLooper();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
        
        return videoHeight;
    }

    //Register the videoSizeChanged listener
    public static int videoWidth(String filePath) throws Exception {
        Log.v(TAG, "videoWidth - " + filePath);
        int videoWidth = 0;

        synchronized (lock) {
            initializeMessageLooper();
            try {
                lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
            } catch(Exception e) {
                Log.v(TAG, "looper was interrupted.");
                return 0;
            }
        }
        try {
            mMediaPlayer.setDataSource(filePath);
            mMediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
            mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
            synchronized (videoSizeChanged) {
                try {
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                    videoSizeChanged.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
                } catch (Exception e) {
                    Log.v(TAG, "wait was interrupted");
                }
            }
            videoWidth = mMediaPlayer.getVideoWidth();
            terminateMessageLooper();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }        
        return videoWidth;
    }

    //This also test the streaming video which may take a long
    //time to start the playback. 
    public static boolean videoSeekTo(String filePath) throws Exception {
        Log.v(TAG, "videoSeekTo - " + filePath);
        int currentPosition = 0;
        int duration = 0;
        boolean videoResult = false;
        MediaPlayer mp = new MediaPlayer();
        mp.setDataSource(filePath);
        mp.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
        mp.prepare();
        mp.start();
        if (filePath.equals(MediaNames.VIDEO_SHORT_3GP)){
            mp.pause();
            Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
            mp.seekTo(0);
            mp.start();
            Thread.sleep(1000);
            currentPosition = mp.getCurrentPosition();
            Log.v(TAG,"short position " + currentPosition);
            if (currentPosition > 100 )
                return true;
            else
                return false;
        }
        Thread.sleep(5000);
        duration = mp.getDuration();
        Log.v(TAG, "video duration " + duration);
        mp.pause();
        Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
        mp.seekTo(duration - 20000 );
        mp.start();
        Thread.sleep(1000);
        mp.pause();
        Thread.sleep(MediaNames.PAUSE_WAIT_TIME);
        mp.seekTo(duration/2);
        mp.start();
        Thread.sleep(10000);
        currentPosition = mp.getCurrentPosition();
        Log.v(TAG, "video currentPosition " + currentPosition);
        mp.release();
        if (currentPosition > (duration /2 )*0.9)
            return true;
        else
            return false;

    }

    public static boolean seekToEnd(String filePath){
        Log.v(TAG, "seekToEnd - " + filePath);
        int duration = 0;
        int currentPosition = 0;
        boolean isPlaying = false;
        MediaPlayer mp = new MediaPlayer();
        try{
            mp.setDataSource(filePath);
            Log.v(TAG, "start playback");
            mp.prepare();
            duration = mp.getDuration();
            mp.seekTo(duration - 3000);
            mp.start();
            Thread.sleep(6000);
        }catch (Exception e){}
        isPlaying = mp.isPlaying();
        currentPosition = mp.getCurrentPosition();
        Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
        mp.stop();
        mp.release();   
        Log.v(TAG, "duration = " + duration);
        if (currentPosition < 0.9 * duration || isPlaying)
            return false;
        else
            return true;        
    }

    public static boolean shortMediaStop(String filePath){
        Log.v(TAG, "shortMediaStop - " + filePath);
        //This test is only for the short media file
        int duration = 0;
        int currentPosition = 0;
        boolean isPlaying = false;
        MediaPlayer mp = new MediaPlayer();
        try{
            mp.setDataSource(filePath);
            Log.v(TAG, "start playback");
            mp.prepare();
            duration = mp.getDuration();
            mp.start();
            Thread.sleep(10000);
        }catch (Exception e){}
        isPlaying = mp.isPlaying();
        currentPosition = mp.getCurrentPosition();
        Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
        mp.stop();
        mp.release();   
        Log.v(TAG, "duration = " + duration);
        if (currentPosition > duration || isPlaying)
            return false;
        else
            return true;        
    }

    public static boolean playToEnd(String filePath){
        Log.v(TAG, "shortMediaStop - " + filePath);
        //This test is only for the short media file
        int duration = 200000;
        int updateDuration = 0;
        int currentPosition = 0;
        boolean isPlaying = false;
        MediaPlayer mp = new MediaPlayer();
        try{
            Thread.sleep(5000);
            mp.setDataSource(filePath);
            Log.v(TAG, "start playback");
            mp.prepare();
            //duration = mp.getDuration();
            mp.start();
            Thread.sleep(50000);
        }catch (Exception e){}
        isPlaying = mp.isPlaying();
        currentPosition = mp.getCurrentPosition();
        //updateDuration = mp.getDuration();
        Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
        mp.stop();
        mp.release();   
        //Log.v(TAG, "duration = " + duration);
        //Log.v(TAG, "Update duration = " + updateDuration);
        if (currentPosition > duration || isPlaying)
            return false;
        else
            return true;        
    }

    public static boolean seektoBeforeStart(String filePath){
        Log.v(TAG, "seektoBeforeStart - " + filePath);
        //This test is only for the short media file
        int duration = 0;
        int currentPosition = 0;

        MediaPlayer mp = new MediaPlayer();
        try{
            mp.setDataSource(filePath);
            mp.prepare();
            duration = mp.getDuration();
            mp.seekTo(duration - 10000);
            mp.start();
            currentPosition=mp.getCurrentPosition();
            mp.stop();
            mp.release();
        }catch (Exception e){}
        if (currentPosition < duration/2)
            return false;
        else
            return true;        
    }

    public static boolean mediaRecorderRecord(String filePath){
        Log.v(TAG, "SoundRecording - " + filePath);
        //This test is only for the short media file
        int duration = 0;
        try{
            MediaRecorder mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile(filePath);
            mRecorder.prepare();
            mRecorder.start();
            Thread.sleep(500);
            mRecorder.stop();
            Log.v(TAG, "sound recorded");
            mRecorder.release();
        }catch (Exception e){
            Log.v(TAG, e.toString());
        }  

        //Verify the recorded file
        MediaPlayer mp = new MediaPlayer();
        try{
            mp.setDataSource(filePath);
            mp.prepare();
            duration = mp.getDuration();
            Log.v(TAG,"Duration " + duration);
            mp.release();
        }catch (Exception e){}
        //Check the record media file length is greate than zero
        if (duration > 0)
            return true;
        else
            return false;

    }

    //Test for mediaMeta Data Thumbnail
    public static boolean getThumbnail(String filePath, String goldenPath){
        Log.v(TAG, "getThumbnail - " + filePath);

        int goldenHeight = 0;
        int goldenWidth = 0;
        int outputWidth = 0;
        int outputHeight = 0;

        //This test is only for the short media file
        try{
            BitmapFactory mBitmapFactory = new BitmapFactory();

            MediaMetadataRetriever mMediaMetadataRetriever = new MediaMetadataRetriever();
            try {
                mMediaMetadataRetriever.setDataSource(filePath);
            } catch(Exception e) {
                e.printStackTrace();
                return false;
            }
            Bitmap outThumbnail = mMediaMetadataRetriever.getFrameAtTime(-1);

            //Verify the thumbnail 
            Bitmap goldenBitmap = mBitmapFactory.decodeFile(goldenPath);
            outputWidth = outThumbnail.getWidth();
            outputHeight = outThumbnail.getHeight();
            goldenHeight = goldenBitmap.getHeight();
            goldenWidth = goldenBitmap.getWidth();

            //check the image dimension
            if ((outputWidth != goldenWidth) || (outputHeight != goldenHeight))
                return false;

            // Check half line of pixel
            int x = goldenHeight / 2;
            for (int j = 1; j < goldenWidth / 2; j++) {
                if (goldenBitmap.getPixel(x, j) != outThumbnail.getPixel(x, j)) {
                    Log.v(TAG, "pixel = " + goldenBitmap.getPixel(x, j));
                    return false;
                }
           }
        }catch (Exception e){
            Log.v(TAG, e.toString());
            return false;
        }
        return true;
    }

    //Load midi file from resources
    public static boolean resourcesPlayback(AssetFileDescriptor afd, int expectedDuration){
        int duration = 0;
        try{
            MediaPlayer mp = new MediaPlayer();
            mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
            mp.prepare();
            mp.start();
            duration = mp.getDuration();
            Thread.sleep(5000);
            mp.release();
        }catch (Exception e){
            Log.v(TAG,e.getMessage());
        }
        if (duration > expectedDuration)
            return true;
        else
            return false;
    }

    public static boolean prepareAsyncReset(String filePath){    
        //preparesAsync 
        try{
            MediaPlayer mp = new MediaPlayer();
            mp.setDataSource(filePath);
            mp.prepareAsync();
            mp.reset();
            mp.release();
        }catch (Exception e){
            Log.v(TAG,e.getMessage());
            return false;
        }
        return true;
    }


    public static boolean isLooping(String filePath) {        
        MediaPlayer mp = null;
        
        try {
            mp = new MediaPlayer();
            if (mp.isLooping()) {
                Log.v(TAG, "MediaPlayer.isLooping() returned true after ctor");
                return false;
            }
            mp.setDataSource(filePath);
            mp.prepare();

            mp.setLooping(true);
            if (!mp.isLooping()) {
                Log.v(TAG, "MediaPlayer.isLooping() returned false after setLooping(true)");
                return false;
            }
            
            mp.setLooping(false);
            if (mp.isLooping()) {
                Log.v(TAG, "MediaPlayer.isLooping() returned true after setLooping(false)");
                return false;
            }
        }catch (Exception e){
            Log.v(TAG, "Exception : " + e.toString());
            return false;
        } finally {
            if (mp != null)
                mp.release();
        }

        return true;
    }

    public static boolean isLoopingAfterReset(String filePath) {
        MediaPlayer mp = null;
        try {
            mp = new MediaPlayer();
            mp.setDataSource(filePath);
            mp.prepare();

            mp.setLooping(true);
            mp.reset();
            if (mp.isLooping()) {
                Log.v(TAG, "MediaPlayer.isLooping() returned true after reset()");
                return false;
            }
        }catch (Exception e){
            Log.v(TAG, "Exception : " + e.toString());
            return false;
        } finally {
            if (mp != null)
                mp.release();
        }

        return true;
    }
    
    /*
     * Initializes the message looper so that the mediaPlayer object can 
     * receive the callback messages.
     */
    private static void initializeMessageLooper() {
        Log.v(TAG, "start looper");
        new Thread() {
            @Override
            public void run() {
                // Set up a looper to be used by camera.
                Looper.prepare();
                Log.v(TAG, "start loopRun");
                // Save the looper so that we can terminate this thread 
                // after we are done with it.
                mLooper = Looper.myLooper();                
                mMediaPlayer = new MediaPlayer();                                
                synchronized (lock) {
                    mInitialized = true;
                    lock.notify();
                }
                Looper.loop();  // Blocks forever until Looper.quit() is called.
                Log.v(TAG, "initializeMessageLooper: quit.");
            }
        }.start();
    }
    
    /*
     * Terminates the message looper thread.
     */
    private static void terminateMessageLooper() {
        mLooper.quit();
        mMediaPlayer.release();
    }
    
    static MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            synchronized (prepareDone) {
                if(mPrepareReset){
                    Log.v(TAG, "call Reset");
                    mMediaPlayer.reset();
                }
                Log.v(TAG, "notify the prepare callback");
                prepareDone.notify();
                onPrepareSuccess = true;
            }
        }
    };
   
    public static boolean prepareAsyncCallback(String filePath, boolean reset) throws Exception {
        //Added the PrepareReset flag which allow us to switch to different
        //test case.
        if (reset){
            mPrepareReset = true;
        }
        
        synchronized (lock) {
            initializeMessageLooper();
            try {
                lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
            } catch(Exception e) {
                Log.v(TAG, "looper was interrupted.");
                return false;
            }
        }
        try{
            mMediaPlayer.setOnPreparedListener(mPreparedListener);
            mMediaPlayer.setDataSource(filePath);
            mMediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
            mMediaPlayer.prepareAsync(); 
            synchronized (prepareDone) {
                try {
                    prepareDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
                } catch (Exception e) {
                    Log.v(TAG, "wait was interrupted.");
                }
            }         
            terminateMessageLooper();
        }catch (Exception e){
            Log.v(TAG,e.getMessage());
        }      
       return onPrepareSuccess;
    }

    static MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
            synchronized (onCompletion) {
                Log.v(TAG, "notify the completion callback from OnCompletion");
                onCompletion.notify();
                onCompleteSuccess = true;
            }
        }
    };
    static MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {
        public boolean onError(MediaPlayer mp, int what, int extra) {
             synchronized (onCompletion) {
                 Log.v(TAG, "notify the completion callback from OnError what=" + what + ", extra=" + extra);
                 onCompletion.notify();
                 onCompleteSuccess = false;
             }
             //avoid to execute the complete again
             return true;
        }
    };

    // For each media file, forward twice and backward once, then play to the end
    public static boolean playMediaSamples(String filePath) throws Exception {
        int duration = 0;
        int curPosition = 0;
        int nextPosition = 0;
        int waittime = 0;
        Random r = new Random();
        initializeMessageLooper();
        synchronized (lock) {
            try {
                lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
            } catch(Exception e) {
                Log.v(TAG, "looper was interrupted.");
                return false;
            }
        }
        try {
            mMediaPlayer.setOnCompletionListener(mCompletionListener);
            mMediaPlayer.setOnErrorListener(mErrorListener);
            Log.v(TAG, "playMediaSamples: sample file name " + filePath);
            mMediaPlayer.setDataSource(filePath);
            mMediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
            mMediaPlayer.prepare();
            duration = mMediaPlayer.getDuration();
            Log.v(TAG, "playMediaSamples: duration = " + duration);
            // start to play
            mMediaPlayer.start();
            // randomly play for time within (0, duration/3)
            Thread.sleep(r.nextInt(duration/3));
            mMediaPlayer.pause();
            Log.v(TAG, "playMediaSamples: current position after pause: "
                        + mMediaPlayer.getCurrentPosition());
            // seek to position (0, 2/3*duration)
            nextPosition = mMediaPlayer.getCurrentPosition() + r.nextInt(duration/3);
            mMediaPlayer.seekTo(nextPosition);
            Log.v(TAG, "playMediaSamples: current position after the first seek:"
                        + mMediaPlayer.getCurrentPosition());
            // play for another short time
            mMediaPlayer.start();
            Thread.sleep(r.nextInt(duration/6));
            Log.v(TAG, "playMediaSamples: position after the second play:"
                        + mMediaPlayer.getCurrentPosition());
            // seek to a random position (0, duration)
            mMediaPlayer.seekTo(r.nextInt(duration));
            Log.v(TAG, "playMediaSamples: current position after the second seek:"
                        + mMediaPlayer.getCurrentPosition());
            waittime = duration - mMediaPlayer.getCurrentPosition();
            synchronized(onCompletion){
                try {
                    onCompletion.wait(waittime + 30000);
                }catch (Exception e) {
                    Log.v(TAG, "playMediaSamples are interrupted");
                    return false;
                }
            }
            terminateMessageLooper();
        }catch (Exception e) {
            Log.v(TAG, "playMediaSamples:" + e.getMessage());
        }
        return onCompleteSuccess;
    }
}