Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import android.media.MediaPlayer;
      4 import android.net.Uri;
      5 import android.widget.VideoView;
      6 import org.robolectric.annotation.Implementation;
      7 import org.robolectric.annotation.Implements;
      8 
      9 @Implements(VideoView.class)
     10 @SuppressWarnings({"UnusedDeclaration"})
     11 public class ShadowVideoView extends ShadowSurfaceView {
     12   private MediaPlayer.OnCompletionListener completionListner;
     13   private MediaPlayer.OnErrorListener errorListener;
     14   private MediaPlayer.OnPreparedListener preparedListener;
     15 
     16   private Uri uri;
     17   private String path;
     18   private int duration = 0;
     19 
     20   public static final int STOP = 0;
     21   public static final int START = 1;
     22   public static final int SUSPEND = 2;
     23   public static final int PAUSE = 3;
     24   public static final int RESUME = 4;
     25 
     26   private int currentState = -1;
     27   private int prevState;
     28   private int currentPosition;
     29 
     30   @Implementation
     31   public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
     32     preparedListener = l;
     33   }
     34 
     35   @Implementation
     36   public void setOnErrorListener(MediaPlayer.OnErrorListener l) {
     37     errorListener = l;
     38   }
     39 
     40   @Implementation
     41   public void setOnCompletionListener(MediaPlayer.OnCompletionListener l) {
     42     completionListner = l;
     43   }
     44 
     45   @Implementation
     46   public void setVideoPath(String path) {
     47     this.path = path;
     48   }
     49 
     50   @Implementation
     51   public void setVideoURI(Uri uri) {
     52     this.uri = uri;
     53   }
     54 
     55   @Implementation
     56   public void start() {
     57     savePrevState();
     58     currentState = ShadowVideoView.START;
     59   }
     60 
     61   @Implementation
     62   public void stopPlayback() {
     63     savePrevState();
     64     currentState = ShadowVideoView.STOP;
     65   }
     66 
     67   @Implementation
     68   public void suspend() {
     69     savePrevState();
     70     currentState = ShadowVideoView.SUSPEND;
     71   }
     72 
     73   @Implementation
     74   public void pause() {
     75     savePrevState();
     76     currentState = ShadowVideoView.PAUSE;
     77   }
     78 
     79   @Implementation
     80   public void resume() {
     81     savePrevState();
     82     currentState = ShadowVideoView.RESUME;
     83   }
     84 
     85   @Implementation
     86   public boolean isPlaying() {
     87     return (currentState == ShadowVideoView.START);
     88   }
     89 
     90   @Implementation
     91   public boolean canPause() {
     92     return (currentState != ShadowVideoView.PAUSE &&
     93         currentState != ShadowVideoView.STOP &&
     94         currentState != ShadowVideoView.SUSPEND);
     95   }
     96 
     97   @Implementation
     98   public void seekTo(int msec) {
     99     currentPosition = msec;
    100   }
    101 
    102   @Implementation
    103   public int getCurrentPosition() {
    104     return currentPosition;
    105   }
    106 
    107   @Implementation
    108   public int getDuration() {
    109     return duration;
    110   }
    111 
    112   /**
    113    * @return On prepared listener.
    114    */
    115   public MediaPlayer.OnPreparedListener getOnPreparedListener() {
    116     return preparedListener;
    117   }
    118 
    119   /**
    120    * @return On error listener.
    121    */
    122   public MediaPlayer.OnErrorListener getOnErrorListener() {
    123     return errorListener;
    124   }
    125 
    126   /**
    127    * @return On completion listener.
    128    */
    129   public MediaPlayer.OnCompletionListener getOnCompletionListener() {
    130     return completionListner;
    131   }
    132 
    133   /**
    134    * @return Video path.
    135    */
    136   public String getVideoPath() {
    137     return path;
    138   }
    139 
    140   /**
    141    * @return Video URI.
    142    */
    143   public String getVideoURIString() {
    144     return uri == null ? null : uri.toString();
    145   }
    146 
    147   /**
    148    * @return Current video state.
    149    */
    150   public int getCurrentVideoState() {
    151     return currentState;
    152   }
    153 
    154   /**
    155    * @return Previous video state.
    156    */
    157   public int getPrevVideoState() {
    158     return prevState;
    159   }
    160 
    161   public void setDuration(int duration) {
    162     this.duration = duration;
    163   }
    164 
    165   private void savePrevState() {
    166     prevState = currentState;
    167   }
    168 }
    169