1 package com.xtremelabs.robolectric.shadows; 2 3 import android.content.Context; 4 import android.media.MediaPlayer; 5 import android.net.Uri; 6 7 import com.xtremelabs.robolectric.internal.Implementation; 8 import com.xtremelabs.robolectric.internal.Implements; 9 import com.xtremelabs.robolectric.internal.RealObject; 10 11 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 12 13 /** 14 * Shadows the Android {@code MediaPlayer} class. 15 */ 16 @Implements(MediaPlayer.class) 17 public class ShadowMediaPlayer { 18 19 @RealObject private MediaPlayer player; 20 21 private boolean playing; 22 private boolean prepared; 23 private int currentPosition; 24 private Uri sourceUri; 25 private int sourceResId; 26 private MediaPlayer.OnCompletionListener completionListener; 27 private MediaPlayer.OnPreparedListener preparedListener; 28 29 @Implementation create(Context context, int resId)30 public static MediaPlayer create(Context context, int resId) { 31 MediaPlayer mp = new MediaPlayer(); 32 shadowOf(mp).sourceResId = resId; 33 try { 34 mp.prepare(); 35 } catch (Exception e) { return null; } 36 37 return mp; 38 } 39 40 @Implementation create(Context context, Uri uri)41 public static MediaPlayer create(Context context, Uri uri) { 42 MediaPlayer mp = new MediaPlayer(); 43 try { 44 mp.setDataSource(context, uri); 45 mp.prepare(); 46 } catch (Exception e) { return null; } 47 48 return mp; 49 } 50 __constructor__()51 public void __constructor__() { 52 playing = true; 53 } 54 55 @Implementation setDataSource(Context context, Uri uri)56 public void setDataSource(Context context, Uri uri) { 57 this.sourceUri = uri; 58 } 59 60 @Implementation setOnCompletionListener(MediaPlayer.OnCompletionListener listener)61 public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) { 62 completionListener = listener; 63 } 64 65 @Implementation setOnPreparedListener(MediaPlayer.OnPreparedListener listener)66 public void setOnPreparedListener(MediaPlayer.OnPreparedListener listener) { 67 preparedListener = listener; 68 } 69 70 @Implementation isPlaying()71 public boolean isPlaying() { 72 return playing; 73 } 74 75 @Implementation prepare()76 public void prepare() { 77 prepared = true; 78 invokePreparedListener(); 79 } 80 81 /** 82 * Test cases are expected to simulate completion of the 'prepare' phase 83 * by manually invoking {@code #invokePreparedListener}. 84 */ 85 @Implementation prepareAsync()86 public void prepareAsync() { 87 prepared = true; 88 } 89 90 @Implementation start()91 public void start() { 92 playing = true; 93 } 94 95 @Implementation pause()96 public void pause() { 97 playing = false; 98 } 99 100 @Implementation release()101 public void release() { 102 playing = false; 103 prepared = false; 104 } 105 106 @Implementation reset()107 public void reset() { 108 playing = false; 109 prepared = false; 110 } 111 112 @Implementation stop()113 public void stop() { 114 playing = false; 115 } 116 117 @Implementation getCurrentPosition()118 public int getCurrentPosition() { 119 return currentPosition; 120 } 121 setCurrentPosition(int position)122 public void setCurrentPosition(int position) { 123 currentPosition = position; 124 } 125 126 /** 127 * Non-Android accessor. Use for assertions. 128 * @return 129 */ getSourceUri()130 public Uri getSourceUri() { 131 return sourceUri; 132 } 133 134 /** 135 * Non-Android accessor. Use for assertions. 136 * @return 137 */ getSourceResId()138 public int getSourceResId() { 139 return sourceResId; 140 } 141 142 /** 143 * Non-Android accessor. Use for assertions. 144 * @return 145 */ isPrepared()146 public boolean isPrepared() { 147 return prepared; 148 } 149 150 /** 151 * Non-Android accessor. Use for assertions. 152 * @return 153 */ getOnCompletionListener()154 public MediaPlayer.OnCompletionListener getOnCompletionListener() { 155 return completionListener; 156 } 157 158 /** 159 * Non-Android accessor. Use for assertions. 160 * @return 161 */ getOnPreparedListener()162 public MediaPlayer.OnPreparedListener getOnPreparedListener() { 163 return preparedListener; 164 } 165 166 /** 167 * Allows test cases to simulate 'prepared' state by invoking callback. 168 */ invokePreparedListener()169 public void invokePreparedListener() { 170 if (preparedListener == null) return; 171 preparedListener.onPrepared( player ); 172 } 173 174 /** 175 * Allows test cases to simulate 'completed' state by invoking callback. 176 */ invokeCompletionListener()177 public void invokeCompletionListener() { 178 if (completionListener == null) return; 179 completionListener.onCompletion( player ); 180 } 181 } 182