• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
setOnPreparedListener(MediaPlayer.OnPreparedListener l)31   protected void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
32     preparedListener = l;
33   }
34 
35   @Implementation
setOnErrorListener(MediaPlayer.OnErrorListener l)36   protected void setOnErrorListener(MediaPlayer.OnErrorListener l) {
37     errorListener = l;
38   }
39 
40   @Implementation
setOnCompletionListener(MediaPlayer.OnCompletionListener l)41   protected void setOnCompletionListener(MediaPlayer.OnCompletionListener l) {
42     completionListner = l;
43   }
44 
45   @Implementation
setVideoPath(String path)46   protected void setVideoPath(String path) {
47     this.path = path;
48   }
49 
50   @Implementation
setVideoURI(Uri uri)51   protected void setVideoURI(Uri uri) {
52     this.uri = uri;
53   }
54 
55   @Implementation
start()56   protected void start() {
57     savePrevState();
58     currentState = ShadowVideoView.START;
59   }
60 
61   @Implementation
stopPlayback()62   protected void stopPlayback() {
63     savePrevState();
64     currentState = ShadowVideoView.STOP;
65   }
66 
67   @Implementation
suspend()68   protected void suspend() {
69     savePrevState();
70     currentState = ShadowVideoView.SUSPEND;
71   }
72 
73   @Implementation
pause()74   protected void pause() {
75     savePrevState();
76     currentState = ShadowVideoView.PAUSE;
77   }
78 
79   @Implementation
resume()80   protected void resume() {
81     savePrevState();
82     currentState = ShadowVideoView.RESUME;
83   }
84 
85   @Implementation
isPlaying()86   protected boolean isPlaying() {
87     return (currentState == ShadowVideoView.START);
88   }
89 
90   @Implementation
canPause()91   protected boolean canPause() {
92     return (currentState != ShadowVideoView.PAUSE &&
93         currentState != ShadowVideoView.STOP &&
94         currentState != ShadowVideoView.SUSPEND);
95   }
96 
97   @Implementation
seekTo(int msec)98   protected void seekTo(int msec) {
99     currentPosition = msec;
100   }
101 
102   @Implementation
getCurrentPosition()103   protected int getCurrentPosition() {
104     return currentPosition;
105   }
106 
107   @Implementation
getDuration()108   protected int getDuration() {
109     return duration;
110   }
111 
112   /**
113    * @return On prepared listener.
114    */
getOnPreparedListener()115   public MediaPlayer.OnPreparedListener getOnPreparedListener() {
116     return preparedListener;
117   }
118 
119   /**
120    * @return On error listener.
121    */
getOnErrorListener()122   public MediaPlayer.OnErrorListener getOnErrorListener() {
123     return errorListener;
124   }
125 
126   /**
127    * @return On completion listener.
128    */
getOnCompletionListener()129   public MediaPlayer.OnCompletionListener getOnCompletionListener() {
130     return completionListner;
131   }
132 
133   /**
134    * @return Video path.
135    */
getVideoPath()136   public String getVideoPath() {
137     return path;
138   }
139 
140   /**
141    * @return Video URI.
142    */
getVideoURIString()143   public String getVideoURIString() {
144     return uri == null ? null : uri.toString();
145   }
146 
147   /**
148    * @return Current video state.
149    */
getCurrentVideoState()150   public int getCurrentVideoState() {
151     return currentState;
152   }
153 
154   /**
155    * @return Previous video state.
156    */
getPrevVideoState()157   public int getPrevVideoState() {
158     return prevState;
159   }
160 
setDuration(int duration)161   public void setDuration(int duration) {
162     this.duration = duration;
163   }
164 
savePrevState()165   private void savePrevState() {
166     prevState = currentState;
167   }
168 }
169