Rename and add test cases for video player in accessibility.
Bug: 168567356 Test: make RunSettingsRoboTests ROBOTEST_FILTER=VideoPlayerTest Change-Id: I2ea415b06d2870f55650290f48dd9b2998ef608d
This commit is contained in:
@@ -25,6 +25,7 @@ import android.view.TextureView.SurfaceTextureListener;
|
||||
|
||||
import androidx.annotation.GuardedBy;
|
||||
import androidx.annotation.RawRes;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* Plays the video by {@link MediaPlayer} on {@link TextureView}, calls {@link #create(Context, int,
|
||||
@@ -32,19 +33,25 @@ import androidx.annotation.RawRes;
|
||||
* is no longer used, call {@link #release()} so that MediaPlayer object can be released.
|
||||
*/
|
||||
public class VideoPlayer implements SurfaceTextureListener {
|
||||
private final Context context;
|
||||
private final Object mediaPlayerLock = new Object();
|
||||
private final Context mContext;
|
||||
private final Object mMediaPlayerLock = new Object();
|
||||
// Media player object can't be used after it has been released, so it will be set to null. But
|
||||
// VideoPlayer is asynchronized, media player object might be paused or resumed again before
|
||||
// released media player is set to null. Therefore, lock mediaPlayer and mediaPlayerState by
|
||||
// mediaPlayerLock keep their states consistent.
|
||||
@VisibleForTesting
|
||||
@GuardedBy("mediaPlayerLock")
|
||||
private MediaPlayer mediaPlayer;
|
||||
MediaPlayer mMediaPlayer;
|
||||
|
||||
@VisibleForTesting
|
||||
@GuardedBy("mediaPlayerLock")
|
||||
private State mediaPlayerState = State.NONE;
|
||||
State mMediaPlayerState = State.NONE;
|
||||
|
||||
@RawRes
|
||||
private final int videoRes;
|
||||
private Surface animationSurface;
|
||||
private final int mVideoRes;
|
||||
|
||||
@VisibleForTesting
|
||||
Surface mAnimationSurface;
|
||||
|
||||
|
||||
/**
|
||||
@@ -58,54 +65,54 @@ public class VideoPlayer implements SurfaceTextureListener {
|
||||
}
|
||||
|
||||
private VideoPlayer(Context context, @RawRes int videoRes, TextureView textureView) {
|
||||
this.context = context;
|
||||
this.videoRes = videoRes;
|
||||
this.mContext = context;
|
||||
this.mVideoRes = videoRes;
|
||||
textureView.setSurfaceTextureListener(this);
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
synchronized (mediaPlayerLock) {
|
||||
if (mediaPlayerState == State.STARTED) {
|
||||
mediaPlayerState = State.PAUSED;
|
||||
mediaPlayer.pause();
|
||||
synchronized (mMediaPlayerLock) {
|
||||
if (mMediaPlayerState == State.STARTED) {
|
||||
mMediaPlayerState = State.PAUSED;
|
||||
mMediaPlayer.pause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void resume() {
|
||||
synchronized (mediaPlayerLock) {
|
||||
if (mediaPlayerState == State.PAUSED) {
|
||||
mediaPlayer.start();
|
||||
mediaPlayerState = State.STARTED;
|
||||
synchronized (mMediaPlayerLock) {
|
||||
if (mMediaPlayerState == State.PAUSED) {
|
||||
mMediaPlayer.start();
|
||||
mMediaPlayerState = State.STARTED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Release media player when it's no longer needed. */
|
||||
public void release() {
|
||||
synchronized (mediaPlayerLock) {
|
||||
if (mediaPlayerState != State.NONE && mediaPlayerState != State.END) {
|
||||
mediaPlayerState = State.END;
|
||||
mediaPlayer.release();
|
||||
mediaPlayer = null;
|
||||
synchronized (mMediaPlayerLock) {
|
||||
if (mMediaPlayerState != State.NONE && mMediaPlayerState != State.END) {
|
||||
mMediaPlayerState = State.END;
|
||||
mMediaPlayer.release();
|
||||
mMediaPlayer = null;
|
||||
}
|
||||
}
|
||||
if (animationSurface != null) {
|
||||
animationSurface.release();
|
||||
animationSurface = null;
|
||||
if (mAnimationSurface != null) {
|
||||
mAnimationSurface.release();
|
||||
mAnimationSurface = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
|
||||
animationSurface = new Surface(surface);
|
||||
synchronized (mediaPlayerLock) {
|
||||
mediaPlayer = MediaPlayer.create(context, videoRes);
|
||||
mediaPlayerState = State.PREPARED;
|
||||
mediaPlayer.setSurface(animationSurface);
|
||||
mediaPlayer.setLooping(true);
|
||||
mediaPlayer.start();
|
||||
mediaPlayerState = State.STARTED;
|
||||
mAnimationSurface = new Surface(surface);
|
||||
synchronized (mMediaPlayerLock) {
|
||||
mMediaPlayer = MediaPlayer.create(mContext, mVideoRes);
|
||||
mMediaPlayerState = State.PREPARED;
|
||||
mMediaPlayer.setSurface(mAnimationSurface);
|
||||
mMediaPlayer.setLooping(true);
|
||||
mMediaPlayer.start();
|
||||
mMediaPlayerState = State.STARTED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user