1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mediaframeworktest.functional; 18 19 20 21 //import android.content.Resources; 22 import com.android.mediaframeworktest.MediaFrameworkTest; 23 import com.android.mediaframeworktest.MediaNames; 24 25 import android.content.res.AssetFileDescriptor; 26 import android.graphics.Bitmap; 27 import android.graphics.BitmapFactory; 28 import android.media.MediaMetadataRetriever; 29 import android.media.MediaPlayer; 30 import android.media.MediaRecorder; 31 import android.os.Looper; 32 import android.os.SystemClock; 33 import android.util.Log; 34 35 import java.io.File; 36 import java.io.FileWriter; 37 import java.io.IOException; 38 import java.io.InputStream; 39 import java.io.OutputStream; 40 import java.io.Writer; 41 import java.io.FileOutputStream; 42 import java.util.Random; 43 /** 44 * Junit / Instrumentation test case for the media player api 45 46 */ 47 public class CodecTest { 48 private static String TAG = "MediaPlayerApiTest"; 49 private static MediaPlayer mMediaPlayer; 50 private MediaPlayer.OnPreparedListener mOnPreparedListener; 51 52 private static int WAIT_FOR_COMMAND_TO_COMPLETE = 60000; //1 min max. 53 private static boolean mInitialized = false; 54 private static boolean mPrepareReset = false; 55 private static Looper mLooper = null; 56 private static final Object lock = new Object(); 57 private static final Object prepareDone = new Object(); 58 private static final Object videoSizeChanged = new Object(); 59 private static final Object onCompletion = new Object(); 60 private static boolean onPrepareSuccess = false; 61 private static boolean onCompleteSuccess = false; 62 63 public static String printCpuInfo(){ 64 String cm = "dumpsys cpuinfo"; 65 String cpuinfo =null; 66 int ch; 67 try{ 68 Process p = Runtime.getRuntime().exec(cm); 69 InputStream in = p.getInputStream(); 70 StringBuffer sb = new StringBuffer(512); 71 while ( ( ch = in.read() ) != -1 ){ 72 sb.append((char) ch); 73 } 74 cpuinfo = sb.toString(); 75 }catch (IOException e){ 76 Log.v(TAG, e.toString()); 77 } 78 return cpuinfo; 79 } 80 81 82 public static int getDuration(String filePath) { 83 Log.v(TAG, "getDuration - " + filePath); 84 MediaPlayer mp = new MediaPlayer(); 85 try{ 86 mp.setDataSource(filePath); 87 mp.prepare(); 88 }catch (Exception e){ 89 Log.v(TAG, e.toString()); 90 } 91 int duration = mp.getDuration(); 92 Log.v(TAG, "Duration " + duration); 93 mp.release(); 94 Log.v(TAG, "release"); 95 return duration; 96 } 97 98 public static boolean getCurrentPosition(String filePath){ 99 Log.v(TAG, "GetCurrentPosition - " + filePath); 100 int currentPosition = 0; 101 long t1=0; 102 long t2 =0; 103 MediaPlayer mp = new MediaPlayer(); 104 try{ 105 mp.setDataSource(filePath); 106 Log.v(TAG, "start playback"); 107 mp.prepare(); 108 mp.start(); 109 t1=SystemClock.uptimeMillis(); 110 Thread.sleep(10000); 111 mp.pause(); 112 Thread.sleep(MediaNames.PAUSE_WAIT_TIME); 113 t2=SystemClock.uptimeMillis(); 114 }catch (Exception e){ 115 Log.v(TAG, e.toString()); 116 } 117 currentPosition = mp.getCurrentPosition(); 118 mp.stop(); 119 mp.release(); 120 Log.v(TAG, "mp currentPositon = " + currentPosition + " play duration = " + (t2-t1)); 121 //The currentposition should be within 10% of the sleep time 122 //For the very short mp3, it should return the length instead of 10 seconds 123 if (filePath.equals(MediaNames.SHORTMP3)){ 124 if (currentPosition < 1000 ) 125 return true; 126 } 127 if ((currentPosition < ((t2-t1) *1.2)) && (currentPosition > 0)) 128 return true; 129 else 130 return false; 131 } 132 133 public static boolean seekTo(String filePath){ 134 Log.v(TAG, "seekTo " + filePath); 135 int currentPosition = 0; 136 MediaPlayer mp = new MediaPlayer(); 137 try{ 138 mp.setDataSource(filePath); 139 mp.prepare(); 140 mp.start(); 141 mp.seekTo(MediaNames.SEEK_TIME); 142 Thread.sleep(MediaNames.WAIT_TIME); 143 currentPosition = mp.getCurrentPosition(); 144 }catch (Exception e){ 145 Log.v(TAG, e.getMessage()); 146 } 147 mp.stop(); 148 mp.release(); 149 Log.v(TAG, "CurrentPosition = " + currentPosition); 150 //The currentposition should be at least greater than the 80% of seek time 151 if ((currentPosition > MediaNames.SEEK_TIME *0.8)) 152 return true; 153 else 154 return false; 155 } 156 157 public static boolean setLooping(String filePath){ 158 int currentPosition = 0; 159 int duration = 0; 160 long t1 =0; 161 long t2 =0; 162 Log.v (TAG, "SetLooping - " + filePath); 163 MediaPlayer mp = new MediaPlayer(); 164 try{ 165 mp.setDataSource(filePath); 166 mp.prepare(); 167 duration = mp.getDuration(); 168 Log.v(TAG, "setLooping duration " + duration); 169 mp.setLooping(true); 170 mp.start(); 171 Thread.sleep(5000); 172 mp.seekTo(duration - 5000); 173 t1=SystemClock.uptimeMillis(); 174 Thread.sleep(20000); 175 t2=SystemClock.uptimeMillis(); 176 Log.v(TAG, "pause"); 177 //Bug# 1106852 - IllegalStateException will be thrown if pause is called 178 //in here 179 //mp.pause(); 180 currentPosition = mp.getCurrentPosition(); 181 Log.v(TAG, "looping position " + currentPosition + "duration = " + (t2-t1)); 182 }catch (Exception e){ 183 Log.v(TAG, "Exception : " + e.toString()); 184 } 185 mp.stop(); 186 mp.release(); 187 //The current position should be within 20% of the sleep time 188 //and should be greater than zero. 189 if ((currentPosition < ((t2-t1-5000)*1.2)) && currentPosition > 0) 190 return true; 191 else 192 return false; 193 } 194 195 public static boolean pause(String filePath) throws Exception { 196 Log.v(TAG, "pause - " + filePath); 197 boolean misPlaying = true; 198 boolean pauseResult = false; 199 long t1=0; 200 long t2=0; 201 MediaPlayer mp = new MediaPlayer(); 202 mp.setDataSource(filePath); 203 mp.prepare(); 204 int duration = mp.getDuration(); 205 mp.start(); 206 t1=SystemClock.uptimeMillis(); 207 Thread.sleep(5000); 208 mp.pause(); 209 Thread.sleep(MediaNames.PAUSE_WAIT_TIME); 210 t2=SystemClock.uptimeMillis(); 211 misPlaying = mp.isPlaying(); 212 int curPosition = mp.getCurrentPosition(); 213 Log.v(TAG, filePath + " pause currentPositon " + curPosition); 214 Log.v(TAG, "isPlaying "+ misPlaying + " wait time " + (t2 - t1) ); 215 String cpuinfo = printCpuInfo(); 216 Log.v(TAG, cpuinfo); 217 if ((curPosition>0) && (curPosition < ((t2-t1) * 1.3)) && (misPlaying == false)) 218 pauseResult = true; 219 mp.stop(); 220 mp.release(); 221 return pauseResult; 222 } 223 224 public static void prepareStopRelease(String filePath) throws Exception { 225 Log.v(TAG, "prepareStopRelease" + filePath); 226 MediaPlayer mp = new MediaPlayer(); 227 mp.setDataSource(filePath); 228 mp.prepare(); 229 mp.stop(); 230 mp.release(); 231 } 232 233 public static void preparePauseRelease(String filePath) throws Exception { 234 Log.v(TAG, "preparePauseRelease" + filePath); 235 MediaPlayer mp = new MediaPlayer(); 236 mp.setDataSource(filePath); 237 mp.prepare(); 238 mp.pause(); 239 mp.release(); 240 } 241 242 static MediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangedListener = 243 new MediaPlayer.OnVideoSizeChangedListener() { 244 public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { 245 synchronized (videoSizeChanged) { 246 Log.v(TAG, "sizechanged notification received ..."); 247 videoSizeChanged.notify(); 248 } 249 } 250 }; 251 252 //Register the videoSizeChanged listener 253 public static int videoHeight(String filePath) throws Exception { 254 Log.v(TAG, "videoHeight - " + filePath); 255 int videoHeight = 0; 256 synchronized (lock) { 257 initializeMessageLooper(); 258 try { 259 lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 260 } catch(Exception e) { 261 Log.v(TAG, "looper was interrupted."); 262 return 0; 263 } 264 } 265 try { 266 mMediaPlayer.setDataSource(filePath); 267 mMediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder()); 268 mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener); 269 synchronized (videoSizeChanged) { 270 try { 271 mMediaPlayer.prepare(); 272 mMediaPlayer.start(); 273 videoSizeChanged.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 274 } catch (Exception e) { 275 Log.v(TAG, "wait was interrupted"); 276 } 277 } 278 videoHeight = mMediaPlayer.getVideoHeight(); 279 terminateMessageLooper(); 280 } catch (Exception e) { 281 Log.e(TAG, e.getMessage()); 282 } 283 284 return videoHeight; 285 } 286 287 //Register the videoSizeChanged listener 288 public static int videoWidth(String filePath) throws Exception { 289 Log.v(TAG, "videoWidth - " + filePath); 290 int videoWidth = 0; 291 292 synchronized (lock) { 293 initializeMessageLooper(); 294 try { 295 lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 296 } catch(Exception e) { 297 Log.v(TAG, "looper was interrupted."); 298 return 0; 299 } 300 } 301 try { 302 mMediaPlayer.setDataSource(filePath); 303 mMediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder()); 304 mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener); 305 synchronized (videoSizeChanged) { 306 try { 307 mMediaPlayer.prepare(); 308 mMediaPlayer.start(); 309 videoSizeChanged.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 310 } catch (Exception e) { 311 Log.v(TAG, "wait was interrupted"); 312 } 313 } 314 videoWidth = mMediaPlayer.getVideoWidth(); 315 terminateMessageLooper(); 316 } catch (Exception e) { 317 Log.e(TAG, e.getMessage()); 318 } 319 return videoWidth; 320 } 321 322 //This also test the streaming video which may take a long 323 //time to start the playback. 324 public static boolean videoSeekTo(String filePath) throws Exception { 325 Log.v(TAG, "videoSeekTo - " + filePath); 326 int currentPosition = 0; 327 int duration = 0; 328 boolean videoResult = false; 329 MediaPlayer mp = new MediaPlayer(); 330 mp.setDataSource(filePath); 331 mp.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder()); 332 mp.prepare(); 333 mp.start(); 334 if (filePath.equals(MediaNames.VIDEO_SHORT_3GP)){ 335 mp.pause(); 336 Thread.sleep(MediaNames.PAUSE_WAIT_TIME); 337 mp.seekTo(0); 338 mp.start(); 339 Thread.sleep(1000); 340 currentPosition = mp.getCurrentPosition(); 341 Log.v(TAG,"short position " + currentPosition); 342 if (currentPosition > 100 ) 343 return true; 344 else 345 return false; 346 } 347 Thread.sleep(5000); 348 duration = mp.getDuration(); 349 Log.v(TAG, "video duration " + duration); 350 mp.pause(); 351 Thread.sleep(MediaNames.PAUSE_WAIT_TIME); 352 mp.seekTo(duration - 20000 ); 353 mp.start(); 354 Thread.sleep(1000); 355 mp.pause(); 356 Thread.sleep(MediaNames.PAUSE_WAIT_TIME); 357 mp.seekTo(duration/2); 358 mp.start(); 359 Thread.sleep(10000); 360 currentPosition = mp.getCurrentPosition(); 361 Log.v(TAG, "video currentPosition " + currentPosition); 362 mp.release(); 363 if (currentPosition > (duration /2 )*0.9) 364 return true; 365 else 366 return false; 367 368 } 369 370 public static boolean seekToEnd(String filePath){ 371 Log.v(TAG, "seekToEnd - " + filePath); 372 int duration = 0; 373 int currentPosition = 0; 374 boolean isPlaying = false; 375 MediaPlayer mp = new MediaPlayer(); 376 try{ 377 mp.setDataSource(filePath); 378 Log.v(TAG, "start playback"); 379 mp.prepare(); 380 duration = mp.getDuration(); 381 mp.seekTo(duration - 3000); 382 mp.start(); 383 Thread.sleep(6000); 384 }catch (Exception e){} 385 isPlaying = mp.isPlaying(); 386 currentPosition = mp.getCurrentPosition(); 387 Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying); 388 mp.stop(); 389 mp.release(); 390 Log.v(TAG, "duration = " + duration); 391 if (currentPosition < 0.9 * duration || isPlaying) 392 return false; 393 else 394 return true; 395 } 396 397 public static boolean shortMediaStop(String filePath){ 398 Log.v(TAG, "shortMediaStop - " + filePath); 399 //This test is only for the short media file 400 int duration = 0; 401 int currentPosition = 0; 402 boolean isPlaying = false; 403 MediaPlayer mp = new MediaPlayer(); 404 try{ 405 mp.setDataSource(filePath); 406 Log.v(TAG, "start playback"); 407 mp.prepare(); 408 duration = mp.getDuration(); 409 mp.start(); 410 Thread.sleep(10000); 411 }catch (Exception e){} 412 isPlaying = mp.isPlaying(); 413 currentPosition = mp.getCurrentPosition(); 414 Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying); 415 mp.stop(); 416 mp.release(); 417 Log.v(TAG, "duration = " + duration); 418 if (currentPosition > duration || isPlaying) 419 return false; 420 else 421 return true; 422 } 423 424 public static boolean playToEnd(String filePath){ 425 Log.v(TAG, "shortMediaStop - " + filePath); 426 //This test is only for the short media file 427 int duration = 200000; 428 int updateDuration = 0; 429 int currentPosition = 0; 430 boolean isPlaying = false; 431 MediaPlayer mp = new MediaPlayer(); 432 try{ 433 Thread.sleep(5000); 434 mp.setDataSource(filePath); 435 Log.v(TAG, "start playback"); 436 mp.prepare(); 437 //duration = mp.getDuration(); 438 mp.start(); 439 Thread.sleep(50000); 440 }catch (Exception e){} 441 isPlaying = mp.isPlaying(); 442 currentPosition = mp.getCurrentPosition(); 443 //updateDuration = mp.getDuration(); 444 Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying); 445 mp.stop(); 446 mp.release(); 447 //Log.v(TAG, "duration = " + duration); 448 //Log.v(TAG, "Update duration = " + updateDuration); 449 if (currentPosition > duration || isPlaying) 450 return false; 451 else 452 return true; 453 } 454 455 public static boolean seektoBeforeStart(String filePath){ 456 Log.v(TAG, "seektoBeforeStart - " + filePath); 457 //This test is only for the short media file 458 int duration = 0; 459 int currentPosition = 0; 460 461 MediaPlayer mp = new MediaPlayer(); 462 try{ 463 mp.setDataSource(filePath); 464 mp.prepare(); 465 duration = mp.getDuration(); 466 mp.seekTo(duration - 10000); 467 mp.start(); 468 currentPosition=mp.getCurrentPosition(); 469 mp.stop(); 470 mp.release(); 471 }catch (Exception e){} 472 if (currentPosition < duration/2) 473 return false; 474 else 475 return true; 476 } 477 478 public static boolean mediaRecorderRecord(String filePath){ 479 Log.v(TAG, "SoundRecording - " + filePath); 480 //This test is only for the short media file 481 int duration = 0; 482 try{ 483 MediaRecorder mRecorder = new MediaRecorder(); 484 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 485 mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 486 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 487 mRecorder.setOutputFile(filePath); 488 mRecorder.prepare(); 489 mRecorder.start(); 490 Thread.sleep(500); 491 mRecorder.stop(); 492 Log.v(TAG, "sound recorded"); 493 mRecorder.release(); 494 }catch (Exception e){ 495 Log.v(TAG, e.toString()); 496 } 497 498 //Verify the recorded file 499 MediaPlayer mp = new MediaPlayer(); 500 try{ 501 mp.setDataSource(filePath); 502 mp.prepare(); 503 duration = mp.getDuration(); 504 Log.v(TAG,"Duration " + duration); 505 mp.release(); 506 }catch (Exception e){} 507 //Check the record media file length is greate than zero 508 if (duration > 0) 509 return true; 510 else 511 return false; 512 513 } 514 515 //Test for mediaMeta Data Thumbnail 516 public static boolean getThumbnail(String filePath, String goldenPath){ 517 Log.v(TAG, "getThumbnail - " + filePath); 518 519 int goldenHeight = 0; 520 int goldenWidth = 0; 521 int outputWidth = 0; 522 int outputHeight = 0; 523 524 //This test is only for the short media file 525 try{ 526 BitmapFactory mBitmapFactory = new BitmapFactory(); 527 528 MediaMetadataRetriever mMediaMetadataRetriever = new MediaMetadataRetriever(); 529 try { 530 mMediaMetadataRetriever.setDataSource(filePath); 531 } catch(Exception e) { 532 e.printStackTrace(); 533 return false; 534 } 535 Bitmap outThumbnail = mMediaMetadataRetriever.captureFrame(); 536 537 //Verify the thumbnail 538 Bitmap goldenBitmap = mBitmapFactory.decodeFile(goldenPath); 539 outputWidth = outThumbnail.getWidth(); 540 outputHeight = outThumbnail.getHeight(); 541 goldenHeight = goldenBitmap.getHeight(); 542 goldenWidth = goldenBitmap.getWidth(); 543 544 //check the image dimension 545 if ((outputWidth != goldenWidth) || (outputHeight != goldenHeight)) 546 return false; 547 548 // Check half line of pixel 549 int x = goldenHeight / 2; 550 for (int j = 1; j < goldenWidth / 2; j++) { 551 if (goldenBitmap.getPixel(x, j) != outThumbnail.getPixel(x, j)) { 552 Log.v(TAG, "pixel = " + goldenBitmap.getPixel(x, j)); 553 return false; 554 } 555 } 556 }catch (Exception e){ 557 Log.v(TAG, e.toString()); 558 return false; 559 } 560 return true; 561 } 562 563 //Load midi file from resources 564 public static boolean resourcesPlayback(AssetFileDescriptor afd, int expectedDuration){ 565 int duration = 0; 566 try{ 567 MediaPlayer mp = new MediaPlayer(); 568 mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength()); 569 mp.prepare(); 570 mp.start(); 571 duration = mp.getDuration(); 572 Thread.sleep(5000); 573 mp.release(); 574 }catch (Exception e){ 575 Log.v(TAG,e.getMessage()); 576 } 577 if (duration > expectedDuration) 578 return true; 579 else 580 return false; 581 } 582 583 public static boolean prepareAsyncReset(String filePath){ 584 //preparesAsync 585 try{ 586 MediaPlayer mp = new MediaPlayer(); 587 mp.setDataSource(filePath); 588 mp.prepareAsync(); 589 mp.reset(); 590 mp.release(); 591 }catch (Exception e){ 592 Log.v(TAG,e.getMessage()); 593 return false; 594 } 595 return true; 596 } 597 598 599 public static boolean isLooping(String filePath) { 600 MediaPlayer mp = null; 601 602 try { 603 mp = new MediaPlayer(); 604 if (mp.isLooping()) { 605 Log.v(TAG, "MediaPlayer.isLooping() returned true after ctor"); 606 return false; 607 } 608 mp.setDataSource(filePath); 609 mp.prepare(); 610 611 mp.setLooping(true); 612 if (!mp.isLooping()) { 613 Log.v(TAG, "MediaPlayer.isLooping() returned false after setLooping(true)"); 614 return false; 615 } 616 617 mp.setLooping(false); 618 if (mp.isLooping()) { 619 Log.v(TAG, "MediaPlayer.isLooping() returned true after setLooping(false)"); 620 return false; 621 } 622 }catch (Exception e){ 623 Log.v(TAG, "Exception : " + e.toString()); 624 return false; 625 } finally { 626 if (mp != null) 627 mp.release(); 628 } 629 630 return true; 631 } 632 633 public static boolean isLoopingAfterReset(String filePath) { 634 MediaPlayer mp = null; 635 try { 636 mp = new MediaPlayer(); 637 mp.setDataSource(filePath); 638 mp.prepare(); 639 640 mp.setLooping(true); 641 mp.reset(); 642 if (mp.isLooping()) { 643 Log.v(TAG, "MediaPlayer.isLooping() returned true after reset()"); 644 return false; 645 } 646 }catch (Exception e){ 647 Log.v(TAG, "Exception : " + e.toString()); 648 return false; 649 } finally { 650 if (mp != null) 651 mp.release(); 652 } 653 654 return true; 655 } 656 657 /* 658 * Initializes the message looper so that the mediaPlayer object can 659 * receive the callback messages. 660 */ 661 private static void initializeMessageLooper() { 662 Log.v(TAG, "start looper"); 663 new Thread() { 664 @Override 665 public void run() { 666 // Set up a looper to be used by camera. 667 Looper.prepare(); 668 Log.v(TAG, "start loopRun"); 669 // Save the looper so that we can terminate this thread 670 // after we are done with it. 671 mLooper = Looper.myLooper(); 672 mMediaPlayer = new MediaPlayer(); 673 synchronized (lock) { 674 mInitialized = true; 675 lock.notify(); 676 } 677 Looper.loop(); // Blocks forever until Looper.quit() is called. 678 Log.v(TAG, "initializeMessageLooper: quit."); 679 } 680 }.start(); 681 } 682 683 /* 684 * Terminates the message looper thread. 685 */ 686 private static void terminateMessageLooper() { 687 mLooper.quit(); 688 mMediaPlayer.release(); 689 } 690 691 static MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() { 692 public void onPrepared(MediaPlayer mp) { 693 synchronized (prepareDone) { 694 if(mPrepareReset){ 695 Log.v(TAG, "call Reset"); 696 mMediaPlayer.reset(); 697 } 698 Log.v(TAG, "notify the prepare callback"); 699 prepareDone.notify(); 700 onPrepareSuccess = true; 701 } 702 } 703 }; 704 705 public static boolean prepareAsyncCallback(String filePath, boolean reset) throws Exception { 706 //Added the PrepareReset flag which allow us to switch to different 707 //test case. 708 if (reset){ 709 mPrepareReset = true; 710 } 711 712 synchronized (lock) { 713 initializeMessageLooper(); 714 try { 715 lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 716 } catch(Exception e) { 717 Log.v(TAG, "looper was interrupted."); 718 return false; 719 } 720 } 721 try{ 722 mMediaPlayer.setOnPreparedListener(mPreparedListener); 723 mMediaPlayer.setDataSource(filePath); 724 mMediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder()); 725 mMediaPlayer.prepareAsync(); 726 synchronized (prepareDone) { 727 try { 728 prepareDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 729 } catch (Exception e) { 730 Log.v(TAG, "wait was interrupted."); 731 } 732 } 733 terminateMessageLooper(); 734 }catch (Exception e){ 735 Log.v(TAG,e.getMessage()); 736 } 737 return onPrepareSuccess; 738 } 739 740 static MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() { 741 public void onCompletion(MediaPlayer mp) { 742 synchronized (onCompletion) { 743 Log.v(TAG, "notify the completion callback"); 744 onCompletion.notify(); 745 onCompleteSuccess = true; 746 } 747 } 748 }; 749 750 // For each media file, forward twice and backward once, then play to the end 751 public static boolean playMediaSamples(String filePath) throws Exception { 752 int duration = 0; 753 int curPosition = 0; 754 int nextPosition = 0; 755 int waittime = 0; 756 Random r = new Random(); 757 initializeMessageLooper(); 758 synchronized (lock) { 759 try { 760 lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 761 } catch(Exception e) { 762 Log.v(TAG, "looper was interrupted."); 763 return false; 764 } 765 } 766 try { 767 mMediaPlayer.setOnCompletionListener(mCompletionListener); 768 Log.v(TAG, "playMediaSamples: sample file name " + filePath); 769 mMediaPlayer.setDataSource(filePath); 770 mMediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder()); 771 mMediaPlayer.prepare(); 772 duration = mMediaPlayer.getDuration(); 773 Log.v(TAG, "playMediaSamples: duration = " + duration); 774 // start to play 775 mMediaPlayer.start(); 776 // randomly play for time within (0, duration/3) 777 Thread.sleep(r.nextInt(duration/3)); 778 mMediaPlayer.pause(); 779 Log.v(TAG, "playMediaSamples: current position after pause: " 780 + mMediaPlayer.getCurrentPosition()); 781 // seek to position (0, 2/3*duration) 782 nextPosition = mMediaPlayer.getCurrentPosition() + r.nextInt(duration/3); 783 mMediaPlayer.seekTo(nextPosition); 784 Log.v(TAG, "playMediaSamples: current position after the first seek:" 785 + mMediaPlayer.getCurrentPosition()); 786 // play for another short time 787 mMediaPlayer.start(); 788 Thread.sleep(r.nextInt(duration/6)); 789 Log.v(TAG, "playMediaSamples: position after the second play:" 790 + mMediaPlayer.getCurrentPosition()); 791 // seek to a random position (0, duration) 792 mMediaPlayer.seekTo(r.nextInt(duration)); 793 Log.v(TAG, "playMediaSamples: current position after the second seek:" 794 + mMediaPlayer.getCurrentPosition()); 795 waittime = duration - mMediaPlayer.getCurrentPosition(); 796 synchronized(onCompletion){ 797 try { 798 onCompletion.wait(waittime + 30000); 799 }catch (Exception e) { 800 Log.v(TAG, "playMediaSamples are interrupted"); 801 return false; 802 } 803 } 804 terminateMessageLooper(); 805 }catch (Exception e) { 806 Log.v(TAG, "playMediaSamples:" + e.getMessage()); 807 } 808 return onCompleteSuccess; 809 } 810 } 811