Release the media player when the video preference is not visible.
Mediaplayer is not released when the device is paused, and hence it keep consuming battery. Fixes: 120945749 Test: adb root && watch -n 0.5 -d "adb shell cat /sys/kernel/debug/msm_vidc/core0/inst*/info\|egrep -i \"height\|width\|instance\|fps\|name\"" Use above command to check if any decoder instance exists. Change-Id: Ia0edbba0c2fd3c70753bc36d23d82c0770f95672
This commit is contained in:
@@ -54,6 +54,7 @@ public class VideoPreference extends Preference {
|
|||||||
private float mAspectRadio = 1.0f;
|
private float mAspectRadio = 1.0f;
|
||||||
private int mPreviewResource;
|
private int mPreviewResource;
|
||||||
private boolean mViewVisible;
|
private boolean mViewVisible;
|
||||||
|
private Surface mSurface;
|
||||||
|
|
||||||
public VideoPreference(Context context, AttributeSet attrs) {
|
public VideoPreference(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
@@ -68,17 +69,12 @@ public class VideoPreference extends Preference {
|
|||||||
.authority(context.getPackageName())
|
.authority(context.getPackageName())
|
||||||
.appendPath(String.valueOf(animation))
|
.appendPath(String.valueOf(animation))
|
||||||
.build();
|
.build();
|
||||||
mMediaPlayer = MediaPlayer.create(mContext, mVideoPath);
|
mPreviewResource = attributes.getResourceId(
|
||||||
|
R.styleable.VideoPreference_preview, 0);
|
||||||
|
initMediaPlayer();
|
||||||
if (mMediaPlayer != null && mMediaPlayer.getDuration() > 0) {
|
if (mMediaPlayer != null && mMediaPlayer.getDuration() > 0) {
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
setLayoutResource(R.layout.video_preference);
|
setLayoutResource(R.layout.video_preference);
|
||||||
|
|
||||||
mPreviewResource = attributes.getResourceId(
|
|
||||||
R.styleable.VideoPreference_preview, 0);
|
|
||||||
|
|
||||||
mMediaPlayer.setOnSeekCompleteListener(mp -> mVideoReady = true);
|
|
||||||
|
|
||||||
mMediaPlayer.setOnPreparedListener(mediaPlayer -> mediaPlayer.setLooping(true));
|
|
||||||
mAnimationAvailable = true;
|
mAnimationAvailable = true;
|
||||||
updateAspectRatio();
|
updateAspectRatio();
|
||||||
} else {
|
} else {
|
||||||
@@ -127,9 +123,8 @@ public class VideoPreference extends Preference {
|
|||||||
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
|
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width,
|
||||||
int height) {
|
int height) {
|
||||||
if (mMediaPlayer != null) {
|
if (mMediaPlayer != null) {
|
||||||
mMediaPlayer.setSurface(new Surface(surfaceTexture));
|
mSurface = new Surface(surfaceTexture);
|
||||||
mVideoReady = false;
|
mMediaPlayer.setSurface(mSurface);
|
||||||
mMediaPlayer.seekTo(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,26 +163,40 @@ public class VideoPreference extends Preference {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDetached() {
|
public void onDetached() {
|
||||||
if (mMediaPlayer != null) {
|
releaseMediaPlayer();
|
||||||
mMediaPlayer.stop();
|
|
||||||
mMediaPlayer.reset();
|
|
||||||
mMediaPlayer.release();
|
|
||||||
}
|
|
||||||
super.onDetached();
|
super.onDetached();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onViewVisible(boolean videoPaused) {
|
public void onViewVisible(boolean videoPaused) {
|
||||||
mViewVisible = true;
|
mViewVisible = true;
|
||||||
mVideoPaused = videoPaused;
|
mVideoPaused = videoPaused;
|
||||||
if (mVideoReady && mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
|
initMediaPlayer();
|
||||||
mMediaPlayer.seekTo(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onViewInvisible() {
|
public void onViewInvisible() {
|
||||||
mViewVisible = false;
|
mViewVisible = false;
|
||||||
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
|
releaseMediaPlayer();
|
||||||
mMediaPlayer.pause();
|
}
|
||||||
|
|
||||||
|
private void initMediaPlayer() {
|
||||||
|
if (mMediaPlayer == null) {
|
||||||
|
mMediaPlayer = MediaPlayer.create(mContext, mVideoPath);
|
||||||
|
mMediaPlayer.seekTo(0);
|
||||||
|
mMediaPlayer.setOnSeekCompleteListener(mp -> mVideoReady = true);
|
||||||
|
mMediaPlayer.setOnPreparedListener(mediaPlayer -> mediaPlayer.setLooping(true));
|
||||||
|
if (mSurface != null) {
|
||||||
|
mMediaPlayer.setSurface(mSurface);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void releaseMediaPlayer() {
|
||||||
|
if (mMediaPlayer != null) {
|
||||||
|
mMediaPlayer.stop();
|
||||||
|
mMediaPlayer.reset();
|
||||||
|
mMediaPlayer.release();
|
||||||
|
mMediaPlayer = null;
|
||||||
|
mVideoReady = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +206,6 @@ public class VideoPreference extends Preference {
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void updateAspectRatio() {
|
void updateAspectRatio() {
|
||||||
mAspectRadio = mMediaPlayer.getVideoWidth() / (float)mMediaPlayer.getVideoHeight();
|
mAspectRadio = mMediaPlayer.getVideoWidth() / (float) mMediaPlayer.getVideoHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -92,4 +92,13 @@ public class VideoPreferenceTest {
|
|||||||
|
|
||||||
verify(mMediaPlayer, never()).start();
|
verify(mMediaPlayer, never()).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onViewInvisible_shouldReleaseMediaplayer() {
|
||||||
|
mVideoPreference.onViewVisible(false);
|
||||||
|
|
||||||
|
mVideoPreference.onViewInvisible();
|
||||||
|
|
||||||
|
verify(mMediaPlayer).release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user